Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_system.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 "core/fxcrt/fx_system.h"
8
9#include <math.h>
10
11#include <limits>
12
13#include "build/build_config.h"
14#include "core/fxcrt/fx_extension.h"
15
16namespace {
17
18#if !BUILDFLAG(IS_WIN)
19uint32_t g_last_error = 0;
20#endif
21
22template <typename IntType, typename CharType>
23IntType FXSYS_StrToInt(const CharType* str) {
24 if (!str)
25 return 0;
26
27 // Process the sign.
28 bool neg = *str == '-';
29 if (neg || *str == '+')
30 str++;
31
32 IntType num = 0;
33 while (*str && FXSYS_IsDecimalDigit(*str)) {
34 IntType val = FXSYS_DecimalCharToInt(*str);
35 if (num > (std::numeric_limits<IntType>::max() - val) / 10) {
36 if (neg && std::numeric_limits<IntType>::is_signed) {
37 // Return MIN when the represented number is signed type and is smaller
38 // than the min value.
39 return std::numeric_limits<IntType>::min();
40 } else {
41 // Return MAX when the represented number is signed type and is larger
42 // than the max value, or the number is unsigned type and out of range.
43 return std::numeric_limits<IntType>::max();
44 }
45 }
46
47 num = num * 10 + val;
48 str++;
49 }
50 // When it is a negative value, -num should be returned. Since num may be of
51 // unsigned type, use ~num + 1 to avoid the warning of applying unary minus
52 // operator to unsigned type.
53 return neg ? ~num + 1 : num;
54}
55
56template <typename T, typename UT, typename STR_T>
57STR_T FXSYS_IntToStr(T value, STR_T str, int radix) {
58 if (radix < 2 || radix > 16) {
59 str[0] = 0;
60 return str;
61 }
62 if (value == 0) {
63 str[0] = '0';
64 str[1] = 0;
65 return str;
66 }
67 int i = 0;
68 UT uvalue;
69 if (value < 0) {
70 str[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 str[d + i] = "0123456789abcdef"[uvalue % radix];
84 uvalue /= radix;
85 }
86 str[digits + i] = 0;
87 return str;
88}
89
90} // namespace
91
92int FXSYS_roundf(float f) {
93 if (isnan(f))
94 return 0;
95 if (f < static_cast<float>(std::numeric_limits<int>::min()))
96 return std::numeric_limits<int>::min();
97 if (f >= static_cast<float>(std::numeric_limits<int>::max()))
98 return std::numeric_limits<int>::max();
99 return static_cast<int>(round(f));
100}
101
102int FXSYS_round(double d) {
103 if (isnan(d))
104 return 0;
105 if (d < static_cast<double>(std::numeric_limits<int>::min()))
106 return std::numeric_limits<int>::min();
107 if (d >= static_cast<double>(std::numeric_limits<int>::max()))
108 return std::numeric_limits<int>::max();
109 return static_cast<int>(round(d));
110}
111
112int32_t FXSYS_atoi(const char* str) {
113 return FXSYS_StrToInt<int32_t, char>(str);
114}
115uint32_t FXSYS_atoui(const char* str) {
116 return FXSYS_StrToInt<uint32_t>(str);
117}
118int32_t FXSYS_wtoi(const wchar_t* str) {
119 return FXSYS_StrToInt<int32_t, wchar_t>(str);
120}
121int64_t FXSYS_atoi64(const char* str) {
122 return FXSYS_StrToInt<int64_t, char>(str);
123}
124const char* FXSYS_i64toa(int64_t value, char* str, int radix) {
125 return FXSYS_IntToStr<int64_t, uint64_t, char*>(value, str, radix);
126}
127
128#if BUILDFLAG(IS_WIN)
129
130size_t FXSYS_wcsftime(wchar_t* strDest,
131 size_t maxsize,
132 const wchar_t* format,
133 const struct tm* timeptr) {
134 // Avoid tripping an invalid parameter handler and crashing process.
135 // Note: leap seconds may cause tm_sec == 60.
136 if (timeptr->tm_year < -1900 || timeptr->tm_year > 8099 ||
137 timeptr->tm_mon < 0 || timeptr->tm_mon > 11 || timeptr->tm_mday < 1 ||
138 timeptr->tm_mday > 31 || timeptr->tm_hour < 0 || timeptr->tm_hour > 23 ||
139 timeptr->tm_min < 0 || timeptr->tm_min > 59 || timeptr->tm_sec < 0 ||
140 timeptr->tm_sec > 60 || timeptr->tm_wday < 0 || timeptr->tm_wday > 6 ||
141 timeptr->tm_yday < 0 || timeptr->tm_yday > 365) {
142 strDest[0] = L'\0';
143 return 0;
144 }
145 return wcsftime(strDest, maxsize, format, timeptr);
146}
147
148#else // BUILDFLAG(IS_WIN)
149
150char* FXSYS_strlwr(char* str) {
151 if (!str) {
152 return nullptr;
153 }
154 char* s = str;
155 while (*str) {
156 *str = tolower(*str);
157 str++;
158 }
159 return s;
160}
161char* FXSYS_strupr(char* str) {
162 if (!str) {
163 return nullptr;
164 }
165 char* s = str;
166 while (*str) {
167 *str = toupper(*str);
168 str++;
169 }
170 return s;
171}
172wchar_t* FXSYS_wcslwr(wchar_t* str) {
173 if (!str) {
174 return nullptr;
175 }
176 wchar_t* s = str;
177 while (*str) {
178 *str = FXSYS_towlower(*str);
179 str++;
180 }
181 return s;
182}
183wchar_t* FXSYS_wcsupr(wchar_t* str) {
184 if (!str) {
185 return nullptr;
186 }
187 wchar_t* s = str;
188 while (*str) {
189 *str = FXSYS_towupper(*str);
190 str++;
191 }
192 return s;
193}
194
195int FXSYS_stricmp(const char* str1, const char* str2) {
196 int f;
197 int l;
198 do {
199 f = toupper(*str1);
200 l = toupper(*str2);
201 ++str1;
202 ++str2;
203 } while (f && f == l);
204 return f - l;
205}
206
207int FXSYS_wcsicmp(const wchar_t* str1, const wchar_t* str2) {
208 wchar_t f;
209 wchar_t l;
210 do {
211 f = FXSYS_towupper(*str1);
212 l = FXSYS_towupper(*str2);
213 ++str1;
214 ++str2;
215 } while (f && f == l);
216 return f - l;
217}
218
219char* FXSYS_itoa(int value, char* str, int radix) {
220 return FXSYS_IntToStr<int32_t, uint32_t, char*>(value, str, radix);
221}
222
223void FXSYS_SetLastError(uint32_t err) {
224 g_last_error = err;
225}
226
228 return g_last_error;
229}
230#endif // BUILDFLAG(IS_WIN)
231
232float FXSYS_sqrt2(float a, float b) {
233 return sqrtf(a * a + b * b);
234}
int32_t FXSYS_towupper(wchar_t c)
int32_t FXSYS_towlower(wchar_t c)
int32_t FXSYS_atoi(const char *str)
char * FXSYS_strlwr(char *str)
wchar_t * FXSYS_wcsupr(wchar_t *str)
char * FXSYS_strupr(char *str)
uint32_t FXSYS_atoui(const char *str)
int FXSYS_wcsicmp(const wchar_t *str1, const wchar_t *str2)
wchar_t * FXSYS_wcslwr(wchar_t *str)
int32_t FXSYS_wtoi(const wchar_t *str)
uint32_t FXSYS_GetLastError()
const char * FXSYS_i64toa(int64_t value, char *str, int radix)
int FXSYS_stricmp(const char *str1, const char *str2)
int64_t FXSYS_atoi64(const char *str)
int FXSYS_roundf(float f)
Definition fx_system.cpp:92
int FXSYS_round(double d)
char * FXSYS_itoa(int value, char *str, int radix)
void FXSYS_SetLastError(uint32_t err)
float FXSYS_sqrt2(float a, float b)