summaryrefslogtreecommitdiff
path: root/core/src/fxcrt/fx_basic_util.cpp
blob: db1a5b4b2ae7c925a3b0dde5d4c22a5f4faab034 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// 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 "../../include/fxcrt/fx_basic.h"
#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
#include <sys/types.h>
#include <dirent.h>
#else
#include <direct.h>
#endif
CFX_PrivateData::~CFX_PrivateData()
{
    ClearAll();
}
void FX_PRIVATEDATA::FreeData()
{
    if (m_pData == NULL) {
        return;
    }
    if (m_bSelfDestruct) {
        delete (CFX_DestructObject*)m_pData;
    } else if (m_pCallback) {
        m_pCallback(m_pData);
    }
}
void CFX_PrivateData::AddData(void* pModuleId, void* pData, PD_CALLBACK_FREEDATA callback, FX_BOOL bSelfDestruct)
{
    if (pModuleId == NULL) {
        return;
    }
    FX_PRIVATEDATA* pList = m_DataList.GetData();
    int count = m_DataList.GetSize();
    for (int i = 0; i < count; i ++) {
        if (pList[i].m_pModuleId == pModuleId) {
            pList[i].FreeData();
            pList[i].m_pData = pData;
            pList[i].m_pCallback = callback;
            return;
        }
    }
    FX_PRIVATEDATA data = {pModuleId, pData, callback, bSelfDestruct};
    m_DataList.Add(data);
}
void CFX_PrivateData::SetPrivateData(void* pModuleId, void* pData, PD_CALLBACK_FREEDATA callback)
{
    AddData(pModuleId, pData, callback, FALSE);
}
void CFX_PrivateData::SetPrivateObj(void* pModuleId, CFX_DestructObject* pObj)
{
    AddData(pModuleId, pObj, NULL, TRUE);
}
FX_BOOL CFX_PrivateData::RemovePrivateData(void* pModuleId)
{
    if (pModuleId == NULL) {
        return FALSE;
    }
    FX_PRIVATEDATA* pList = m_DataList.GetData();
    int count = m_DataList.GetSize();
    for (int i = 0; i < count; i ++) {
        if (pList[i].m_pModuleId == pModuleId) {
            m_DataList.RemoveAt(i);
            return TRUE;
        }
    }
    return FALSE;
}
void* CFX_PrivateData::GetPrivateData(void* pModuleId)
{
    if (pModuleId == NULL) {
        return NULL;
    }
    FX_PRIVATEDATA* pList = m_DataList.GetData();
    int count = m_DataList.GetSize();
    for (int i = 0; i < count; i ++) {
        if (pList[i].m_pModuleId == pModuleId) {
            return pList[i].m_pData;
        }
    }
    return NULL;
}
void CFX_PrivateData::ClearAll()
{
    FX_PRIVATEDATA* pList = m_DataList.GetData();
    int count = m_DataList.GetSize();
    for (int i = 0; i < count; i ++) {
        pList[i].FreeData();
    }
    m_DataList.RemoveAll();
}
void FX_atonum(FX_BSTR strc, FX_BOOL& bInteger, void* pData)
{
    if (FXSYS_memchr(strc.GetPtr(), '.', strc.GetLength()) == NULL) {
        bInteger = TRUE;
        int cc = 0, integer = 0;
        const FX_CHAR* str = strc.GetCStr();
        int len = strc.GetLength();
        FX_BOOL bNegative = FALSE;
        if (str[0] == '+') {
            cc++;
        } else if (str[0] == '-') {
            bNegative = TRUE;
            cc++;
        }
        while (cc < len) {
            if (str[cc] < '0' || str[cc] > '9') {
                break;
            }
            integer = integer * 10 + str[cc] - '0';
            if (integer < 0) {
                break;
            }
            cc ++;
        }
        if (bNegative) {
            integer = -integer;
        }
        *(int*)pData = integer;
    } else {
        bInteger = FALSE;
        *(FX_FLOAT*)pData = FX_atof(strc);
    }
}
FX_FLOAT FX_atof(FX_BSTR strc)
{
    if (strc.GetLength() == 0) {
        return 0.0;
    }
    int cc = 0;
    FX_BOOL bNegative = FALSE;
    const FX_CHAR* str = strc.GetCStr();
    int len = strc.GetLength();
    if (str[0] == '+') {
        cc++;
    } else if (str[0] == '-') {
        bNegative = TRUE;
        cc++;
    }
    while (cc < len) {
        if (str[cc] != '+' && str[cc] != '-') {
            break;
        }
        cc ++;
    }
    FX_FLOAT value = 0;
    while (cc < len) {
        if (str[cc] == '.') {
            break;
        }
        value = value * 10 + str[cc] - '0';
        cc ++;
    }
    static const FX_FLOAT fraction_scales[] = {0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f,
                                               0.0000001f, 0.00000001f, 0.000000001f, 0.0000000001f, 0.00000000001f
                                              };
    int scale = 0;
    if (cc < len && str[cc] == '.') {
        cc ++;
        while (cc < len) {
            value += fraction_scales[scale] * (str[cc] - '0');
            scale ++;
            if (scale == sizeof fraction_scales / sizeof(FX_FLOAT)) {
                break;
            }
            cc ++;
        }
    }
    return bNegative ? -value : value;
}

#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900
void FXSYS_snprintf(char *str, size_t size, _Printf_format_string_ const char* fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    FXSYS_vsnprintf(str, size, fmt, ap);
    va_end(ap);
}
void FXSYS_vsnprintf(char *str, size_t size, const char* fmt, va_list ap)
{
    (void) _vsnprintf(str, size, fmt, ap);
    if (size) {
        str[size - 1] = 0;
    }
}
#endif  // _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900

static FX_BOOL FX_IsDigit(uint8_t ch)
{
    return (ch >= '0' && ch <= '9') ? TRUE : FALSE;
}
static FX_BOOL FX_IsXDigit(uint8_t ch)
{
    return (FX_IsDigit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) ? TRUE : FALSE;
}
static uint8_t FX_MakeUpper(uint8_t ch)
{
    if (ch < 'a' || ch > 'z') {
        return ch;
    }
    return ch - 32;
}
static int FX_HexToI(uint8_t ch)
{
    ch = FX_MakeUpper(ch);
    return FX_IsDigit(ch) ? (ch - '0') : (ch - 55);
}
static const unsigned char url_encodeTable[128] = {
    1,  1,  1,  1,		1,  1,  1,  1,
    1,  1,  1,  1,		1,  1,  1,  1,
    1,  1,  1,  1,		1,  1,  1,  1,
    1,  1,  1,  1,		1,  1,  1,  1,
    1,  0,  1,  1,		0,  1,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		1,  0,  1,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  1,		1,  1,  1,  0,
    1,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  0,		0,  0,  0,  0,
    0,  0,  0,  1,		1,  1,  1,  1,
};
CFX_ByteString FX_UrlEncode(const CFX_WideString& wsUrl)
{
    const char arDigits[] = "0123456789ABCDEF";
    CFX_ByteString rUrl;
    int nLength = wsUrl.GetLength();
    for (int i = 0; i < nLength; i++) {
        FX_DWORD word = wsUrl.GetAt(i);
        if (word > 0x7F || url_encodeTable[word] == 1) {
            CFX_ByteString bsUri = CFX_ByteString::FromUnicode((FX_WORD)word);
            int nByte = bsUri.GetLength();
            for (int j = 0; j < nByte; j++) {
                rUrl += '%';
                uint8_t code = bsUri.GetAt(j);
                rUrl += arDigits[code >> 4];
                rUrl += arDigits[code & 0x0F];
            }
        } else {
            rUrl += CFX_ByteString::FromUnicode((FX_WORD)word);
        }
    }
    return rUrl;
}
CFX_WideString FX_UrlDecode(const CFX_ByteString& bsUrl)
{
    CFX_ByteString rUrl;
    int nLength = bsUrl.GetLength();
    for (int i = 0; i < nLength; i++) {
        if (i < nLength - 2 && bsUrl[i] == '%' && FX_IsXDigit(bsUrl[i + 1]) && FX_IsXDigit(bsUrl[i + 2])) {
            rUrl += (FX_HexToI(bsUrl[i + 1]) << 4 | FX_HexToI(bsUrl[i + 2]));
            i += 2;
        } else {
            rUrl += bsUrl[i];
        }
    }
    return CFX_WideString::FromLocal(rUrl);
}
CFX_ByteString FX_EncodeURI(const CFX_WideString& wsURI)
{
    const char arDigits[] = "0123456789ABCDEF";
    CFX_ByteString rURI;
    CFX_ByteString bsUri = wsURI.UTF8Encode();
    int nLength = bsUri.GetLength();
    for (int i = 0; i < nLength; i++) {
        uint8_t code = bsUri.GetAt(i);
        if (code > 0x7F || url_encodeTable[code] == 1) {
            rURI += '%';
            rURI += arDigits[code >> 4];
            rURI += arDigits[code & 0x0F];
        } else {
            rURI += code;
        }
    }
    return rURI;
}
CFX_WideString FX_DecodeURI(const CFX_ByteString& bsURI)
{
    CFX_ByteString rURI;
    int nLength = bsURI.GetLength();
    for (int i = 0; i < nLength; i++) {
        if (i < nLength - 2 && bsURI[i] == '%' && FX_IsXDigit(bsURI[i + 1]) && FX_IsXDigit(bsURI[i + 2])) {
            rURI += (FX_HexToI(bsURI[i + 1]) << 4 | FX_HexToI(bsURI[i + 2]));
            i += 2;
        } else {
            rURI += bsURI[i];
        }
    }
    return CFX_WideString::FromUTF8(rURI, rURI.GetLength());
}
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
class CFindFileData 
{
public:
    virtual ~CFindFileData() {}
    HANDLE				m_Handle;
    FX_BOOL				m_bEnd;
};
class CFindFileDataA : public CFindFileData
{
public:
    virtual ~CFindFileDataA() {}
    WIN32_FIND_DATAA	m_FindData;
};
class CFindFileDataW : public CFindFileData
{
public:
    virtual ~CFindFileDataW() {}
    WIN32_FIND_DATAW	m_FindData;
};
#endif
void* FX_OpenFolder(const FX_CHAR* path)
{
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
#ifndef _WIN32_WCE
    CFindFileDataA* pData = new CFindFileDataA;
#ifdef _FX_WINAPI_PARTITION_DESKTOP_
    pData->m_Handle = FindFirstFileA(CFX_ByteString(path) + "/*.*", &pData->m_FindData);
#else
    pData->m_Handle = FindFirstFileExA(CFX_ByteString(path) + "/*.*", FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0);
#endif
#else
    CFindFileDataW* pData = new CFindFileDataW;
    pData->m_Handle = FindFirstFileW(CFX_WideString::FromLocal(path) + L"/*.*", &pData->m_FindData);
#endif
    if (pData->m_Handle == INVALID_HANDLE_VALUE) {
        delete pData;
        return NULL;
    }
    pData->m_bEnd = FALSE;
    return pData;
#else
    DIR* dir = opendir(path);
    return dir;
#endif
}
void* FX_OpenFolder(const FX_WCHAR* path)
{
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    CFindFileDataW* pData = new CFindFileDataW;
#ifdef _FX_WINAPI_PARTITION_DESKTOP_
    pData->m_Handle = FindFirstFileW((CFX_WideString(path) + L"/*.*").c_str(), &pData->m_FindData);
#else
    pData->m_Handle = FindFirstFileExW((CFX_WideString(path) + L"/*.*").c_str(), FindExInfoStandard, &pData->m_FindData, FindExSearchNameMatch, NULL, 0);
#endif
    if (pData->m_Handle == INVALID_HANDLE_VALUE) {
        delete pData;
        return NULL;
    }
    pData->m_bEnd = FALSE;
    return pData;
#else
    DIR* dir = opendir(CFX_ByteString::FromUnicode(path));
    return dir;
#endif
}
FX_BOOL FX_GetNextFile(void* handle, CFX_ByteString& filename, FX_BOOL& bFolder)
{
    if (handle == NULL) {
        return FALSE;
    }
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
#ifndef _WIN32_WCE
    CFindFileDataA* pData = (CFindFileDataA*)handle;
    if (pData->m_bEnd) {
        return FALSE;
    }
    filename = pData->m_FindData.cFileName;
    bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    if (!FindNextFileA(pData->m_Handle, &pData->m_FindData)) {
        pData->m_bEnd = TRUE;
    }
    return TRUE;
#else
    CFindFileDataW* pData = (CFindFileDataW*)handle;
    if (pData->m_bEnd) {
        return FALSE;
    }
    filename = CFX_ByteString::FromUnicode(pData->m_FindData.cFileName);
    bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) {
        pData->m_bEnd = TRUE;
    }
    return TRUE;
#endif
#elif defined(__native_client__)
    abort();
    return FALSE;
#else
    struct dirent *de = readdir((DIR*)handle);
    if (de == NULL) {
        return FALSE;
    }
    filename = de->d_name;
    bFolder = de->d_type == DT_DIR;
    return TRUE;
#endif
}
FX_BOOL FX_GetNextFile(void* handle, CFX_WideString& filename, FX_BOOL& bFolder)
{
    if (handle == NULL) {
        return FALSE;
    }
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    CFindFileDataW* pData = (CFindFileDataW*)handle;
    if (pData->m_bEnd) {
        return FALSE;
    }
    filename = pData->m_FindData.cFileName;
    bFolder = pData->m_FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
    if (!FindNextFileW(pData->m_Handle, &pData->m_FindData)) {
        pData->m_bEnd = TRUE;
    }
    return TRUE;
#elif defined(__native_client__)
    abort();
    return FALSE;
#else
    struct dirent *de = readdir((DIR*)handle);
    if (de == NULL) {
        return FALSE;
    }
    filename = CFX_WideString::FromLocal(de->d_name);
    bFolder = de->d_type == DT_DIR;
    return TRUE;
#endif
}
void FX_CloseFolder(void* handle)
{
    if (handle == NULL) {
        return;
    }
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    CFindFileData* pData = (CFindFileData*)handle;
    FindClose(pData->m_Handle);
    delete pData;
#else
    closedir((DIR*)handle);
#endif
}
FX_WCHAR FX_GetFolderSeparator()
{
#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
    return '\\';
#else
    return '/';
#endif
}

CFX_Matrix_3by3 CFX_Matrix_3by3::Inverse()
{
    FX_FLOAT det = a*(e*i - f*h) - b*(i*d - f*g) + c*(d*h - e*g);
    if (FXSYS_fabs(det) < 0.0000001)
        return CFX_Matrix_3by3();
    else
        return CFX_Matrix_3by3(
            (e*i - f*h) / det,
            -(b*i - c*h) / det,
            (b*f - c*e) / det,
            -(d*i - f*g) / det,
            (a*i - c*g) / det,
            -(a*f - c*d) / det,
            (d*h - e*g) / det,
            -(a*h - b*g) / det,
            (a*e - b*d) / det
        );
}

CFX_Matrix_3by3 CFX_Matrix_3by3::Multiply(const CFX_Matrix_3by3 &m)
{
    return CFX_Matrix_3by3(
        a*m.a + b*m.d + c*m.g,
        a*m.b + b*m.e + c*m.h,
        a*m.c + b*m.f + c*m.i,
        d*m.a + e*m.d + f*m.g,
        d*m.b + e*m.e + f*m.h,
        d*m.c + e*m.f + f*m.i,
        g*m.a + h*m.d + i*m.g,
        g*m.b + h*m.e + i*m.h,
        g*m.c + h*m.f + i*m.i
      );
}

CFX_Vector_3by1 CFX_Matrix_3by3::TransformVector(const CFX_Vector_3by1 &v)
{
    return CFX_Vector_3by1(
        a * v.a + b * v.b + c * v.c,
        d * v.a + e * v.b + f * v.c,
        g * v.a + h * v.b + i * v.c
    );
}