Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
/*
99 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
4 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <include/core/SkBitmap.h>
20
#include <include/core/SkData.h>
21
#include <include/core/SkImage.h>
22
#include <include/core/SkImageGenerator.h>
23
#include <include/core/SkStream.h>
57 andreas 24
#include <include/core/SkFont.h>
4 andreas 25
#include <include/core/SkTypeface.h>
179 andreas 26
#include <include/core/SkColorSpace.h>
4 andreas 27
 
11 andreas 28
#include <iconv.h>
71 andreas 29
#include <libgen.h>
11 andreas 30
 
125 andreas 31
#include <sys/types.h>
32
#include <sys/stat.h>
33
 
4 andreas 34
#include "tresources.h"
197 andreas 35
#include "tpagemanager.h"
4 andreas 36
#include "terror.h"
37
#include "tconfig.h"
38
 
186 andreas 39
#if __cplusplus < 201402L
40
#   error "This module requires at least C++14 standard!"
41
#else
42
#   if __cplusplus < 201703L
43
#       include <experimental/filesystem>
44
namespace fs = std::experimental::filesystem;
45
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
46
#   else
47
#       include <filesystem>
48
#       ifdef __ANDROID__
49
namespace fs = std::__fs::filesystem;
50
#       else
51
namespace fs = std::filesystem;
52
#       endif
53
#   endif
54
#endif
55
 
4 andreas 56
using std::string;
57
using std::endl;
11 andreas 58
using std::vector;
4 andreas 59
 
197 andreas 60
extern TPageManager *gPageManager;
61
 
11 andreas 62
typedef struct
63
{
64
    unsigned char ch;
70 andreas 65
    unsigned int byte;
11 andreas 66
}CHTABLE;
67
 
68
static CHTABLE __cht[] = {
70 andreas 69
    {0x80,	0xE282AC},
70
    {0x81,	0x0081},    // not used
71
    {0x82,	0xE2809A},
72
    {0x83,	0x0192},    // not used
73
    {0x84,	0xE2809E},
74
    {0x85,	0xE280A6},
75
    {0x86,	0xE280A0},
76
    {0x87,	0xE280A1},
77
    {0x88,	0x02C6},    // not used
78
    {0x89,	0xE280B0},
79
    {0x8A,	0xC5A0},
80
    {0x8B,	0xE280B9},
81
    {0x8C,	0xC59A},
82
    {0x8D,	0xC5A4},
83
    {0x8E,	0xC5BD},
84
    {0x8F,	0xC5B9},
85
    {0x90,	0x0090},    // not used
86
    {0x91,	0xE28098},
87
    {0x92,	0xE28099},
88
    {0x93,	0xE2809C},
89
    {0x94,	0xE2809D},
90
    {0x95,	0xE280A2},
91
    {0x96,	0xE28093},
92
    {0x97,	0xE28094},
93
    {0x98,	0x02DC},    // not used
94
    {0x99,	0xE284A2},
95
    {0x9A,	0xC5A1},
96
    {0x9B,	0xE280BA},
97
    {0x9C,	0xC59B},
98
    {0x9D,	0xC5A5},
99
    {0x9E,	0xC5BE},
100
    {0x9F,	0xC5BA},
101
    {0xA0,	0xC2A0},
102
    {0xA1,	0xCB87},
103
    {0xA2,	0xCB98},
104
    {0xA3,	0xC581},
105
    {0xA4,	0xC2A0},
106
    {0xA5,	0xC484},
107
    {0xA6,	0xC2A6},
108
    {0xA7,	0xC2A7},
109
    {0xA8,	0xC2A8},
110
    {0xA9,	0xC2A9},
111
    {0xAA,	0xC59E},
112
    {0xAB,	0xC2AB},
113
    {0xAC,	0xC2AC},
114
    {0xAD,	0xC2AD},
115
    {0xAE,	0xC2AE},
116
    {0xAF,	0xC5BB},
117
    {0xB0,	0xC2B0},
118
    {0xB1,	0xC2B1},
119
    {0xB2,	0xCB9B},
120
    {0xB3,	0xC582},
121
    {0xB4,	0xC2B4},
122
    {0xB5,	0xC2B5},
123
    {0xB6,	0xC2B6},
124
    {0xB7,	0xC2B7},
125
    {0xB8,	0xC2B8},
126
    {0xB9,	0xC485},
127
    {0xBA,	0xC59F},
128
    {0xBB,	0xC2BB},
129
    {0xBC,	0xC4BD},
130
    {0xBD,	0xCB9D},
131
    {0xBE,	0xC4BE},
132
    {0xBF,	0xC5BC},
133
    {0xC0,	0xC594},
134
    {0xC1,	0xC381},
135
    {0xC2,	0xC382},
136
    {0xC3,	0xC482},
137
    {0xC4,	0xC384},
138
    {0xC5,	0xC4B9},
139
    {0xC6,	0xC486},
140
    {0xC7,	0xC387},
141
    {0xC8,	0xC48C},
142
    {0xC9,	0xC389},
143
    {0xCA,	0xC489},
144
    {0xCB,	0xC38b},
145
    {0xCC,	0xC49A},
146
    {0xCD,	0xC38D},
147
    {0xCE,	0xC38E},
148
    {0xCF,	0xC48E},
149
    {0xD0,	0xC490},
150
    {0xD1,	0xC583},
151
    {0xD2,	0xC587},
152
    {0xD3,	0xC398},
153
    {0xD4,	0xC394},
154
    {0xD5,	0xC590},
155
    {0xD6,	0xC396},
156
    {0xD7,	0xC397},
157
    {0xD8,	0xC598},
158
    {0xD9,	0xC5AE},
159
    {0xDA,	0xC39A},
160
    {0xDB,	0xC5B0},
161
    {0xDC,	0xC39C},
162
    {0xDD,	0xC39D},
163
    {0xDE,	0xC5A2},
164
    {0xDF,	0xC39F},
165
    {0xE0,	0xC595},
166
    {0xE1,	0xC3A1},
167
    {0xE2,	0xC3A2},
168
    {0xE3,	0xC483},
169
    {0xE4,	0xC3A4},
170
    {0xE5,	0xC4BA},
171
    {0xE6,	0xC487},
172
    {0xE7,	0xC3A7},
173
    {0xE8,	0xC48D},
174
    {0xE9,	0xC3A9},
175
    {0xEA,	0xC499},
176
    {0xEB,	0xC3AB},
177
    {0xEC,	0xC49B},
178
    {0xED,	0xC3AD},
179
    {0xEE,	0xC3AE},
180
    {0xEF,	0xC48F},
181
    {0xF0,	0xC491},
182
    {0xF1,	0xC584},
183
    {0xF2,	0xC588},
184
    {0xF3,	0xC3B3},
185
    {0xF4,	0xC3B4},
186
    {0xF5,	0xC591},
187
    {0xF6,	0xC3B6},
188
    {0xF7,	0xC3B7},
189
    {0xF8,	0xC599},
190
    {0xF9,	0xC5AF},
191
    {0xFA,	0xC3BA},
192
    {0xFB,	0xC5B1},
193
    {0xFC,	0xC3BC},
194
    {0xFD,	0xC3BD},
195
    {0xFE,	0xC5A3},
196
    {0xFF,	0xCB99}
11 andreas 197
};
198
 
156 andreas 199
SkString GetResourcePath(const char* resource, _RESOURCE_TYPE rs)
4 andreas 200
{
163 andreas 201
    if (!resource)
202
        return SkString();
203
 
204
    if (*resource == '/')       // absolute path?
205
    {                           // yes, then take it as it is
206
        return SkString(resource);
207
    }
208
 
156 andreas 209
    string pth;
67 andreas 210
 
156 andreas 211
    switch(rs)
212
    {
213
        case RESTYPE_BORDER:    pth = "/borders/"; break;
214
        case RESTYPE_CURSOR:    pth = "/cursors/"; break;
215
        case RESTYPE_FONT:      pth = "/fonts/"; break;
216
        case RESTYPE_UNKNOWN:
217
        case RESTYPE_IMAGE:     pth = "/images/"; break;
161 andreas 218
        case RESTYPE_SLIDER:    pth = "/sliders/"; break;
219
        case RESTYPE_SYSBORDER: pth = "/__system/graphics/borders/"; break;
220
        case RESTYPE_SYSCURSOR: pth = "/__system/graphics/cursors/"; break;
221
        case RESTYPE_SYSFONT:   pth = "/__system/graphics/fonts/"; break;
222
        case RESTYPE_SYSIMAGE:  pth = "/__system/graphics/images/"; break;
223
        case RESTYPE_SYSSLIDER: pth = "/__system/graphics/sliders/"; break;
156 andreas 224
    }
225
 
197 andreas 226
    string projectPath = ((gPageManager && gPageManager->isSetupActive()) ? TConfig::getSystemProjectPath() : TConfig::getProjectPath());
186 andreas 227
    string path = projectPath + pth + resource;
4 andreas 228
    return SkString(path);
229
}
230
 
231
bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst)
232
{
233
    std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data)));
234
    return gen && dst->tryAllocPixels(gen->getInfo()) &&
23 andreas 235
    gen->getPixels(gen->getInfo().makeColorType(kBGRA_8888_SkColorType).makeAlphaType(kPremul_SkAlphaType), dst->getPixels(), dst->rowBytes());
4 andreas 236
}
237
 
156 andreas 238
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, _RESOURCE_TYPE rs)
4 andreas 239
{
156 andreas 240
    sk_sp<SkData> data = GetResourceAsData(resource, rs);
4 andreas 241
    return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
242
    : nullptr;
243
}
244
 
156 andreas 245
sk_sp<SkData> GetResourceAsData(const char* resource, _RESOURCE_TYPE rs)
4 andreas 246
{
163 andreas 247
    SkString str = GetResourcePath(resource, rs);
248
    sk_sp<SkData> data = SkData::MakeFromFileName(str.c_str());
249
 
250
    if (data)
4 andreas 251
        return data;
252
 
163 andreas 253
    MSG_ERROR("GetResourceAsData: Resource \"" << str.c_str() << "\" not found.");
4 andreas 254
    TError::setError();
255
#ifdef SK_TOOLS_REQUIRE_RESOURCES
6 andreas 256
    SK_ABORT("GetResourceAsData: missing resource");
4 andreas 257
#endif
258
    return nullptr;
259
}
260
 
156 andreas 261
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex, _RESOURCE_TYPE rs)
4 andreas 262
{
156 andreas 263
    return SkTypeface::MakeFromStream(GetResourceAsStream(resource, rs), ttcIndex);
4 andreas 264
}
6 andreas 265
 
266
/*
267
 * Read the image from a file and save it into a data buffer. This is the base
268
 * to convert the image.
269
 */
270
sk_sp<SkData> readImage(const string& fname)
271
{
272
    sk_sp<SkData> data = GetResourceAsData(fname.c_str());
273
 
274
    if (!data)
275
    {
276
        MSG_ERROR("readImage: Error loading the image " << fname);
277
        TError::setError();
278
    }
279
 
280
    return data;
281
}
11 andreas 282
 
283
vector<string> StrSplit(const string& str, const string& seps, const bool trimEmpty)
284
{
285
    size_t pos = 0, mark = 0;
286
    vector<string> parts;
287
    string::const_iterator it, sepIt;
288
 
96 andreas 289
    if (str.empty())
290
        return parts;
291
 
11 andreas 292
    for (it = str.begin(); it != str.end(); ++it)
293
    {
294
        for (sepIt = seps.begin(); sepIt != seps.end(); ++sepIt)
295
        {
21 andreas 296
            if (pos > 0 && *it == *sepIt)
11 andreas 297
            {
298
                size_t len = pos - mark;
21 andreas 299
 
96 andreas 300
                if (len > 0 && *sepIt != '\n')
21 andreas 301
                    parts.push_back(str.substr(mark, len));
96 andreas 302
                else if (len > 0)
303
                    parts.push_back(str.substr(mark, len) + "\n");
164 andreas 304
                else if (*sepIt == '\n')
305
                    parts.push_back("\n");
21 andreas 306
                else
307
                    parts.push_back(string());
308
 
11 andreas 309
                mark = pos + 1;
310
                break;
311
            }
21 andreas 312
            else if (*it == *sepIt)
96 andreas 313
            {
314
                if (*sepIt == '\n')
315
                    parts.push_back("\n");
316
 
21 andreas 317
                mark = pos + 1;
96 andreas 318
            }
11 andreas 319
        }
320
 
321
        pos++;
322
    }
323
 
324
    parts.push_back(str.substr(mark));
325
 
326
    if (trimEmpty)
327
    {
328
        vector<string> nparts;
329
 
96 andreas 330
        for (auto iter = parts.begin(); iter != parts.end(); ++iter)
11 andreas 331
        {
96 andreas 332
            if (iter->empty())
11 andreas 333
                continue;
334
 
96 andreas 335
            nparts.push_back(*iter);
11 andreas 336
        }
337
 
338
        return nparts;
339
    }
340
 
341
    return parts;
342
}
343
 
344
string latin1ToUTF8(const string& str)
345
{
346
    DECL_TRACER("NameFormat::latin1ToUTF8(const string& str)");
347
    string out;
348
 
349
    for (size_t i = 0; i < str.length(); i++)
350
    {
351
        uint8_t ch = str.at(i);
352
 
353
        if (ch < 0x80)
354
        {
355
            out.push_back(ch);
356
        }
357
        else
358
        {
359
            out.push_back(0xc0 | ch >> 6);
360
            out.push_back(0x80 | (ch & 0x3f));
361
        }
362
    }
363
 
364
    return out;
365
}
366
 
367
string cp1250ToUTF8(const string& str)
368
{
369
    DECL_TRACER("cp1250ToUTF8(const string& str)");
370
 
371
    string out;
372
 
373
    for (size_t j = 0; j < str.length(); j++)
374
    {
375
        int i = -1;
376
        unsigned char ch = str.at(j);
70 andreas 377
        unsigned int utf = 0x80000000;
11 andreas 378
 
70 andreas 379
        if (ch >= 0x80)
11 andreas 380
        {
381
            do
382
            {
383
                i++;
384
 
385
                if (__cht[i].ch == ch)
386
                {
387
                    utf = __cht[i].byte;
388
                    break;
389
                }
390
            }
391
            while (__cht[i].ch != 0xff);
392
 
70 andreas 393
            if (utf == 0x80000000)
11 andreas 394
                utf = ch;
395
        }
396
        else
397
            utf = ch;
398
 
70 andreas 399
        if (utf > 0x00ffff)
11 andreas 400
        {
70 andreas 401
            out.push_back((utf >> 16) & 0x0000ff);
402
            out.push_back((utf >> 8) & 0x0000ff);
403
            out.push_back(utf & 0x0000ff);
11 andreas 404
        }
70 andreas 405
        else if (utf > 0x0000ff)
406
        {
407
            out.push_back((utf >> 8) & 0x0000ff);
408
            out.push_back(utf & 0x0000ff);
409
        }
11 andreas 410
        else if (ch > 0x7f)
411
        {
412
            out.push_back(0xc0 | ch >> 6);
413
            out.push_back(0x80 | (ch & 0x3f));
414
        }
415
        else
416
            out.push_back(ch);
417
    }
418
 
419
    return out;
420
}
421
 
422
string UTF8ToCp1250(const string& str)
423
{
424
    DECL_TRACER("UTF8ToCp1250(const string& str)");
70 andreas 425
#ifdef __ANDROID__
426
    string out;
427
    string::const_iterator iter;
428
    bool three = false;
11 andreas 429
 
70 andreas 430
    for (iter = str.begin(); iter != str.end(); ++iter)
431
    {
432
        unsigned int uch;
433
 
434
        if ((*iter & 0xc0) == 0xc0)     // If UTF8 then we need the next char also
435
        {
436
            uch = 0;
437
 
438
            if ((*iter & 0xe0) == 0xe0) // UTF8 consists of 3 bytes?
439
            {
440
                uch = (*iter << 16) & 0x00ff0000;
441
                ++iter;
442
                three = true;
443
            }
444
 
445
            uch |= ((*iter << 8) & 0x0000ff00);
446
            ++iter;
447
            uch |= (*iter & 0x000000ff);
448
        }
449
        else
450
            uch = *iter;
451
 
452
        if (three || uch > 0x00ff)
453
        {
454
            int i = 0;
455
            bool found = false;
456
 
457
            while(three && __cht[i].ch != 0xff)
458
            {
459
                if (__cht[i].byte == uch)
460
                {
461
                    out.push_back(__cht[i].ch);
462
                    found = true;
463
                    break;
464
                }
465
 
466
                i++;
467
            }
468
 
469
            three = false;
470
 
471
            if (!found)
472
            {
473
                unsigned ch = ((uch & 0x0300) >> 2) | (uch & 0x003f);
474
                out.push_back(ch);
475
            }
476
        }
477
        else
478
            out.push_back(uch);
479
    }
162 andreas 480
 
70 andreas 481
    return out;
482
#else
11 andreas 483
    char dst[1024];
484
    size_t srclen = 0;
485
    char* pIn, *pInSave;
486
 
487
    srclen = str.length();
488
    memset(&dst[0], 0, sizeof(dst));
489
 
490
    try
491
    {
492
        pIn = new char[srclen + 1];
493
        memcpy(pIn, str.c_str(), srclen);
494
        *(pIn+srclen) = 0;
495
        pInSave = pIn;
496
    }
497
    catch(std::exception& e)
498
    {
499
        MSG_ERROR("Error: " << e.what());
500
        return "";
501
    }
502
 
503
    size_t dstlen = sizeof(dst) - 1;
504
    char* pOut = (char *)dst;
505
 
70 andreas 506
    iconv_t conv = iconv_open("UTF-8", "CP1250");
11 andreas 507
 
508
    if (conv == (iconv_t)-1)
509
    {
70 andreas 510
        MSG_ERROR("Error opening iconv: " << strerror(errno));
11 andreas 511
        delete[] pInSave;
512
        return str;
513
    }
514
 
515
    size_t ret = iconv(conv, &pIn, &srclen, &pOut, &dstlen);
516
    iconv_close(conv);
517
    delete[] pInSave;
518
 
519
    if (ret == (size_t)-1)
520
    {
521
        MSG_ERROR("Error converting a string!");
522
        return str;
523
    }
524
 
525
    return string(dst);
70 andreas 526
#endif
11 andreas 527
}
21 andreas 528
 
93 andreas 529
void *renew(char **mem, size_t old_size, size_t new_size)
21 andreas 530
{
531
    if (old_size == new_size)
93 andreas 532
        return *mem;
21 andreas 533
 
93 andreas 534
    if (!mem || !*mem)
535
        return nullptr;
536
 
21 andreas 537
    try
538
    {
93 andreas 539
        char *memory = new char[new_size];
21 andreas 540
        size_t len = (new_size < old_size) ? new_size : old_size;
93 andreas 541
        memcpy(memory, *mem, len);
542
        delete[] *mem;
543
        *mem = memory;
21 andreas 544
        return memory;
545
    }
546
    catch(std::exception& e)
547
    {
548
        MSG_ERROR(e.what());
93 andreas 549
        throw;
21 andreas 550
    }
551
 
552
    return nullptr;
553
}
554
 
555
string toUpper(string& str)
556
{
557
    string::iterator iter;
558
 
559
    for (iter = str.begin(); iter != str.end(); iter++)
560
        *iter = std::toupper(*iter);
561
 
562
    return str;
563
}
564
 
565
string toLower(string& str)
566
{
567
    string::iterator iter;
568
 
569
    for (iter = str.begin(); iter != str.end(); iter++)
570
        *iter = std::tolower(*iter);
571
 
572
    return str;
573
}
54 andreas 574
 
575
vector<string> splitLine(const string& str)
576
{
577
    DECL_TRACER("splitLine(const string& str)");
578
 
579
    vector<string> lines;
580
    string sl;
96 andreas 581
    string::const_iterator iter;
54 andreas 582
 
96 andreas 583
    if (str.empty())
584
        return lines;
585
 
586
    for (iter = str.begin(); iter != str.end(); iter++)
54 andreas 587
    {
96 andreas 588
        if (*iter == '\r')  // ignore bloating byte coming from brain death windows
54 andreas 589
            continue;
590
 
96 andreas 591
        if (*iter == '\n')
54 andreas 592
        {
593
            lines.push_back(sl);
594
            sl.clear();
595
            continue;
596
        }
597
 
57 andreas 598
        char ch[2];
96 andreas 599
        ch[0] = *iter;
57 andreas 600
        ch[1] = 0;
601
        sl.append(ch);
54 andreas 602
    }
603
 
604
    if (!sl.empty())
605
        lines.push_back(sl);
606
 
607
    return lines;
608
}
609
 
57 andreas 610
vector<string> splitLine(const string& str, int width, int height, SkFont& font, SkPaint& paint)
611
{
612
    DECL_TRACER("splitLine(const string& str, int width, int height, SkFont& font, SkPaint& paint)");
613
 
614
    SkRect rect;
70 andreas 615
    vector<string> lines, words;
57 andreas 616
    SkScalar lnHeight = font.getSize();
617
    int maxLines = (int)((SkScalar)height / lnHeight);
70 andreas 618
    string part, oldPart;
96 andreas 619
 
620
    if (str.empty())
621
        return lines;
622
 
70 andreas 623
    words = StrSplit(str, " \n");
96 andreas 624
    MSG_DEBUG("Found " << words.size() << " words.");
70 andreas 625
    vector<string>::iterator iter;
57 andreas 626
 
96 andreas 627
    if (words.size() == 0)
628
        return lines;
629
 
70 andreas 630
    for (iter = words.begin(); iter != words.end(); ++iter)
57 andreas 631
    {
96 andreas 632
        size_t pos;
633
        bool lineBreak = false;
634
 
635
        if ((pos = iter->find("\n")) != string::npos)
636
        {
637
            if (pos > 0)
164 andreas 638
                *iter = iter->substr(0, pos);
96 andreas 639
            else
640
                *iter = "";
641
 
642
            lineBreak = true;
643
        }
644
 
70 andreas 645
        if (part.empty())
646
            part += *iter;
647
        else
648
            part += " " + *iter;
649
 
57 andreas 650
        font.measureText(part.c_str(), part.length(), SkTextEncoding::kUTF8, &rect, &paint);
651
 
652
        if (rect.width() > (width - 8))
653
        {
70 andreas 654
            if (oldPart.empty())
655
            {
656
                string sample;
657
                size_t len = part.length();
658
                size_t pos = 1, start = 0;
57 andreas 659
 
70 andreas 660
                for (size_t i = 0; i < len; i++)
661
                {
662
                    sample = part.substr(start, pos);
663
                    font.measureText(sample.c_str(), sample.length(), SkTextEncoding::kUTF8, &rect, &paint);
664
 
665
                    if (rect.width() > (width - 8))
666
                    {
163 andreas 667
                        lines.push_back(sample.substr(0, sample.length() - 1)); // Cut off the last character because it is already out of bounds.
70 andreas 668
                        start = i;                                              // Set the new start of the string
669
                        i--;                                                    // We must repeat the last character
670
                        pos = 0;                                                // Reset the position counter
671
                        sample = sample.substr(sample.length() - 1);            // Put the last character into the part.
672
 
673
                        if (lines.size() >= (size_t)maxLines)                   // Break if we've reached the maximum of lines
674
                            return lines;
675
                    }
676
 
677
                    pos++;
678
                }
679
 
680
                oldPart.clear();
681
                part = sample;
682
                continue;
683
            }
684
            else
685
            {
686
                lines.push_back(oldPart);
687
                oldPart.clear();
688
                part = *iter;
689
 
690
                if (lines.size() >= (size_t)maxLines)               // Break if we've reached the maximum of lines
691
                    return lines;
692
 
693
                font.measureText(part.c_str(), part.length(), SkTextEncoding::kUTF8, &rect, &paint);
694
 
695
                if (rect.width() > (width - 8))
696
                    continue;
697
            }
57 andreas 698
        }
96 andreas 699
        else if (lineBreak)
700
        {
701
            lines.push_back(part);
702
            part.clear();
703
        }
57 andreas 704
 
70 andreas 705
        oldPart = part;
57 andreas 706
    }
707
 
70 andreas 708
    if (lines.empty())
709
        lines.push_back(str);
710
    else if (!part.empty())
57 andreas 711
        lines.push_back(part);
712
 
713
    return lines;
714
}
60 andreas 715
 
716
bool isHex(int c)
717
{
718
    if ((c >= '0' && c <= '9') ||
719
        (c >= 'a' && c <= 'f') ||
720
        (c >= 'A' && c <= 'F'))
721
        return true;
722
 
723
    return false;
724
}
725
 
726
int strCaseCompare(const string& str1, const string& str2)
727
{
728
    return strcasecmp(str1.c_str(), str2.c_str());
729
}
63 andreas 730
 
731
std::string fillString(int c, int len)
732
{
733
    if (len < 1 || c < 1 || c > 0x00ffffff)
734
        return string();
735
 
736
    string str;
737
    char ch[4];
738
 
739
    if (c <= 0x00ff)
740
    {
741
        ch[0] = c;
742
        ch[1] = 0;
743
    }
744
    else if (c <= 0x00ffff)
745
    {
746
        ch[0] = c >> 8;
747
        ch[1] = c;
748
        ch[2] = 0;
749
    }
750
    else
751
    {
752
        ch[0] = c >> 16;
753
        ch[1] = c >> 8;
754
        ch[2] = c;
755
        ch[3] = 0;
756
    }
757
 
758
    for (int i = 0; i < len; i++)
759
        str.append(ch);
760
 
761
    return str;
762
}
67 andreas 763
 
764
bool isUTF8(const string& str)
765
{
766
    int c,i,ix,n,j;
767
 
768
    for (i=0, ix = str.length(); i < ix; i++)
769
    {
770
        c = (unsigned char) str[i];
771
 
772
        if (0x00 <= c && c <= 0x7f)
773
            n=0; // 0bbbbbbb
774
        else if ((c & 0xE0) == 0xC0)
775
            n=1; // 110bbbbb
776
        else if ( c==0xed && i<(ix-1) && ((unsigned char)str[i+1] & 0xa0)==0xa0)
777
            return false; //U+d800 to U+dfff
778
        else if ((c & 0xF0) == 0xE0)
779
            n=2; // 1110bbbb
780
        else if ((c & 0xF8) == 0xF0)
781
            n=3; // 11110bbb
782
        else
783
            return false;
784
 
785
        for (j=0; j<n && i<ix; j++) // n bytes matching 10bbbbbb follow ?
786
        {
787
            if ((++i == ix) || (( (unsigned char)str[i] & 0xC0) != 0x80))
788
                return false;
789
        }
790
    }
791
 
792
    return true;
793
}
794
 
795
size_t utf8Strlen(const std::string& str)
796
{
797
    int c,i,ix;
798
    size_t q;
799
 
800
    for (q = 0, i = 0, ix = str.length(); i < ix; i++, q++)
801
    {
157 andreas 802
        c = (unsigned char)str[i];
67 andreas 803
 
157 andreas 804
        if (c >= 0 && c <= 127)
805
            i += 0;
67 andreas 806
        else if ((c & 0xE0) == 0xC0)
157 andreas 807
            i += 1;
67 andreas 808
        else if ((c & 0xF0) == 0xE0)
157 andreas 809
            i += 2;
67 andreas 810
        else if ((c & 0xF8) == 0xF0)
157 andreas 811
            i += 3;
67 andreas 812
        else
813
            return 0;   //invalid utf8
814
    }
815
 
816
    return q;
817
}
818
 
819
uint16_t getUint16(const unsigned char *p, bool big_endian)
820
{
821
    if (!p)
822
        return 0;
823
 
824
    uint16_t num;
825
    memmove(&num, p, sizeof(uint16_t));
826
 
827
    if (!big_endian)
828
    {
829
        uint16_t le_num = ((num >> 8) & 0x00ff) | ((num << 8) & 0xff00);
830
        num = le_num;
831
    }
832
 
833
    return num;
834
}
835
 
836
uint32_t getUint32(const unsigned char *p, bool big_endian)
837
{
838
    if (!p)
839
        return 0;
840
 
841
    uint32_t num;
842
    memmove(&num, p, sizeof(uint32_t));
843
 
844
    if (!big_endian)
845
    {
846
        uint32_t le_num = ((num >> 24) & 0x000000ff) | ((num >> 8) & 0x0000ff00) | ((num << 8) & 0x00ff0000) | ((num << 24) & 0xff000000);
847
        num = le_num;
848
    }
849
 
850
    return num;
851
}
71 andreas 852
 
853
bool endsWith (const std::string &src, const std::string &end)
854
{
855
    vector<string> list;
856
 
857
    if (end.find("|") == string::npos)
858
        list.push_back(end);
859
    else
860
        list = StrSplit(end, "|", false);
861
 
862
    vector<string>::iterator iter;
863
 
864
    for (iter = list.begin(); iter != list.end(); ++iter)
865
    {
866
        size_t len = iter->length();
867
 
868
        if (len > src.length())
869
            continue;
870
 
871
        string part = src.substr(src.length() - len);
872
 
873
        if (part.compare(*iter) == 0)
874
            return true;
875
    }
876
 
877
    return false;
878
}
879
 
880
bool startsWith (const std::string &src, const std::string &start)
881
{
882
    vector<string> list;
883
 
884
    if (start.find("|") == string::npos)
885
        list.push_back(start);
886
    else
887
        list = StrSplit(start, "|", false);
888
 
889
    vector<string>::iterator iter;
890
 
891
    for (iter = list.begin(); iter != list.end(); ++iter)
892
    {
893
        size_t len = iter->length();
894
 
895
        if (len > src.length())
896
            continue;
897
 
898
        string part = src.substr(0, len);
899
 
900
        if (part.compare(*iter) == 0)
901
            return true;
902
    }
903
 
904
    return false;
905
}
906
 
907
std::string dirName (const std::string &path)
908
{
909
    if (path.empty())
910
        return ".";
911
 
912
    size_t len = path.length();
913
 
914
    char *buffer = new char[len+1];
915
    memset(buffer, 0, len+1);
916
    strncpy(buffer, path.c_str(), len);
917
 
918
    char *dir = dirname(buffer);
919
    string ret(dir);
920
    delete[] buffer;
921
    return ret;
922
}
923
 
924
std::string baseName (const std::string &path)
925
{
926
    if (path.empty())
927
        return ".";
928
 
929
    size_t len = path.length();
930
 
931
    char *buffer = new char[len+1];
932
    memset(buffer, 0, len+1);
933
    strncpy(buffer, path.c_str(), len);
934
 
935
    char *dir = basename(buffer);
936
    string ret(dir);
937
    delete[] buffer;
938
    return ret;
939
}
97 andreas 940
 
222 andreas 941
/**
942
 * @brief strnstr - Find the needle in a haystack
943
 * The function searches for a string in a larger string. In case the wanted
944
 * string is found it returns a pointer to the start of the string in \b haystack.
945
 *
946
 * @param haystack  The string who may contain \b needle.
947
 * @param needle    The string to search for in \b haystack.
948
 * @param len       The length of the \b haystack.
949
 *
950
 * @return If the \b needle was found in \b haystack a pointer to the start of
951
 * the string in \b haystack is returned. Otherwise a NULL pointer is returned.
952
 */
97 andreas 953
char *strnstr(const char* haystack, const char* needle, size_t len)
954
{
955
    if (!haystack || !needle)
956
        return nullptr;
957
 
958
    size_t needleLen = strlen(needle);
959
 
960
    if (needleLen > len)
961
        return nullptr;
962
 
963
    char *start = (char *)haystack;
964
    size_t pos = 0;
965
 
966
    while ((pos + needleLen) < len)
967
    {
968
        bool match = true;
969
 
970
        for (size_t i = 0; i < needleLen; i++)
971
        {
972
            if (*(start + i) != *(needle + i))
973
            {
974
                match = false;
975
                break;
976
            }
977
        }
978
 
979
        if (match)
980
            return start;
981
 
982
        pos++;
983
        start++;
984
    }
985
 
986
    return nullptr;
987
}
104 andreas 988
 
222 andreas 989
bool StrContains(const std::string& str, const std::string& part)
990
{
991
    return str.find(part) != string::npos;
992
}
993
 
104 andreas 994
string getCommand(const string& fullCmd)
995
{
996
    DECL_TRACER("getCommand(const string& fullCmd)");
997
 
998
    size_t pos = fullCmd.find_first_of("-");
999
    string cmd;
1000
 
1001
    if (pos != string::npos)
153 andreas 1002
    {
104 andreas 1003
        cmd = fullCmd.substr(0, pos);
153 andreas 1004
        cmd = toUpper(cmd);
1005
    }
104 andreas 1006
    else
153 andreas 1007
    {
104 andreas 1008
        cmd = fullCmd;
153 andreas 1009
        cmd = toUpper(cmd);
1010
    }
104 andreas 1011
 
1012
    return cmd;
1013
}
162 andreas 1014
 
233 andreas 1015
bool isTrue(const std::string &value)
1016
{
1017
    std::string v = value;
1018
    std::string low = toLower(v);
1019
 
1020
    if (low.find("true") != std::string::npos ||
1021
        low.find("on") != std::string::npos ||
1022
        low.find("yes") != std::string::npos ||
1023
        low.find("1") != std::string::npos)
1024
        return true;
1025
 
1026
    return false;
1027
}
1028
 
1029
bool isFalse(const std::string &value)
1030
{
1031
    std::string v = value;
1032
    std::string low = toLower(v);
1033
 
1034
    if (low.find("false") != std::string::npos ||
1035
        low.find("off") != std::string::npos ||
1036
        low.find("no") != std::string::npos ||
1037
        low.find("0") != std::string::npos)
1038
        return true;
1039
 
1040
    return false;
1041
}
1042
 
1043
bool isNumeric(const std::string &str, bool blank)
1044
{
1045
    std::string::const_iterator iter;
1046
 
1047
    for (iter = str.begin(); iter != str.end(); ++iter)
1048
    {
1049
        if (*iter < '0' || *iter > '9')
1050
        {
1051
            if (blank && *iter == ' ')
1052
                continue;
1053
 
1054
            return false;
1055
        }
1056
    }
1057
 
1058
    return true;
1059
}