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