/
util-basics.cpp
126 lines (103 loc) · 2.67 KB
/
util-basics.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
#include "util.h"
#include <cstdarg>
#include <cstdio>
#include <limits>
namespace util
{
extern const float infinity = std::numeric_limits<float>::infinity();
extern const float NaN = std::numeric_limits<float>::quiet_NaN();
void __declspec(noreturn) exit(const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
::exit(1);
}
bool LoadFile(const char * path, std::vector<byte> * pDataOut, LFK lfk /*= LFK_Binary*/)
{
ASSERT_ERR(path);
ASSERT_ERR(pDataOut);
FILE * pFile = nullptr;
if (fopen_s(&pFile, path, (lfk == LFK_Text) ? "rt" : "rb") != 0)
{
WARN("Couldn't open file %s", path);
return false;
}
ASSERT_ERR(pFile);
// Determine file size
fseek(pFile, 0, SEEK_END);
size_t size = ftell(pFile);
// Read the whole file into memory
pDataOut->resize((lfk == LFK_Text) ? size+1 : size);
rewind(pFile);
size_t sizeRead = fread(&(*pDataOut)[0], sizeof(byte), size, pFile);
// Size can be smaller for text files, due to newline conversion
ASSERT_ERR(sizeRead == size || ((lfk == LFK_Text) && sizeRead <= size));
fclose(pFile);
// Automatically null-terminate text files so string functions can be used
if (lfk == LFK_Text)
{
pDataOut->resize(sizeRead + 1);
(*pDataOut)[sizeRead] = 0;
}
return true;
}
inline bool isDelimiter(char c, const char * delim)
{
for (; *delim; ++delim)
{
if (c == *delim)
return true;
}
return false;
}
char * tokenize(char * & str, const char * delim)
{
if (!str || !*str || !delim)
return nullptr;
// Find the start of the next token
char * pStartToken = str;
while (*pStartToken && isDelimiter(*pStartToken, delim))
++pStartToken;
// Did we hit the end of the string?
if (!*pStartToken)
{
str = pStartToken;
return nullptr;
}
// Find the end of the token
char * pEndToken = pStartToken;
while (*pEndToken && !isDelimiter(*pEndToken, delim))
++pEndToken;
// Did we hit the end of the string?
if (!*pEndToken)
{
str = pEndToken;
return pStartToken;
}
// Terminate the token and update str to the following character
*pEndToken = 0;
str = pEndToken + 1;
return pStartToken;
}
char * tokenizeConsecutive(char * & str, const char * delim)
{
if (!str || !*str || !delim)
return nullptr;
char * pStartToken = str;
// Find the end of the token
char * pEndToken = pStartToken;
while (*pEndToken && !isDelimiter(*pEndToken, delim))
++pEndToken;
// Did we hit the end of the string?
if (!*pEndToken)
{
str = pEndToken;
return pStartToken;
}
// Terminate the token and update str to the following character
*pEndToken = 0;
str = pEndToken + 1;
return pStartToken;
}
}