aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/utils/StringUtil.cpp
blob: 8ca2ed8c07f6d6f59d7f54fa70120137aa7793f9 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#include "engines/wintermute/dcgf.h"
#include <algorithm>
#include <string>
#include <sstream>
#include "common/tokenizer.h"
#include "StringUtil.h"
#include "ConvertUTF.h"

namespace WinterMute {

//////////////////////////////////////////////////////////////////////////
void StringUtil::ToLowerCase(AnsiString &str) {
	std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}

//////////////////////////////////////////////////////////////////////////
/*void StringUtil::ToLowerCase(WideString &str) {
    std::transform(str.begin(), str.end(), str.begin(), ::towlower);
}*/

//////////////////////////////////////////////////////////////////////////
void StringUtil::ToUpperCase(AnsiString &str) {
	std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}

//////////////////////////////////////////////////////////////////////////
/*void StringUtil::ToUpperCase(WideString &str) {
    std::transform(str.begin(), str.end(), str.begin(), ::towupper);
}*/

//////////////////////////////////////////////////////////////////////////
bool StringUtil::CompareNoCase(const AnsiString &str1, const AnsiString &str2) {
	AnsiString str1lc = str1;
	AnsiString str2lc = str2;

	ToLowerCase(str1lc);
	ToLowerCase(str2lc);

	return (str1lc == str2lc);
}

//////////////////////////////////////////////////////////////////////////
/*bool StringUtil::CompareNoCase(const WideString &str1, const WideString &str2) {
    WideString str1lc = str1;
    WideString str2lc = str2;

    ToLowerCase(str1lc);
    ToLowerCase(str2lc);

    return (str1lc == str2lc);
}*/

//////////////////////////////////////////////////////////////////////////
WideString StringUtil::Utf8ToWide(const Utf8String &Utf8Str) {
	error("StringUtil::Utf8ToWide - WideString not supported yet");
	/*  size_t WideSize = Utf8Str.size();

	    if (sizeof(wchar_t) == 2) {
	        wchar_t *WideStringNative = new wchar_t[WideSize + 1];

	        const UTF8 *SourceStart = reinterpret_cast<const UTF8 *>(Utf8Str.c_str());
	        const UTF8 *SourceEnd = SourceStart + WideSize;

	        UTF16 *TargetStart = reinterpret_cast<UTF16 *>(WideStringNative);
	        UTF16 *TargetEnd = TargetStart + WideSize + 1;

	        ConversionResult res = ConvertUTF8toUTF16(&SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion);
	        if (res != conversionOK) {
	            delete [] WideStringNative;
	            return L"";
	        }
	        *TargetStart = 0;
	        WideString ResultString(WideStringNative);
	        delete [] WideStringNative;

	        return ResultString;
	    } else if (sizeof(wchar_t) == 4) {
	        wchar_t *WideStringNative = new wchar_t[WideSize + 1];

	        const UTF8 *SourceStart = reinterpret_cast<const UTF8 *>(Utf8Str.c_str());
	        const UTF8 *SourceEnd = SourceStart + WideSize;

	        UTF32 *TargetStart = reinterpret_cast<UTF32 *>(WideStringNative);
	        UTF32 *TargetEnd = TargetStart + WideSize;

	        ConversionResult res = ConvertUTF8toUTF32(&SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion);
	        if (res != conversionOK) {
	            delete [] WideStringNative;
	            return L"";
	        }
	        *TargetStart = 0;
	        WideString ResultString(WideStringNative);
	        delete [] WideStringNative;

	        return ResultString;
	    } else {
	        return L"";
	    }*/
	return "";
}

//////////////////////////////////////////////////////////////////////////
Utf8String StringUtil::WideToUtf8(const WideString &WideStr) {
	error("StringUtil::WideToUtf8 - Widestring not supported yet");
	/*  size_t WideSize = WideStr.length();

	    if (sizeof(wchar_t) == 2) {
	        size_t Utf8Size = 3 * WideSize + 1;
	        char *Utf8StringNative = new char[Utf8Size];

	        const UTF16 *SourceStart = reinterpret_cast<const UTF16 *>(WideStr.c_str());
	        const UTF16 *SourceEnd = SourceStart + WideSize;

	        UTF8 *TargetStart = reinterpret_cast<UTF8 *>(Utf8StringNative);
	        UTF8 *TargetEnd = TargetStart + Utf8Size;

	        ConversionResult res = ConvertUTF16toUTF8(&SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion);
	        if (res != conversionOK) {
	            delete [] Utf8StringNative;
	            return (Utf8String)"";
	        }
	        *TargetStart = 0;
	        Utf8String ResultString(Utf8StringNative);
	        delete [] Utf8StringNative;
	        return ResultString;
	    } else if (sizeof(wchar_t) == 4) {
	        size_t Utf8Size = 4 * WideSize + 1;
	        char *Utf8StringNative = new char[Utf8Size];

	        const UTF32 *SourceStart = reinterpret_cast<const UTF32 *>(WideStr.c_str());
	        const UTF32 *SourceEnd = SourceStart + WideSize;

	        UTF8 *TargetStart = reinterpret_cast<UTF8 *>(Utf8StringNative);
	        UTF8 *TargetEnd = TargetStart + Utf8Size;

	        ConversionResult res = ConvertUTF32toUTF8(&SourceStart, SourceEnd, &TargetStart, TargetEnd, strictConversion);
	        if (res != conversionOK) {
	            delete [] Utf8StringNative;
	            return (Utf8String)"";
	        }
	        *TargetStart = 0;
	        Utf8String ResultString(Utf8StringNative);
	        delete [] Utf8StringNative;
	        return ResultString;
	    } else {
	        return (Utf8String)"";
	    }*/
	return "";
}

//////////////////////////////////////////////////////////////////////////
WideString StringUtil::AnsiToWide(const AnsiString &str) {
	// using default os locale!
	warning("StringUtil::AnsiToWide - WideString not supported yet");
	/*  setlocale(LC_CTYPE, "");
	    size_t WideSize = mbstowcs(NULL, str.c_str(), 0) + 1;
	    wchar_t *wstr = new wchar_t[WideSize];
	    mbstowcs(wstr, str.c_str(), WideSize);
	    WideString ResultString(wstr);
	    delete [] wstr;
	    return ResultString;*/
	return WideString(str);
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::WideToAnsi(const WideString &wstr) {
	// using default os locale!
	warning("StringUtil::WideToAnsi - WideString not supported yet");
	/*  setlocale(LC_CTYPE, "");
	    size_t WideSize = wcstombs(NULL, wstr.c_str(), 0) + 1;
	    char *str = new char[WideSize];
	    wcstombs(str, wstr.c_str(), WideSize);
	    AnsiString ResultString(str);
	    delete [] str;
	    return ResultString;*/
	return AnsiString(wstr);
}

//////////////////////////////////////////////////////////////////////////
bool StringUtil::StartsWith(const AnsiString &str, const AnsiString &pattern, bool ignoreCase) {
	/*  size_t strLength = str.size();
	    size_t patternLength = pattern.size();

	    if (strLength < patternLength || patternLength == 0)
	        return false;

	    AnsiString startPart = str.substr(0, patternLength);

	    if (ignoreCase) return CompareNoCase(startPart, pattern);
	    else return (startPart == pattern);*/
	if (!ignoreCase)
		return str.hasPrefix(pattern);
	else {
		size_t strLength = str.size();
		size_t patternLength = pattern.size();

		if (strLength < patternLength || patternLength == 0)
			return false;

		AnsiString startPart(str.c_str(), patternLength);
		uint32 likeness = str.compareToIgnoreCase(pattern.c_str());
		return (likeness == 0);
	}
}

//////////////////////////////////////////////////////////////////////////
bool StringUtil::EndsWith(const AnsiString &str, const AnsiString &pattern, bool ignoreCase) {
	/*  size_t strLength = str.size(); // TODO: Remove
	    size_t patternLength = pattern.size();

	    if (strLength < patternLength || patternLength == 0)
	        return false;

	    AnsiString endPart = str.substr(strLength - patternLength, patternLength);

	    if (ignoreCase) return CompareNoCase(endPart, pattern);
	    else return (endPart == pattern);*/
	if (!ignoreCase) {
		return str.hasSuffix(pattern);
	} else {
		size_t strLength = str.size();
		size_t patternLength = pattern.size();

		if (strLength < patternLength || patternLength == 0)
			return false;

		Common::String endPart(str.c_str() + (strLength - patternLength), patternLength);
		uint32 likeness = str.compareToIgnoreCase(pattern.c_str());
		return (likeness != 0);
	}
}

//////////////////////////////////////////////////////////////////////////
bool StringUtil::IsUtf8BOM(const byte *Buffer, uint32 BufferSize) {
	if (BufferSize > 3 && Buffer[0] == 0xEF && Buffer[1] == 0xBB && Buffer[2] == 0xBF) return true;
	else return false;
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::Replace(const AnsiString &str, const AnsiString &from, const AnsiString &to) {
	if (from.empty() || from == to) return str;

	AnsiString result = str;
	/*size_t pos = 0;*/

	while (result.contains(from)) {
		const char *startPtr = strstr(result.c_str(), from.c_str());
		uint32 index = startPtr - result.c_str();

		Common::String tail(result.c_str() + index + to.size());
		result = Common::String(result.c_str(), index);
		result += to;
		result += tail;

		/*      pos = result.find(from, pos);
		        if (pos == result.npos) break;

		        result.replace(pos, from.size(), to);
		        pos += to.size();*/
	}

	return result;
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::Trim(const AnsiString &str, bool fromLeft, bool fromRight, const AnsiString &chars) {
	AnsiString trimmedStr = str;

	if (fromRight) {
		//trimmedStr.erase(trimmedStr.find_last_not_of(chars) + 1); // TODO
		warning("fromRight-trim not implemented yet, %s", chars.c_str());
	}
	if (fromLeft) {
		uint32 lastOf = LastIndexOf(str, chars, 0);
		trimmedStr = Common::String(trimmedStr.c_str() + lastOf);
		//trimmedStr.erase(0, trimmedStr.find_first_not_of(chars));
	}
	return trimmedStr;
}

//////////////////////////////////////////////////////////////////////////
int StringUtil::IndexOf(const WideString &str, const WideString &toFind, size_t startFrom) {
	/*size_t pos = str.find(toFind, startFrom);
	if (pos == str.npos) return -1;
	else return pos;*/
	const char *index = strstr(str.c_str(), toFind.c_str());
	if (index == NULL)
		return -1;
	else
		return index - str.c_str();
}

//////////////////////////////////////////////////////////////////////////
int StringUtil::LastIndexOf(const WideString &str, const WideString &toFind, size_t startFrom) {
	/*size_t pos = str.rfind(toFind, startFrom);
	if (pos == str.npos) return -1;
	else return pos;*/
	int32 lastIndex = -1;
	bool found = false;
	for (size_t i = startFrom; i < str.size(); i++) {
		found = false;
		for (size_t j = 0; j < toFind.size(); j++) {
			if (str[i + j] != toFind[j]) {
				found = false;
				break;
			} else {
				found = true;
			}
		}
		if (found)
			lastIndex = i;
	}
	return lastIndex;
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::ToString(size_t val) {
	/*  std::ostringstream str;
	    str << val;
	    return str.str();*/
	return Common::String::format("%u", (uint32)val);
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::ToString(int val) {
	/*  std::ostringstream str;
	    str << val;
	    return str.str();*/
	return Common::String::format("%d", val);
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::ToString(float val) {
	/*  std::ostringstream str;
	    str << val;
	    return str.str();*/
	return Common::String::format("%f", val);
}

//////////////////////////////////////////////////////////////////////////
AnsiString StringUtil::ToString(double val) {
	/*  std::ostringstream str;
	    str << val;
	    return str.str();*/
	return Common::String::format("%f", val);
}


//////////////////////////////////////////////////////////////////////////
void StringUtil::Split(const AnsiString &list, const AnsiString &delimiters, AnsiStringArray &result, bool keepEmptyItems) {
	result.clear();
//TODO: Verify this, wrt keepEmptyItems.
	Common::StringTokenizer tokenizer(list.c_str(), delimiters.c_str());
	//typedef boost::char_separator<char> separator_t;
	//typedef boost::tokenizer<separator_t, AnsiString::const_iterator, AnsiString> tokenizer_t;

	//separator_t del(delimiters.c_str(), "", keepEmptyItems ? boost::keep_empty_tokens : boost::drop_empty_tokens);
	//tokenizer_t tokens(list, del);
	while (!tokenizer.empty()) {
		Common::String copy(tokenizer.nextToken().c_str());
		result.push_back(copy);
	}
}

} // end of namespace WinterMute