OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "../../include/fxcrt/fx_ext.h" | 9 #include "../../include/fxcrt/fx_ext.h" |
10 #include "../../include/fxcrt/fx_string.h" | 10 #include "../../include/fxcrt/fx_string.h" |
11 | 11 |
12 template <class T, class STR_T> | 12 template <class T, class STR_T> |
13 T FXSYS_StrToInt(STR_T str) | 13 T FXSYS_StrToInt(STR_T str) { |
14 { | 14 FX_BOOL neg = FALSE; |
15 FX_BOOL neg = FALSE; | 15 if (str == NULL) { |
16 if (str == NULL) { | 16 return 0; |
17 return 0; | 17 } |
| 18 if (*str == '-') { |
| 19 neg = TRUE; |
| 20 str++; |
| 21 } |
| 22 T num = 0; |
| 23 while (*str) { |
| 24 if ((*str) < '0' || (*str) > '9') { |
| 25 break; |
18 } | 26 } |
19 if (*str == '-') { | 27 if (num > (std::numeric_limits<T>::max() - 9) / 10) { |
20 neg = TRUE; | 28 break; |
21 str ++; | |
22 } | 29 } |
23 T num = 0; | 30 num = num * 10 + (*str) - '0'; |
24 while (*str) { | 31 str++; |
25 if ((*str) < '0' || (*str) > '9') { | 32 } |
26 break; | 33 return neg ? -num : num; |
27 } | |
28 if (num > (std::numeric_limits<T>::max() - 9) / 10) { | |
29 break; | |
30 } | |
31 num = num * 10 + (*str) - '0'; | |
32 str ++; | |
33 } | |
34 return neg ? -num : num; | |
35 } | 34 } |
36 template <typename T, typename STR_T> | 35 template <typename T, typename STR_T> |
37 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) | 36 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) { |
38 { | 37 int i = 0; |
39 int i = 0; | 38 if (value < 0) { |
40 if (value < 0) { | 39 string[i++] = '-'; |
41 string[i++] = '-'; | 40 value = -value; |
42 value = -value; | 41 } else if (value == 0) { |
43 } else if (value == 0) { | 42 string[0] = '0'; |
44 string[0] = '0'; | 43 string[1] = 0; |
45 string[1] = 0; | |
46 return string; | |
47 } | |
48 int digits = 1; | |
49 T order = value / 10; | |
50 while(order > 0) { | |
51 digits++; | |
52 order = order / 10; | |
53 } | |
54 for (int d = digits - 1; d > -1; d--) { | |
55 string[d + i] = "0123456789abcdef"[value % 10]; | |
56 value /= 10; | |
57 } | |
58 string[digits + i] = 0; | |
59 return string; | 44 return string; |
| 45 } |
| 46 int digits = 1; |
| 47 T order = value / 10; |
| 48 while (order > 0) { |
| 49 digits++; |
| 50 order = order / 10; |
| 51 } |
| 52 for (int d = digits - 1; d > -1; d--) { |
| 53 string[d + i] = "0123456789abcdef"[value % 10]; |
| 54 value /= 10; |
| 55 } |
| 56 string[digits + i] = 0; |
| 57 return string; |
60 } | 58 } |
61 #ifdef __cplusplus | 59 #ifdef __cplusplus |
62 extern "C" { | 60 extern "C" { |
63 #endif | 61 #endif |
64 int32_t FXSYS_atoi(const FX_CHAR* str) | 62 int32_t FXSYS_atoi(const FX_CHAR* str) { |
65 { | 63 return FXSYS_StrToInt<int32_t, const FX_CHAR*>(str); |
66 return FXSYS_StrToInt<int32_t, const FX_CHAR*>(str); | |
67 } | 64 } |
68 int32_t FXSYS_wtoi(const FX_WCHAR* str) | 65 int32_t FXSYS_wtoi(const FX_WCHAR* str) { |
69 { | 66 return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); |
70 return FXSYS_StrToInt<int32_t, const FX_WCHAR*>(str); | |
71 } | 67 } |
72 int64_t FXSYS_atoi64(const FX_CHAR* str) | 68 int64_t FXSYS_atoi64(const FX_CHAR* str) { |
73 { | 69 return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); |
74 return FXSYS_StrToInt<int64_t, const FX_CHAR*>(str); | |
75 } | 70 } |
76 int64_t FXSYS_wtoi64(const FX_WCHAR* str) | 71 int64_t FXSYS_wtoi64(const FX_WCHAR* str) { |
77 { | 72 return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); |
78 return FXSYS_StrToInt<int64_t, const FX_WCHAR*>(str); | |
79 } | 73 } |
80 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) | 74 const FX_CHAR* FXSYS_i64toa(int64_t value, FX_CHAR* str, int radix) { |
81 { | 75 return FXSYS_IntToStr<int64_t, FX_CHAR*>(value, str, radix); |
82 return FXSYS_IntToStr<int64_t, FX_CHAR*>(value, str, radix); | |
83 } | 76 } |
84 const FX_WCHAR* FXSYS_i64tow(int64_t value, FX_WCHAR* str, int radix) | 77 const FX_WCHAR* FXSYS_i64tow(int64_t value, FX_WCHAR* str, int radix) { |
85 { | 78 return FXSYS_IntToStr<int64_t, FX_WCHAR*>(value, str, radix); |
86 return FXSYS_IntToStr<int64_t, FX_WCHAR*>(value, str, radix); | |
87 } | 79 } |
88 #ifdef __cplusplus | 80 #ifdef __cplusplus |
89 } | 81 } |
90 #endif | 82 #endif |
91 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 83 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
92 #ifdef __cplusplus | 84 #ifdef __cplusplus |
93 extern "C" { | 85 extern "C" { |
94 #endif | 86 #endif |
95 int FXSYS_GetACP() | 87 int FXSYS_GetACP() { |
96 { | 88 return 0; |
97 return 0; | |
98 } | 89 } |
99 FX_DWORD FXSYS_GetFullPathName(const FX_CHAR* filename, FX_DWORD buflen, FX_CHAR
* buf, FX_CHAR** filepart) | 90 FX_DWORD FXSYS_GetFullPathName(const FX_CHAR* filename, |
100 { | 91 FX_DWORD buflen, |
101 int srclen = FXSYS_strlen(filename); | 92 FX_CHAR* buf, |
102 if (buf == NULL || (int)buflen < srclen + 1) { | 93 FX_CHAR** filepart) { |
103 return srclen + 1; | 94 int srclen = FXSYS_strlen(filename); |
104 } | 95 if (buf == NULL || (int)buflen < srclen + 1) { |
105 FXSYS_strcpy(buf, filename); | 96 return srclen + 1; |
106 return srclen; | 97 } |
| 98 FXSYS_strcpy(buf, filename); |
| 99 return srclen; |
107 } | 100 } |
108 FX_DWORD FXSYS_GetModuleFileName(void* hModule, char* buf, FX_DWORD bufsize) | 101 FX_DWORD FXSYS_GetModuleFileName(void* hModule, char* buf, FX_DWORD bufsize) { |
109 { | 102 return (FX_DWORD)-1; |
110 return (FX_DWORD) - 1; | |
111 } | 103 } |
112 #ifdef __cplusplus | 104 #ifdef __cplusplus |
113 } | 105 } |
114 #endif | 106 #endif |
115 #endif | 107 #endif |
116 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 108 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
117 #ifdef __cplusplus | 109 #ifdef __cplusplus |
118 extern "C" { | 110 extern "C" { |
119 #endif | 111 #endif |
120 FXSYS_FILE* FXSYS_wfopen(const FX_WCHAR* filename, const FX_WCHAR* mode) | 112 FXSYS_FILE* FXSYS_wfopen(const FX_WCHAR* filename, const FX_WCHAR* mode) { |
121 { | 113 return FXSYS_fopen(CFX_ByteString::FromUnicode(filename), |
122 return FXSYS_fopen(CFX_ByteString::FromUnicode(filename), CFX_ByteString::Fr
omUnicode(mode)); | 114 CFX_ByteString::FromUnicode(mode)); |
123 } | 115 } |
124 char* FXSYS_strlwr(char* str) | 116 char* FXSYS_strlwr(char* str) { |
125 { | 117 if (str == NULL) { |
126 if (str == NULL) { | 118 return NULL; |
127 return NULL; | 119 } |
| 120 char* s = str; |
| 121 while (*str) { |
| 122 *str = FXSYS_tolower(*str); |
| 123 str++; |
| 124 } |
| 125 return s; |
| 126 } |
| 127 char* FXSYS_strupr(char* str) { |
| 128 if (str == NULL) { |
| 129 return NULL; |
| 130 } |
| 131 char* s = str; |
| 132 while (*str) { |
| 133 *str = FXSYS_toupper(*str); |
| 134 str++; |
| 135 } |
| 136 return s; |
| 137 } |
| 138 FX_WCHAR* FXSYS_wcslwr(FX_WCHAR* str) { |
| 139 if (str == NULL) { |
| 140 return NULL; |
| 141 } |
| 142 FX_WCHAR* s = str; |
| 143 while (*str) { |
| 144 *str = FXSYS_tolower(*str); |
| 145 str++; |
| 146 } |
| 147 return s; |
| 148 } |
| 149 FX_WCHAR* FXSYS_wcsupr(FX_WCHAR* str) { |
| 150 if (str == NULL) { |
| 151 return NULL; |
| 152 } |
| 153 FX_WCHAR* s = str; |
| 154 while (*str) { |
| 155 *str = FXSYS_toupper(*str); |
| 156 str++; |
| 157 } |
| 158 return s; |
| 159 } |
| 160 int FXSYS_stricmp(const char* dst, const char* src) { |
| 161 int f, l; |
| 162 do { |
| 163 if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) { |
| 164 f -= ('A' - 'a'); |
128 } | 165 } |
129 char* s = str; | 166 if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) { |
130 while (*str) { | 167 l -= ('A' - 'a'); |
131 *str = FXSYS_tolower(*str); | |
132 str ++; | |
133 } | 168 } |
134 return s; | 169 } while (f && (f == l)); |
| 170 return (f - l); |
135 } | 171 } |
136 char* FXSYS_strupr(char* str) | 172 int FXSYS_wcsicmp(const FX_WCHAR* dst, const FX_WCHAR* src) { |
137 { | 173 FX_WCHAR f, l; |
138 if (str == NULL) { | 174 do { |
139 return NULL; | 175 if (((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z')) { |
| 176 f -= ('A' - 'a'); |
140 } | 177 } |
141 char* s = str; | 178 if (((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z')) { |
142 while (*str) { | 179 l -= ('A' - 'a'); |
143 *str = FXSYS_toupper(*str); | |
144 str ++; | |
145 } | 180 } |
146 return s; | 181 } while (f && (f == l)); |
| 182 return (f - l); |
147 } | 183 } |
148 FX_WCHAR* FXSYS_wcslwr(FX_WCHAR* str) | 184 char* FXSYS_itoa(int value, char* string, int radix) { |
149 { | 185 return FXSYS_IntToStr<int32_t, FX_CHAR*>(value, string, radix); |
150 if (str == NULL) { | |
151 return NULL; | |
152 } | |
153 FX_WCHAR* s = str; | |
154 while (*str) { | |
155 *str = FXSYS_tolower(*str); | |
156 str ++; | |
157 } | |
158 return s; | |
159 } | |
160 FX_WCHAR* FXSYS_wcsupr(FX_WCHAR* str) | |
161 { | |
162 if (str == NULL) { | |
163 return NULL; | |
164 } | |
165 FX_WCHAR* s = str; | |
166 while (*str) { | |
167 *str = FXSYS_toupper(*str); | |
168 str ++; | |
169 } | |
170 return s; | |
171 } | |
172 int FXSYS_stricmp(const char*dst, const char*src) | |
173 { | |
174 int f, l; | |
175 do { | |
176 if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') ) { | |
177 f -= ('A' - 'a'); | |
178 } | |
179 if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') ) { | |
180 l -= ('A' - 'a'); | |
181 } | |
182 } while ( f && (f == l) ); | |
183 return(f - l); | |
184 } | |
185 int FXSYS_wcsicmp(const FX_WCHAR *dst, const FX_WCHAR *src) | |
186 { | |
187 FX_WCHAR f, l; | |
188 do { | |
189 if ( ((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) { | |
190 f -= ('A' - 'a'); | |
191 } | |
192 if ( ((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z') ) { | |
193 l -= ('A' - 'a'); | |
194 } | |
195 } while ( f && (f == l) ); | |
196 return(f - l); | |
197 } | |
198 char* FXSYS_itoa(int value, char* string, int radix) | |
199 { | |
200 return FXSYS_IntToStr<int32_t, FX_CHAR*>(value, string, radix); | |
201 } | 186 } |
202 #ifdef __cplusplus | 187 #ifdef __cplusplus |
203 } | 188 } |
204 #endif | 189 #endif |
205 #endif | 190 #endif |
206 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ | 191 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ |
207 #ifdef __cplusplus | 192 #ifdef __cplusplus |
208 extern "C" { | 193 extern "C" { |
209 #endif | 194 #endif |
210 int FXSYS_WideCharToMultiByte(FX_DWORD codepage, FX_DWORD dwFlags, const FX_WCHA
R* wstr, int wlen, | 195 int FXSYS_WideCharToMultiByte(FX_DWORD codepage, |
211 FX_CHAR* buf, int buflen, const FX_CHAR* default_s
tr, int* pUseDefault) | 196 FX_DWORD dwFlags, |
212 { | 197 const FX_WCHAR* wstr, |
213 int len = 0; | 198 int wlen, |
214 for (int i = 0; i < wlen; i ++) { | 199 FX_CHAR* buf, |
215 if (wstr[i] < 0x100) { | 200 int buflen, |
216 if (buf && len < buflen) { | 201 const FX_CHAR* default_str, |
217 buf[len] = (FX_CHAR)wstr[i]; | 202 int* pUseDefault) { |
218 } | 203 int len = 0; |
219 len ++; | 204 for (int i = 0; i < wlen; i++) { |
220 } | 205 if (wstr[i] < 0x100) { |
| 206 if (buf && len < buflen) { |
| 207 buf[len] = (FX_CHAR)wstr[i]; |
| 208 } |
| 209 len++; |
221 } | 210 } |
222 return len; | 211 } |
| 212 return len; |
223 } | 213 } |
224 int FXSYS_MultiByteToWideChar(FX_DWORD codepage, FX_DWORD dwFlags, const FX_CHAR
* bstr, int blen, | 214 int FXSYS_MultiByteToWideChar(FX_DWORD codepage, |
225 FX_WCHAR* buf, int buflen) | 215 FX_DWORD dwFlags, |
226 { | 216 const FX_CHAR* bstr, |
227 int wlen = 0; | 217 int blen, |
228 for (int i = 0; i < blen; i ++) { | 218 FX_WCHAR* buf, |
229 if (buf && wlen < buflen) { | 219 int buflen) { |
230 buf[wlen] = bstr[i]; | 220 int wlen = 0; |
231 } | 221 for (int i = 0; i < blen; i++) { |
232 wlen ++; | 222 if (buf && wlen < buflen) { |
| 223 buf[wlen] = bstr[i]; |
233 } | 224 } |
234 return wlen; | 225 wlen++; |
| 226 } |
| 227 return wlen; |
235 } | 228 } |
236 #ifdef __cplusplus | 229 #ifdef __cplusplus |
237 } | 230 } |
238 #endif | 231 #endif |
239 #endif | 232 #endif |
OLD | NEW |