summaryrefslogtreecommitdiff
path: root/core/src/fxcrt/fx_basic_gcc.cpp
blob: 93c71ce66028fd4e6b25cc73f55f529df91727b2 (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
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include <limits>
#include "../../include/fxcrt/fx_ext.h"
template <class T, class STR_T>
T FXSYS_StrToInt(STR_T str)
{
    FX_BOOL neg = FALSE;
    if (str == NULL) {
        return 0;
    }
    if (*str == '-') {
        neg = TRUE;
        str ++;
    }
    T num = 0;
    while (*str) {
        if ((*str) < '0' || (*str) > '9') {
            break;
        }
        if (num > (std::numeric_limits<T>::max() - 9) / 10) {
            break;
        }
        num = num * 10 + (*str) - '0';
        str ++;
    }
    return neg ? -num : num;
}
template <typename T, typename STR_T>
STR_T FXSYS_IntToStr(T value, STR_T string, int radix)
{
    int i = 0;
    if (value < 0) {
        string[i++] = '-';
        value = -value;
    } else if (value == 0) {
        string[0] = '0';
        string[1] = 0;
        return string;
    }
    int digits = 1;
    T order = value / 10;
    while(order > 0) {
        digits++;
        order = order / 10;
    }
    for (int d = digits - 1; d > -1; d--) {
        string[d + i] = "0123456789abcdef"[value % 10];
        value /= 10;
    }
    string[digits + i] = 0;
    return string;
}
#ifdef __cplusplus
extern "C" {
#endif
FX_INT32 FXSYS_atoi(FX_LPCSTR str)
{
    return FXSYS_StrToInt<FX_INT32, FX_LPCSTR>(str);
}
FX_INT32 FXSYS_wtoi(FX_LPCWSTR str)
{
    return FXSYS_StrToInt<FX_INT32, FX_LPCWSTR>(str);
}
FX_INT64 FXSYS_atoi64(FX_LPCSTR str)
{
    return FXSYS_StrToInt<FX_INT64, FX_LPCSTR>(str);
}
FX_INT64 FXSYS_wtoi64(FX_LPCWSTR str)
{
    return FXSYS_StrToInt<FX_INT64, FX_LPCWSTR>(str);
}
FX_LPCSTR FXSYS_i64toa(FX_INT64 value, FX_LPSTR str, int radix)
{
    return FXSYS_IntToStr<FX_INT64, FX_LPSTR>(value, str, radix);
}
FX_LPCWSTR FXSYS_i64tow(FX_INT64 value, FX_LPWSTR str, int radix)
{
    return FXSYS_IntToStr<FX_INT64, FX_LPWSTR>(value, str, radix);
}
#ifdef __cplusplus
}
#endif
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
#ifdef __cplusplus
extern "C" {
#endif
int FXSYS_GetACP()
{
    return 0;
}
FX_DWORD FXSYS_GetFullPathName(FX_LPCSTR filename, FX_DWORD buflen, FX_LPSTR buf, FX_LPSTR* filepart)
{
    int srclen = FXSYS_strlen(filename);
    if (buf == NULL || (int)buflen < srclen + 1) {
        return srclen + 1;
    }
    FXSYS_strcpy(buf, filename);
    return srclen;
}
FX_DWORD FXSYS_GetModuleFileName(FX_LPVOID hModule, char* buf, FX_DWORD bufsize)
{
    return (FX_DWORD) - 1;
}
#ifdef __cplusplus
}
#endif
#endif
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
#ifdef __cplusplus
extern "C" {
#endif
FXSYS_FILE* FXSYS_wfopen(FX_LPCWSTR filename, FX_LPCWSTR mode)
{
    return FXSYS_fopen(CFX_ByteString::FromUnicode(filename), CFX_ByteString::FromUnicode(mode));
}
char* FXSYS_strlwr(char* str)
{
    if (str == NULL) {
        return NULL;
    }
    char* s = str;
    while (*str) {
        *str = FXSYS_tolower(*str);
        str ++;
    }
    return s;
}
char* FXSYS_strupr(char* str)
{
    if (str == NULL) {
        return NULL;
    }
    char* s = str;
    while (*str) {
        *str = FXSYS_toupper(*str);
        str ++;
    }
    return s;
}
FX_WCHAR* FXSYS_wcslwr(FX_WCHAR* str)
{
    if (str == NULL) {
        return NULL;
    }
    FX_WCHAR* s = str;
    while (*str) {
        *str = FXSYS_tolower(*str);
        str ++;
    }
    return s;
}
FX_WCHAR* FXSYS_wcsupr(FX_WCHAR* str)
{
    if (str == NULL) {
        return NULL;
    }
    FX_WCHAR* s = str;
    while (*str) {
        *str = FXSYS_toupper(*str);
        str ++;
    }
    return s;
}
int FXSYS_stricmp(const char*dst, const char*src)
{
    int f, l;
    do {
        if ( ((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z') ) {
            f -= ('A' - 'a');
        }
        if ( ((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z') ) {
            l -= ('A' - 'a');
        }
    } while ( f && (f == l) );
    return(f - l);
}
int FXSYS_wcsicmp(const FX_WCHAR *dst, const FX_WCHAR *src)
{
    FX_WCHAR f, l;
    do {
        if ( ((f = (FX_WCHAR)(*(dst++))) >= 'A') && (f <= 'Z') ) {
            f -= ('A' - 'a');
        }
        if ( ((l = (FX_WCHAR)(*(src++))) >= 'A') && (l <= 'Z') ) {
            l -= ('A' - 'a');
        }
    } while ( f && (f == l) );
    return(f - l);
}
char* FXSYS_itoa(int value, char* string, int radix)
{
    return FXSYS_IntToStr<FX_INT32, FX_LPSTR>(value, string, radix);
}
#ifdef __cplusplus
}
#endif
#endif
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
#ifdef __cplusplus
extern "C" {
#endif
int 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)
{
    int len = 0;
    for (int i = 0; i < wlen; i ++) {
        if (wstr[i] < 0x100) {
            if (buf && len < buflen) {
                buf[len] = (FX_CHAR)wstr[i];
            }
            len ++;
        }
    }
    return len;
}
int FXSYS_MultiByteToWideChar(FX_DWORD codepage, FX_DWORD dwFlags, FX_LPCSTR bstr, int blen,
                              FX_LPWSTR buf, int buflen)
{
    int wlen = 0;
    for (int i = 0; i < blen; i ++) {
        if (buf && wlen < buflen) {
            buf[wlen] = bstr[i];
        }
        wlen ++;
    }
    return wlen;
}
#ifdef __cplusplus
}
#endif
#endif