Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: core/fxcrt/fx_basic_util.cpp

Issue 2419433004: Clean up fx_basic_util a little (Closed)
Patch Set: Comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | core/fxcrt/fx_stream.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fxcrt/fx_basic.h" 7 #include "core/fxcrt/fx_basic.h"
8 #include "core/fxcrt/fx_ext.h" 8 #include "core/fxcrt/fx_ext.h"
9 9
10 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
11 #include <dirent.h>
12 #include <sys/types.h>
13 #else
14 #include <direct.h>
15 #endif
16
17 #include <algorithm> 10 #include <algorithm>
18 #include <cctype> 11 #include <cctype>
19 #include <limits> 12 #include <limits>
20 #include <memory> 13 #include <memory>
21 14
22 namespace { 15 namespace {
23 16
24 const int kDefaultIntValue = 0; 17 const int kDefaultIntValue = 0;
25 18
26 } // namespace 19 } // namespace
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 71
79 int* pInt = static_cast<int*>(pData); 72 int* pInt = static_cast<int*>(pData);
80 *pInt = value; 73 *pInt = value;
81 return true; 74 return true;
82 } 75 }
83 76
84 static const FX_FLOAT fraction_scales[] = { 77 static const FX_FLOAT fraction_scales[] = {
85 0.1f, 0.01f, 0.001f, 0.0001f, 78 0.1f, 0.01f, 0.001f, 0.0001f,
86 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 79 0.00001f, 0.000001f, 0.0000001f, 0.00000001f,
87 0.000000001f, 0.0000000001f, 0.00000000001f}; 80 0.000000001f, 0.0000000001f, 0.00000000001f};
81
88 int FXSYS_FractionalScaleCount() { 82 int FXSYS_FractionalScaleCount() {
89 return FX_ArraySize(fraction_scales); 83 return FX_ArraySize(fraction_scales);
90 } 84 }
91 85
92 FX_FLOAT FXSYS_FractionalScale(size_t scale_factor, int value) { 86 FX_FLOAT FXSYS_FractionalScale(size_t scale_factor, int value) {
93 return fraction_scales[scale_factor] * value; 87 return fraction_scales[scale_factor] * value;
94 } 88 }
95 89
96 FX_FLOAT FX_atof(const CFX_ByteStringC& strc) { 90 FX_FLOAT FX_atof(const CFX_ByteStringC& strc) {
97 if (strc.IsEmpty()) 91 if (strc.IsEmpty())
98 return 0.0; 92 return 0.0;
99 93
100 int cc = 0; 94 int cc = 0;
101 bool bNegative = false; 95 bool bNegative = false;
102 int len = strc.GetLength(); 96 int len = strc.GetLength();
103 if (strc[0] == '+') { 97 if (strc[0] == '+') {
104 cc++; 98 cc++;
105 } else if (strc[0] == '-') { 99 } else if (strc[0] == '-') {
106 bNegative = TRUE; 100 bNegative = TRUE;
107 cc++; 101 cc++;
108 } 102 }
109 while (cc < len) { 103 while (cc < len) {
110 if (strc[cc] != '+' && strc[cc] != '-') { 104 if (strc[cc] != '+' && strc[cc] != '-')
111 break; 105 break;
112 }
113 cc++; 106 cc++;
114 } 107 }
115 FX_FLOAT value = 0; 108 FX_FLOAT value = 0;
116 while (cc < len) { 109 while (cc < len) {
117 if (strc[cc] == '.') { 110 if (strc[cc] == '.')
118 break; 111 break;
119 }
120 value = value * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); 112 value = value * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc));
121 cc++; 113 cc++;
122 } 114 }
123 int scale = 0; 115 int scale = 0;
124 if (cc < len && strc[cc] == '.') { 116 if (cc < len && strc[cc] == '.') {
125 cc++; 117 cc++;
126 while (cc < len) { 118 while (cc < len) {
127 value += 119 value +=
128 FXSYS_FractionalScale(scale, FXSYS_toDecimalDigit(strc.CharAt(cc))); 120 FXSYS_FractionalScale(scale, FXSYS_toDecimalDigit(strc.CharAt(cc)));
129 scale++; 121 scale++;
130 if (scale == FXSYS_FractionalScaleCount()) 122 if (scale == FXSYS_FractionalScaleCount())
131 break; 123 break;
132 cc++; 124 cc++;
133 } 125 }
134 } 126 }
135 return bNegative ? -value : value; 127 return bNegative ? -value : value;
136 } 128 }
137 129
138 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 130 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
139 void FXSYS_snprintf(char* str, 131 void FXSYS_snprintf(char* str,
140 size_t size, 132 size_t size,
141 _Printf_format_string_ const char* fmt, 133 _Printf_format_string_ const char* fmt,
142 ...) { 134 ...) {
143 va_list ap; 135 va_list ap;
144 va_start(ap, fmt); 136 va_start(ap, fmt);
145 FXSYS_vsnprintf(str, size, fmt, ap); 137 FXSYS_vsnprintf(str, size, fmt, ap);
146 va_end(ap); 138 va_end(ap);
147 } 139 }
140
148 void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap) { 141 void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap) {
149 (void)_vsnprintf(str, size, fmt, ap); 142 (void)_vsnprintf(str, size, fmt, ap);
150 if (size) { 143 if (size)
151 str[size - 1] = 0; 144 str[size - 1] = 0;
152 }
153 } 145 }
154 #endif // _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 146 #endif // _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
155 147
156 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 148 FX_FileHandle* FX_OpenFolder(const FX_CHAR* path) {
157 class CFindFileData {
158 public:
159 virtual ~CFindFileData() {}
160 HANDLE m_Handle;
161 FX_BOOL m_bEnd;
162 };
163
164 class CFindFileDataA : public CFindFileData {
165 public:
166 ~CFindFileDataA() override {}
167 WIN32_FIND_DATAA m_FindData;
168 };
169
170 class CFindFileDataW : public CFindFileData {
171 public:
172 ~CFindFileDataW() override {}
173 WIN32_FIND_DATAW m_FindData;
174 };
175 #endif
176
177 void* FX_OpenFolder(const FX_CHAR* path) {
178 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 149 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
179 std::unique_ptr<CFindFileDataA> pData(new CFindFileDataA); 150 std::unique_ptr<CFindFileDataA> pData(new CFindFileDataA);
180 pData->m_Handle = FindFirstFileExA((CFX_ByteString(path) + "/*.*").c_str(), 151 pData->m_Handle = FindFirstFileExA((CFX_ByteString(path) + "/*.*").c_str(),
181 FindExInfoStandard, &pData->m_FindData, 152 FindExInfoStandard, &pData->m_FindData,
182 FindExSearchNameMatch, nullptr, 0); 153 FindExSearchNameMatch, nullptr, 0);
183 if (pData->m_Handle == INVALID_HANDLE_VALUE) 154 if (pData->m_Handle == INVALID_HANDLE_VALUE)
184 return nullptr; 155 return nullptr;
185 156
186 pData->m_bEnd = FALSE; 157 pData->m_bEnd = false;
187 return pData.release(); 158 return pData.release();
188 #else 159 #else
189 DIR* dir = opendir(path); 160 return opendir(path);
190 return dir;
191 #endif 161 #endif
192 } 162 }
193 163
194 void* FX_OpenFolder(const FX_WCHAR* path) { 164 bool FX_GetNextFile(FX_FileHandle* handle,
165 CFX_ByteString* filename,
166 bool* bFolder) {
167 if (!handle)
168 return false;
169
195 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 170 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
196 std::unique_ptr<CFindFileDataW> pData(new CFindFileDataW); 171 if (handle->m_bEnd)
197 pData->m_Handle = FindFirstFileExW((CFX_WideString(path) + L"/*.*").c_str(), 172 return false;
198 FindExInfoStandard, &pData->m_FindData,
199 FindExSearchNameMatch, nullptr, 0);
200 if (pData->m_Handle == INVALID_HANDLE_VALUE)
201 return nullptr;
202 173
203 pData->m_bEnd = FALSE; 174 *filename = handle->m_FindData.cFileName;
204 return pData.release(); 175 *bFolder =
176 (handle->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
177 if (!FindNextFileA(handle->m_Handle, &handle->m_FindData))
178 handle->m_bEnd = true;
179 return true;
180 #elif defined(__native_client__)
181 abort();
182 return false;
205 #else 183 #else
206 DIR* dir = opendir(CFX_ByteString::FromUnicode(path).c_str()); 184 struct dirent* de = readdir(handle);
207 return dir; 185 if (!de)
186 return false;
187 *filename = de->d_name;
188 *bFolder = de->d_type == DT_DIR;
189 return true;
208 #endif 190 #endif
209 } 191 }
210 FX_BOOL FX_GetNextFile(void* handle, 192
211 CFX_ByteString& filename, 193 void FX_CloseFolder(FX_FileHandle* handle) {
212 FX_BOOL& bFolder) { 194 if (!handle)
213 if (!handle) { 195 return;
214 return FALSE; 196
215 }
216 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 197 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
217 CFindFileDataA* pData = (CFindFileDataA*)handle; 198 FindClose(handle->m_Handle);
218 if (pData->m_bEnd) 199 delete handle;
219 return FALSE;
220
221 filename = pData->m_FindData.cFileName;
222 bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
223 if (!FindNextFileA(pData->m_Handle, &pData->m_FindData))
224 pData->m_bEnd = TRUE;
225 return TRUE;
226 #elif defined(__native_client__)
227 abort();
228 return FALSE;
229 #else 200 #else
230 struct dirent* de = readdir((DIR*)handle); 201 closedir(handle);
231 if (!de) {
232 return FALSE;
233 }
234 filename = de->d_name;
235 bFolder = de->d_type == DT_DIR;
236 return TRUE;
237 #endif 202 #endif
238 } 203 }
239 FX_BOOL FX_GetNextFile(void* handle, 204
240 CFX_WideString& filename,
241 FX_BOOL& bFolder) {
242 if (!handle) {
243 return FALSE;
244 }
245 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
246 CFindFileDataW* pData = (CFindFileDataW*)handle;
247 if (pData->m_bEnd) {
248 return FALSE;
249 }
250 filename = pData->m_FindData.cFileName;
251 bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
252 if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) {
253 pData->m_bEnd = TRUE;
254 }
255 return TRUE;
256 #elif defined(__native_client__)
257 abort();
258 return FALSE;
259 #else
260 struct dirent* de = readdir((DIR*)handle);
261 if (!de) {
262 return FALSE;
263 }
264 filename = CFX_WideString::FromLocal(de->d_name);
265 bFolder = de->d_type == DT_DIR;
266 return TRUE;
267 #endif
268 }
269 void FX_CloseFolder(void* handle) {
270 if (!handle) {
271 return;
272 }
273 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
274 CFindFileData* pData = (CFindFileData*)handle;
275 FindClose(pData->m_Handle);
276 delete pData;
277 #else
278 closedir((DIR*)handle);
279 #endif
280 }
281 FX_WCHAR FX_GetFolderSeparator() { 205 FX_WCHAR FX_GetFolderSeparator() {
282 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 206 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
283 return '\\'; 207 return '\\';
284 #else 208 #else
285 return '/'; 209 return '/';
286 #endif 210 #endif
287 } 211 }
288 212
289 CFX_Matrix_3by3 CFX_Matrix_3by3::Inverse() { 213 CFX_Matrix_3by3 CFX_Matrix_3by3::Inverse() {
290 FX_FLOAT det = 214 FX_FLOAT det =
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 if (nbits < 8 && nbits + bitCount <= 8) { 247 if (nbits < 8 && nbits + bitCount <= 8) {
324 bitShift = 8 - nbits - bitCount; 248 bitShift = 8 - nbits - bitCount;
325 bitMask = (1 << nbits) - 1; 249 bitMask = (1 << nbits) - 1;
326 dstShift = 0; 250 dstShift = 0;
327 } else { 251 } else {
328 bitShift = 0; 252 bitShift = 0;
329 int bitOffset = 8 - bitCount; 253 int bitOffset = 8 - bitCount;
330 bitMask = (1 << std::min(bitOffset, nbits)) - 1; 254 bitMask = (1 << std::min(bitOffset, nbits)) - 1;
331 dstShift = nbits - bitOffset; 255 dstShift = nbits - bitOffset;
332 } 256 }
333 uint32_t result = (uint32_t)(*dataPtr++ >> bitShift & bitMask) << dstShift; 257 uint32_t result =
258 static_cast<uint32_t>((*dataPtr++ >> bitShift & bitMask) << dstShift);
334 while (dstShift >= 8) { 259 while (dstShift >= 8) {
335 dstShift -= 8; 260 dstShift -= 8;
336 result |= *dataPtr++ << dstShift; 261 result |= *dataPtr++ << dstShift;
337 } 262 }
338 if (dstShift > 0) { 263 if (dstShift > 0) {
339 bitShift = 8 - dstShift; 264 bitShift = 8 - dstShift;
340 bitMask = (1 << dstShift) - 1; 265 bitMask = (1 << dstShift) - 1;
341 result |= *dataPtr++ >> bitShift & bitMask; 266 result |= *dataPtr++ >> bitShift & bitMask;
342 } 267 }
343 return result; 268 return result;
344 } 269 }
OLDNEW
« no previous file with comments | « no previous file | core/fxcrt/fx_stream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698