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