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