forked from Nomoresleep/hlslparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.cpp
162 lines (124 loc) · 3.48 KB
/
Engine.cpp
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
#include "Engine.h"
#include <stdio.h> // vsnprintf
#include <string.h> // strcmp, strcasecmp
#include <stdlib.h> // strtod, strtol
namespace M4 {
// Engine/String.cpp
int String_PrintfArgList(char * buffer, int size, const char * format, va_list args) {
va_list tmp;
va_copy(tmp, args);
#if _MSC_VER >= 1400
int n = vsnprintf_s(buffer, size, _TRUNCATE, format, tmp);
#else
int n = vsnprintf(buffer, size, format, tmp);
#endif
va_end(tmp);
if (n < 0 || n > size) return -1;
return n;
}
int String_Printf(char * buffer, int size, const char * format, ...) {
va_list args;
va_start(args, format);
int n = String_PrintfArgList(buffer, size, format, args);
va_end(args);
return n;
}
int String_FormatFloat(char * buffer, int size, float value) {
return String_Printf(buffer, size, "%f", value);
}
bool String_Equal(const char * a, const char * b) {
if (a == b) return true;
if (a == NULL || b == NULL) return false;
return strcmp(a, b) == 0;
}
bool String_EqualNoCase(const char * a, const char * b) {
if (a == b) return true;
if (a == NULL || b == NULL) return false;
#if _MSC_VER
return _stricmp(a, b) == 0;
#else
return strcasecmp(a, b) == 0;
#endif
}
double String_ToDouble(const char * str, char ** endptr) {
return strtod(str, endptr);
}
int String_ToInteger(const char * str, char ** endptr) {
return strtol(str, endptr, 10);
}
int String_ToIntegerHex(const char * str, char ** endptr) {
return strtol(str, endptr, 16);
}
// Engine/StringPool.cpp
StringPool::StringPool(Allocator * allocator) : stringArray(allocator) {
}
StringPool::~StringPool() {
for (int i = 0; i < stringArray.GetSize(); i++) {
free((void *)stringArray[i]);
stringArray[i] = NULL;
}
}
const char * StringPool::AddString(const char * string) {
for (int i = 0; i < stringArray.GetSize(); i++) {
if (String_Equal(stringArray[i], string)) return stringArray[i];
}
#if _MSC_VER
const char * dup = _strdup(string);
#else
const char * dup = strdup(string);
#endif
stringArray.PushBack(dup);
return dup;
}
// @@ From mprintf.cpp
static char *mprintf_valist(int size, const char *fmt, va_list args) {
ASSERT(size > 0);
char *res = NULL;
va_list tmp;
while (1) {
res = new char[size];
if (!res) return NULL;
va_copy(tmp, args);
int len = vsnprintf(res, size, fmt, tmp);
va_end(tmp);
if ((len >= 0) && (size >= len + 1)) {
break;
}
delete [] res;
if (len > -1 ) {
size = len + 1;
}
else {
size *= 2;
}
}
return res;
}
const char * StringPool::AddStringFormatList(const char * format, va_list args) {
va_list tmp;
va_copy(tmp, args);
const char * string = mprintf_valist(256, format, tmp);
va_end(tmp);
for (int i = 0; i < stringArray.GetSize(); i++) {
if (String_Equal(stringArray[i], string)) {
delete [] string;
return stringArray[i];
}
}
stringArray.PushBack(string);
return string;
}
const char * StringPool::AddStringFormat(const char * format, ...) {
va_list args;
va_start(args, format);
const char * string = AddStringFormatList(format, args);
va_end(args);
return string;
}
bool StringPool::GetContainsString(const char * string) const {
for (int i = 0; i < stringArray.GetSize(); i++) {
if (String_Equal(stringArray[i], string)) return true;
}
return false;
}
} // M4 namespace