-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.cpp
132 lines (109 loc) · 2.65 KB
/
misc.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
#include <stdarg.h> // va_*()
#include <stdio.h>
#include <string>
#include "misc.h"
namespace RLE
{
template<>
bool Read(bool* dest, int len, u8* src)
{
return Read((u8*)dest, len, src);
}
}
int sgn(int x)
{
if (x>0)
return 1;
else if (x<0)
return -1;
return 0;
}
//////////////////////////////////////////////////////////
// String manupulation. (TODO: get rid of all this) //
//////////////////////////////////////////////////////////
char* va(const char* format, ...)
{
va_list argptr;
static char str[1024];
va_start(argptr, format);
vsprintf(str, format, argptr);
va_end(argptr);
return str;
}
std::string trimString(const std::string& s) {
std::string t;
// Find the first nonwhitespace character
for (unsigned int i = 0; i < s.length(); i++) {
if (s[i] != ' ') {
t = s.substr(i);
break;
}
}
// Find the last nonwhitespace character
for (int i = t.length()-1; i >= 0; i++) {
if (t[i] != ' ') {
t = t.substr(0, i);
return t;
}
}
return ""; // if we've made it this far, then every character in the std::string is whitespace.
}
std::vector<std::string> splitString(const std::string& s, const std::string& delimiters) {
std::vector<std::string> sv;
unsigned int p = 0;
int pos = 0;
for (unsigned int i = 0; i < s.length(); i++) {
if (( pos = delimiters.find(s[i]) ) != -1) {
std::string st = s.substr(p, i - p);
if (st.length() > 0) {
sv.push_back(st);
}
p = i + 1;
i++;
}
}
if (p < s.length())
sv.push_back(s.substr(p)); // Push whatever remains (if anything remains)
return sv;
}
std::string stripExtension(const std::string& s) {
int p = s.rfind('.');
if (p != -1)
return s.substr(0, p);
else
return s;
}
std::string getExtension(const std::string& s) {
int p = s.rfind('.');
if (p != -1)
return s.substr(p);
else
return "";
}
std::string getPath(const std::string& s) {
int p = s.rfind('/');
if (p != -1)
return s.substr(0, p + 1 );
else
return "";
}
std::string getFilename(const std::string& s) {
int p = s.rfind('/');
if (p != -1)
return s.substr(p+1);
else
return s;
}
// suboptimal. meh.
std::string lowerCase(const std::string& s) {
std::string t;
t.reserve(s.length());
for (unsigned int i = 0; i<s.length(); i++)
{
char c = s[i];
if (c>='A' && c<='Z')
c|=32;
t+=c;
}
return t;
}