📄 2002-12-12_proxymain.cpp
字号:
/*
+-----------------------------------------------------------------------------+
| |
| proxymain.cpp |
| Last change: yyyy-mm-dd |
| |
| Author: Jan Krumsiek |
| eMail: proxy@krumsiek.com |
| Web: http://www.krumsiek.com/proxy |
| |
| Copyright 2003 - Jan Krumsiek |
| This source code can freely be modified and redistributed. No liability |
| whatsoever is taken by the author for any use of this software. |
| |
| Description: |
| This module contains general functions which are used by the rest of the |
| project. There is much platform-dependent code in here, you can see this |
| at the #ifdef preprocessor directives. |
| |
+-----------------------------------------------------------------------------+
*/
#include "proxymain.h"
#include <string.h>
#include "stdio.h"
#include "cfgfiles.h"
#include "memory.h"
#include "time.h"
#ifdef WIN32
#include "windows.h"
#endif
void GetFileCont(CHARPTR filename, CHARPTR* cont, UINT* length)
// this function reads the contents of 'filename' and saves a
// new pointer in *cont and file length is saved in *length
{
FILE* open=0;
fpos_t pos;
// Get file length
*length = GetFileLength(filename) ; // [DBG]
// Open file
open = fopen(filename,"r");
// If open == 0 then failed to open file
if (open == 0)
{
*cont = 0;
}
else
{
// Allocate memory for file contents
*cont = new char[int(length)];
// Read from file
fread(*cont,*length,1,open);
// Close file
fclose(open);
}
}
void WriteToFile(CHARPTR filepath, CHARPTR data)
// opens or creates a file and writes 'data' into it
// 'data' must be zero-terminated
{
// open file
FILE* hfile = fopen(filepath,"w+");
// write to file
fputs(data, hfile);
// close file
fclose(hfile);
}
void ErrorAbort()
// simple macro which writes an abort message to the screen
{
cout << endl << endl << "Program aborted! See error message(s) above" << endl;
}
void* CopyAndPtr(void* src, UINT length)
// copies 'length' bytes from src to new allocated memory, puts
// a \0 at the end and returns a pointer to that memory
{
CHARPTR ptr_return = new char[length+1];
memcpy(ptr_return,src,length);
ptr_return[length] = 0;
return ptr_return;
}
CHARPTR ConnectStrings(CHARPTR str1, CHARPTR str2)
// connects two strings and returns pointer to new string
{
UINT len1 = strlen(str1);
UINT len2 = strlen(str2);
CHARPTR ret = new char[len1+len2+1];
strcpy(ret,str1);
strcat(ret,str2);
return ret;
}
CHARPTR GetID()
// Produces an unique identifier of 10 chars length (+ zero)
//(unique at least for the current machine)
// using the current time
{
CHARPTR ret = new char[11];
static UINT count=0;
static time_t oldtime=0;
time_t newtime;
newtime = time(NULL);
// the static variable will be increased until time since last call has changed
// for allowing the program to produce more than one ID per second
if (oldtime != newtime)
count = 0;
else
count ++;
// write amount of seconds since 1970 to ret in hex-form
sprintf(ret,"%08x%02x\0",newtime,count);
// I'm adding the current counter as two hexadecimal digits
// this will allow 256 different IDs per second
oldtime = newtime;
return ret;
}
CHARPTR GetFilename()
// creates a filename using GetID()
{
// filename will be 16 bytes: /IIIIIIIIII.txt0
CHARPTR ret = new char[16];
CHARPTR id = GetID();
sprintf(ret,"/%s.txt\0",id);
delete id;
return ret;
}
UINT GetFileLength(CHARPTR path)
// returns the size of a file
{
#ifdef WIN32
// use WIN32 GetFileSize function
// open file
HANDLE file = CreateFile(path ,
GENERIC_READ, 0, 0, OPEN_EXISTING,0 , 0);
UINT ret = GetFileSize(file, NULL);
CloseHandle(file);
return ret;
#endif
}
void CreateDir(CHARPTR name)
// creates a directory
{
#ifdef WIN32
// use win32 CreateDirectory function
CreateDirectory(name,0);
#endif
}
void KillFile(CHARPTR file)
// deletes a file
{
#ifdef WIN32
DeleteFile(file);
#endif
}
int FileExists(CHARPTR name)
// checks if a file exits
{
#ifdef WIN32
// use WIN32 CreateFile() to check if file can be opened
HANDLE check = CreateFile(name,GENERIC_READ, 0, 0, OPEN_EXISTING, 0,0);
CloseHandle(check);
return (check == (void*)0xFFFFFFFF) ? false : true;
#endif
}
void WriteToLog(FILE* logfile, CHARPTR entry)
// prints to contents of 'entry' prefixed with the current date/time
// to the log file 'logfile'
{
// determine current time
time_t curtime;
curtime = time(NULL);
// convert to local time and place in readable struct
struct tm *loctime;
loctime = localtime(&curtime);
// reserve memory for string to be printed
// length of string + 1 (zero-terminator) + 22 bytes + [LF]
// -> 'yyyy-mm-dd hh:mm:ss - '
CHARPTR spr = new char[22 + 1 + strlen(entry)];
// print str
sprintf(spr, "%04u-%02u-%02u %02u:%02u:%02u - %s\x0A\0",
loctime->tm_year+1900, loctime->tm_mon+1, loctime->tm_mday,
loctime->tm_hour, loctime->tm_min, loctime->tm_sec,
entry);
fwrite(spr, 1, strlen(spr), logfile);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -