Menu

[860f38]: / d2cpp / string_utils.cpp  Maximize  Restore  History

Download this file

204 lines (156 with data), 5.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "string_utils.h"
#include <cctype>
#include <locale>
using namespace std;
/// Returns index in path where the file extension begins (=last dot).
static DPath::size_type extPosInPath(DPath const& path)
{
DPath::size_type n = path.find_last_of(".\\/:");
return ( path[n]=='.' ) ? n : DPath::npos;
}
/// Returns index in @a path where the file name begins
static DPath::size_type namePosInPath(DPath const& path)
{
DPath::size_type n = path.find_last_of("\\/:");
return ( DPath::npos == n ) ? 0 : n+1;
}
/// Returns @a path with a different file extension
DPath changeFileExt(DPath const& path, std::string const& newExt)
{
return DPath( path, 0, extPosInPath(path) ) + newExt;
}
/// Returns the extension-free file name extracted from @a path
std::string baseName(DPath const& path)
{
DPath::size_type n = namePosInPath(path);
return DPath( path, n, extPosInPath(path)-n );
}
/// Case-insensitive string comparisons (is provided by some platforms as stricmp).
int my_stricmp(const char* p, const char* q)
{
for(;;) {
int c = toupper( (unsigned char)*p++ );
int d = toupper( (unsigned char)*q++ );
if( c != d ) return (c<d) ? -1 : +1;
if( c=='\0' ) return 0; // => end of string: c == d == '\0'
}
}
char const kBlankChars[] = " \t\n\r";
std::string rtrimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const last = str.find_last_not_of(sepSet);
return ( last==std::string::npos )
? std::string()
: str.substr(0, last+1);
}
std::string trimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}
/// Replaces in @a ioStre the first occurence of @a findStr with @a replStr. @return true iff a substitution was made.
bool replaceFirst(std::string& ioStr, std::string const& findStr, std::string const& replStr)
{
std::string::size_type const pos = ioStr.find(findStr);
if( std::string::npos == pos )
return false;
ioStr.replace( pos, findStr.size(), replStr );
return true;
}
/// Returns @a str as a lowercase string -- converted using std::toupper.
std::string toupper(std::string const& str)
{
std::string ans( str.size(), '\0' );
std::string::const_iterator src = str.begin();
std::string::const_iterator end = str.end();
std::string::iterator dst = ans.begin();
for( ; src != end ; ++src, ++dst )
*dst = (char)std::toupper((unsigned char)*src);
return ans;
}
DStrPtrMap sIDFilter; /// map used to filter identifier names received
/// Load a static table of string pairs into sIDFilter (see baseIDFilterTable).
void addIDFilterTable( char const*const* pairTable )
{
while( const char* src = *pairTable++ )
sIDFilter.insert( make_pair<const char*,const char*>(src, *pairTable++) );
}
/** Table where previously encountered identifiers are stored.
Used to convert subsequent encouters of the same identifier
to use the same case (C++ is case-sensitive, Delphi is not).
*/
DStrCaselessSet sIDCaseSet;
/* can be used to enforce a specific case to some identifiers ... currently not used
void addIDCaseDefs( const char* const* idTable )
{
while( const char* id = *idTable++ )
sIDCaseSet.insert(id);
}
*/
/// Returns @a id, or a converted indentifier if it is defined in sIDFilter.
char const* filterID( char const* id )
{
DStrPtrMap::iterator const pos = sIDFilter.find(id);
if( pos!=sIDFilter.end() ) return pos->second;
// translate the identifier using or table of previous encounters
return sIDCaseSet.insert(id).first->c_str();
}
/// Default conversion of standard identidiers
const char* const baseIDFilterTable[] =
// conversion of common Delphi and OpenGL types
{ "integer", "int"
, "dword", "unsigned long"
, "longint", "long"
, "single", "float"
, "real", "double"
, "double", "double"
, "byte", "unsigned char"
, "word", "unsigned short"
, "longword", "unsigned long"
, "cardinal", "unsigned long"
, "shortstring", "std::string"
, "shortint", "signed char"
, "smallint", "short"
, "int64", "__int64"
, "char", "char"
, "boolean", "bool"
// Common Pointer types
, "PChar", "char*"
, "PWord", "unsigned short*"
, "PDWORD", "unsigned long*"
// some OpenGL stuff
, "gldouble", "double"
, "glfloat", "float"
, "glboolean", "bool"
, "glbyte", "unsigned char"
// common math functions
, "arcsin", "asin"
, "arccos", "acos"
, "arctan", "atan"
, "arctan2", "atan2"
// case-conversions
, "true", "true"
, "false", "false"
, "result", "result"
, "min", "min"
, "max", "max"
, "abs", "abs"
, "sqrt", "sqrt"
, "cos", "cos"
, "sin", "sin"
, "tan", "tan"
, "atan", "atan"
, "Assigned", "!!"
// , "", ""
// known identifiers of custom functions/types/...
, "vec3f", "vec3f"
, "vec2f", "vec2f"
, "vec3i", "vec3i"
, "init3f", "vec3f"
, "init2f", "vec2f"
, "init3i", "vec3i"
, "StrToFloat", "atof"
, 00
};