1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef STRING_H
20 #define STRING_H
21
22 #include "exports.h"
23 #include "ie_types.h"
24
25 #include <cstring>
26 #include <cwctype>
27 #include <string>
28
29 #ifndef WIN32
30 # define stricmp strcasecmp
31 # define strnicmp strncasecmp
32 #endif
33
34 #define WHITESPACE_STRING L"\n\t\r "
35
36 namespace GemRB {
37
38 extern GEM_EXPORT unsigned char pl_uppercase[256];
39 extern GEM_EXPORT unsigned char pl_lowercase[256];
40
41 #ifdef HAS_WSTRING
42 typedef std::wstring String;
43 #else
44 typedef std::basic_string<wchar_t> String;
45 #endif
46
47 // String creators
48 GEM_EXPORT char* ConvertCharEncoding(const char * string, const char * from, const char* to);
49 GEM_EXPORT String* StringFromCString(const char* string);
50 GEM_EXPORT char* MBCStringFromString(const String& string);
51
52 // char manipulators
tolower(wchar_t c)53 inline wchar_t tolower(wchar_t c) { return ::towlower(c); }
toupper(wchar_t c)54 inline wchar_t toupper(wchar_t c) { return ::towupper(c); }
55
56 template <typename T>
ToLower(T c)57 GEM_EXPORT_T T ToLower(T c) {
58 if (c < 256) {
59 return pl_lowercase[static_cast<unsigned char>(c)];
60 } else {
61 return tolower(c);
62 }
63 }
64
65 template <typename T>
ToUpper(T c)66 GEM_EXPORT_T T ToUpper(T c) {
67 if (c < 256) {
68 return pl_uppercase[static_cast<unsigned char>(c)];
69 } else {
70 return towupper(c);
71 }
72 }
73
74 // String manipulators
75 template <typename T>
StringToLower(T & string)76 GEM_EXPORT_T void StringToLower(T& string) {
77 for (size_t i = 0; i < string.length(); i++) {
78 string[i] = ToLower(string[i]);
79 }
80 }
81
82 template <typename T>
StringToUpper(T & string)83 GEM_EXPORT_T void StringToUpper(T& string) {
84 for (size_t i = 0; i < string.length(); i++) {
85 string[i] = ToUpper(string[i]);
86 }
87 }
88
89 GEM_EXPORT void TrimString(String& string);
90
91 /* these functions will work with pl/cz special characters */
92 GEM_EXPORT void strnlwrcpy(char* d, const char *s, int l, bool pad = true);
93 GEM_EXPORT void strnuprcpy(char* d, const char *s, int l);
94 GEM_EXPORT void strnspccpy(char* d, const char *s, int l, bool upper = false);
95 GEM_EXPORT int strlench(const char* string, char ch);
96 }
97
98 #ifndef HAVE_STRNLEN
99 GEM_EXPORT int strnlen(const char* string, int maxlen);
100 #endif
101 #ifndef HAVE_STRLCPY
102 GEM_EXPORT size_t strlcpy(char *d, const char *s, size_t l);
103 #endif
104
105 #ifndef WIN32
106 GEM_EXPORT char* strlwr(char* string);
107 #endif
108
109 #endif
110