1 | #include "config.h"
|
---|
2 |
|
---|
3 | #include <io.h>
|
---|
4 | #include <stdarg.h>
|
---|
5 |
|
---|
6 | // compare strings
|
---|
7 | bool StringCompare(const string &word1, const string &word2)
|
---|
8 | {
|
---|
9 | if (word1.compare(word2) < 0)
|
---|
10 | return true;
|
---|
11 | return false;
|
---|
12 | }
|
---|
13 | bool StringIdentical(const string &word1, const string &word2)
|
---|
14 | {
|
---|
15 | if (word1.compare(word2) == 0)
|
---|
16 | return true;
|
---|
17 | return false;
|
---|
18 | }
|
---|
19 |
|
---|
20 | // safe print string
|
---|
21 | void SAFE_SPRINTF(char *buffer, size_t count, const char *format, ...)
|
---|
22 | {
|
---|
23 | if (buffer && count > 0)
|
---|
24 | {
|
---|
25 | va_list argptr;
|
---|
26 | va_start(argptr, format);
|
---|
27 | int chars = _vsnprintf(buffer, count, format, argptr);
|
---|
28 | va_end(argptr);
|
---|
29 | if (chars == count)
|
---|
30 | {
|
---|
31 | // no null-terminator was appended, we need to do that ourselves
|
---|
32 | buffer[count-1] = '\0';
|
---|
33 | }
|
---|
34 | else if (chars == -1)
|
---|
35 | {
|
---|
36 | // length of formatted string exceeded length of buffer, and no
|
---|
37 | // null-terminator was appended
|
---|
38 | buffer[count-1] = '\0';
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | // print string using variable amount of arguments
|
---|
44 | char* SAFE_VAPRINT(const char *format, ...)
|
---|
45 | {
|
---|
46 | va_list argptr;
|
---|
47 | static char string[2][32000]; // in case va is called by nested functions
|
---|
48 | static int index = 0;
|
---|
49 | char *buf;
|
---|
50 |
|
---|
51 | buf = string[index & 1];
|
---|
52 | index++;
|
---|
53 |
|
---|
54 | va_start(argptr, format);
|
---|
55 | _vsnprintf(buf, 31999, format, argptr);
|
---|
56 | va_end(argptr);
|
---|
57 |
|
---|
58 | return buf;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // easy print to debug window
|
---|
62 | void SAFE_DEBUG(const char *format, ...)
|
---|
63 | {
|
---|
64 | va_list argptr;
|
---|
65 | static char string[2][32000]; // in case va is called by nested functions
|
---|
66 | static int index = 0;
|
---|
67 | char *buf;
|
---|
68 |
|
---|
69 | buf = string[index & 1];
|
---|
70 | index++;
|
---|
71 |
|
---|
72 | va_start(argptr, format);
|
---|
73 | _vsnprintf(buf, 31999, format, argptr);
|
---|
74 | va_end(argptr);
|
---|
75 |
|
---|
76 | OutputDebugString(buf);
|
---|
77 | }
|
---|
78 |
|
---|
79 | // safe print string to file and immediately flush it
|
---|
80 | void SAFE_FLUSHPRINT(FILE *file, const char *format, ...)
|
---|
81 | {
|
---|
82 | va_list argptr;
|
---|
83 | va_start(argptr, format);
|
---|
84 | vfprintf(file, format, argptr);
|
---|
85 | va_end(argptr);
|
---|
86 | fflush(file);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // safe read line from file
|
---|
90 | bool SAFE_GETLINE(char *buffer, unsigned int buffersize, unsigned int &linelength, FILE *file)
|
---|
91 | {
|
---|
92 | if (buffer == NULL || buffersize == 0 || file == NULL)
|
---|
93 | return false;
|
---|
94 | // read line from file
|
---|
95 | if (fgets(buffer, buffersize, file) == NULL)
|
---|
96 | return false;
|
---|
97 | // trim newline character that fgets normally reads as well
|
---|
98 | linelength = (unsigned int)strlen(buffer);
|
---|
99 | if (linelength > 0 && buffer[linelength-1] == '\n')
|
---|
100 | buffer[--linelength] = '\0';
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // quick load file from disk
|
---|
105 | bool SAFE_LOADFILE(const char *fname, unsigned char **data, unsigned int &length)
|
---|
106 | {
|
---|
107 | if (fname == NULL || data == NULL)
|
---|
108 | return false;
|
---|
109 | FILE *file = fopen(fname, "rb");
|
---|
110 | if (file == NULL)
|
---|
111 | return false;
|
---|
112 | // load the file
|
---|
113 | bool success = SAFE_LOADFILE(file, data, length);
|
---|
114 | fclose(file);
|
---|
115 | return success;
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool SAFE_LOADFILE(FILE *file, unsigned char **data, unsigned int &length)
|
---|
119 | {
|
---|
120 | // determine size of file
|
---|
121 | if (fseek(file, 0L, SEEK_END) != 0)
|
---|
122 | return false;
|
---|
123 | long pos = ftell(file);
|
---|
124 | if (pos <= 0)
|
---|
125 | return false;
|
---|
126 | rewind(file);
|
---|
127 | // read all data
|
---|
128 | unsigned char *dat = NEW unsigned char[pos];
|
---|
129 | if (fread(dat, pos, 1, file) != 1)
|
---|
130 | {
|
---|
131 | SAFE_DELETE_ARRAY(dat);
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 | *data = dat;
|
---|
135 | length = pos;
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static int stderro = -1;
|
---|
140 | static FILE *redirect = NULL;
|
---|
141 | bool RedirectStderr(const char *fname)
|
---|
142 | {
|
---|
143 | if (redirect != NULL)
|
---|
144 | return false;
|
---|
145 | stderro = _dup(_fileno(stderr));
|
---|
146 | if (stderro == -1)
|
---|
147 | return false;
|
---|
148 | redirect = fopen(fname, "a");
|
---|
149 | if (redirect == NULL)
|
---|
150 | return false;
|
---|
151 | if (_dup2(_fileno(redirect), 2) == -1)
|
---|
152 | {
|
---|
153 | fclose(redirect);
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool RestoreStderr()
|
---|
160 | {
|
---|
161 | if (redirect == NULL)
|
---|
162 | return false;
|
---|
163 | fflush(stderr);
|
---|
164 | _dup2(stderro, _fileno(stderr));
|
---|
165 | fclose(redirect);
|
---|
166 | redirect = NULL;
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 |
|
---|
170 | void CenterWindow(HWND hWnd)
|
---|
171 | {
|
---|
172 | if (hWnd == NULL)
|
---|
173 | return;
|
---|
174 | // center the window (code taken from MSDN: "Using Dialog Boxes")
|
---|
175 | HWND hParent;
|
---|
176 | RECT rcParent, rcWnd, rc;
|
---|
177 | if ((hParent = GetParent(hWnd)) == NULL)
|
---|
178 | {
|
---|
179 | hParent = GetDesktopWindow();
|
---|
180 | assert(hParent != NULL);
|
---|
181 | }
|
---|
182 | GetWindowRect(hParent, &rcParent);
|
---|
183 | GetWindowRect(hWnd, &rcWnd);
|
---|
184 | CopyRect(&rc, &rcParent);
|
---|
185 | OffsetRect(&rcWnd, -rcWnd.left, -rcWnd.top);
|
---|
186 | OffsetRect(&rc, -rc.left, -rc.top);
|
---|
187 | OffsetRect(&rc, -rcWnd.right, -rcWnd.bottom);
|
---|
188 | SetWindowPos(hWnd, HWND_TOP, rcParent.left + (rc.right / 2),
|
---|
189 | rcParent.top + (rc.bottom / 2), 0, 0, SWP_NOSIZE);
|
---|
190 | }
|
---|