#include "config.h" #include #include // compare strings bool StringCompare(const string &word1, const string &word2) { if (word1.compare(word2) < 0) return true; return false; } bool StringIdentical(const string &word1, const string &word2) { if (word1.compare(word2) == 0) return true; return false; } // safe print string void SAFE_SPRINTF(char *buffer, size_t count, const char *format, ...) { if (buffer && count > 0) { va_list argptr; va_start(argptr, format); int chars = _vsnprintf(buffer, count, format, argptr); va_end(argptr); if (chars == count) { // no null-terminator was appended, we need to do that ourselves buffer[count-1] = '\0'; } else if (chars == -1) { // length of formatted string exceeded length of buffer, and no // null-terminator was appended buffer[count-1] = '\0'; } } } // print string using variable amount of arguments char* SAFE_VAPRINT(const char *format, ...) { va_list argptr; static char string[2][32000]; // in case va is called by nested functions static int index = 0; char *buf; buf = string[index & 1]; index++; va_start(argptr, format); _vsnprintf(buf, 31999, format, argptr); va_end(argptr); return buf; } // easy print to debug window void SAFE_DEBUG(const char *format, ...) { va_list argptr; static char string[2][32000]; // in case va is called by nested functions static int index = 0; char *buf; buf = string[index & 1]; index++; va_start(argptr, format); _vsnprintf(buf, 31999, format, argptr); va_end(argptr); OutputDebugString(buf); } // safe print string to file and immediately flush it void SAFE_FLUSHPRINT(FILE *file, const char *format, ...) { va_list argptr; va_start(argptr, format); vfprintf(file, format, argptr); va_end(argptr); fflush(file); } // safe read line from file bool SAFE_GETLINE(char *buffer, unsigned int buffersize, unsigned int &linelength, FILE *file) { if (buffer == NULL || buffersize == 0 || file == NULL) return false; // read line from file if (fgets(buffer, buffersize, file) == NULL) return false; // trim newline character that fgets normally reads as well linelength = (unsigned int)strlen(buffer); if (linelength > 0 && buffer[linelength-1] == '\n') buffer[--linelength] = '\0'; return true; } // quick load file from disk bool SAFE_LOADFILE(const char *fname, unsigned char **data, unsigned int &length) { if (fname == NULL || data == NULL) return false; FILE *file = fopen(fname, "rb"); if (file == NULL) return false; // load the file bool success = SAFE_LOADFILE(file, data, length); fclose(file); return success; } bool SAFE_LOADFILE(FILE *file, unsigned char **data, unsigned int &length) { // determine size of file if (fseek(file, 0L, SEEK_END) != 0) return false; long pos = ftell(file); if (pos <= 0) return false; rewind(file); // read all data unsigned char *dat = NEW unsigned char[pos]; if (fread(dat, pos, 1, file) != 1) { SAFE_DELETE_ARRAY(dat); return false; } *data = dat; length = pos; return true; } static int stderro = -1; static FILE *redirect = NULL; bool RedirectStderr(const char *fname) { if (redirect != NULL) return false; stderro = _dup(_fileno(stderr)); if (stderro == -1) return false; redirect = fopen(fname, "a"); if (redirect == NULL) return false; if (_dup2(_fileno(redirect), 2) == -1) { fclose(redirect); return false; } return true; } bool RestoreStderr() { if (redirect == NULL) return false; fflush(stderr); _dup2(stderro, _fileno(stderr)); fclose(redirect); redirect = NULL; return true; } void CenterWindow(HWND hWnd) { if (hWnd == NULL) return; // center the window (code taken from MSDN: "Using Dialog Boxes") HWND hParent; RECT rcParent, rcWnd, rc; if ((hParent = GetParent(hWnd)) == NULL) { hParent = GetDesktopWindow(); assert(hParent != NULL); } GetWindowRect(hParent, &rcParent); GetWindowRect(hWnd, &rcWnd); CopyRect(&rc, &rcParent); OffsetRect(&rcWnd, -rcWnd.left, -rcWnd.top); OffsetRect(&rc, -rc.left, -rc.top); OffsetRect(&rc, -rcWnd.right, -rcWnd.bottom); SetWindowPos(hWnd, HWND_TOP, rcParent.left + (rc.right / 2), rcParent.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE); }