-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cpp
More file actions
187 lines (154 loc) · 3.49 KB
/
Copy pathCommon.cpp
File metadata and controls
187 lines (154 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma warning(disable:4996)
#include "Common.h"
#include <cstdarg>
#include <assert.h>
#include <windows.h>
USING_NS_BF;
String Beefy::vformat(const char* fmt, va_list argPtr)
{
// We draw the line at a 1MB string.
const int maxSize = 1000000;
//va_list checkArgPtr = argPtr;
//va_start(checkArgPtr, fmt);
int argIdx = 0;
char* newFmt = NULL;
char tempBuff[2048];
char* tempBuffPtr = tempBuff;
#ifdef _WIN32
for (int i = 0; fmt[i] != 0; i++)
{
if (fmt[i] == '%')
{
if (fmt[i + 1] == '%')
{
i++;
continue;
}
#ifdef BF32
bool isLongAddr = false;
#else
bool isLongAddr = true;
#endif
if ((fmt[i + 1] == 'l') && (fmt[i + 2] == '@'))
{
isLongAddr = true;
i++;
}
if (fmt[i + 1] == '@')
{
if (newFmt == NULL)
{
newFmt = (char*)malloc(strlen(fmt) + 1);
strcpy(newFmt, fmt);
//newFmt = strdup(fmt);
}
newFmt[i + 1] = 's';
const char*& argValPtr = *(const char**)((intptr*)argPtr + argIdx);
if (isLongAddr)
{
int64 iVal = *(int64*)((intptr*)argPtr + argIdx);
#ifdef BF32
argIdx++; // Takes two spots
#endif
argValPtr = tempBuffPtr;
int leftVal = (int)(iVal >> 32);
if (leftVal != 0)
{
sprintf(tempBuffPtr, "%x", leftVal);
tempBuffPtr += strlen(tempBuffPtr);
}
tempBuffPtr[0] = '\'';
tempBuffPtr++;
sprintf(tempBuffPtr, "%08x", (int)iVal);
tempBuffPtr += strlen(tempBuffPtr) + 1;
}
else
{
int32 iVal = (int32)(intptr)argValPtr;
argValPtr = tempBuffPtr;
sprintf(tempBuffPtr, "%08x", iVal);
tempBuffPtr += strlen(tempBuffPtr) + 1;
}
if (newFmt[i] == 'l')
newFmt[i] = '+';
}
else
{
//const char*& argValPtr = va_arg(checkArgPtr, const char*);
}
argIdx++;
}
}
#endif
if (newFmt != NULL)
{
Beefy::String retVal = vformat(newFmt, argPtr);
free(newFmt);
return retVal;
}
// If the string is less than 161 characters,
// allocate it on the stack because this saves
// the malloc/free time.
const int bufSize = 161;
char stackBuffer[bufSize];
int attemptedSize = bufSize - 1;
int numChars = 0;
#ifdef _WIN32
numChars = _vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);
#else
numChars = vsnprintf(stackBuffer, attemptedSize, fmt, argPtr);
#endif
if ((numChars >= 0) && (numChars <= attemptedSize))
{
// Needed for case of 160-character printf thing
stackBuffer[numChars] = '\0';
// Got it on the first try.
return String(stackBuffer);
}
// Now use the heap.
char* heapBuffer = NULL;
while (((numChars == -1) || (numChars > attemptedSize)) &&
(attemptedSize < maxSize))
{
// Try a bigger size
attemptedSize *= 2;
heapBuffer = (char*)realloc(heapBuffer, (attemptedSize + 1));
#ifdef _WIN32
numChars = _vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
#else
numChars = vsnprintf(heapBuffer, attemptedSize, fmt, argPtr);
#endif
}
if (numChars == -1)
{
free(heapBuffer);
return "";
}
heapBuffer[numChars] = 0;
Beefy::String aResult = Beefy::String(heapBuffer);
free(heapBuffer);
return aResult;
}
String Beefy::StrFormat(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
String aResult = vformat(fmt, argList);
va_end(argList);
return aResult;
}
void Beefy::OutputDebugStrF(const char* fmt ...)
{
va_list argList;
va_start(argList, fmt);
String aResult = vformat(fmt, argList);
va_end(argList);
::OutputDebugStringA(aResult.c_str());
}
void Beefy::BFFatalError(const char* message, const char* file, int line)
{
assert(0);
while (true)
{
}
}