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
{
262 andreas 233
    if (!data || !dst)
234
        return false;
235
 
4 andreas 236
    std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data)));
237
    return gen && dst->tryAllocPixels(gen->getInfo()) &&
262 andreas 238
            gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
4 andreas 239
}
240
 
156 andreas 241
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, _RESOURCE_TYPE rs)
4 andreas 242
{
156 andreas 243
    sk_sp<SkData> data = GetResourceAsData(resource, rs);
4 andreas 244
    return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
245
    : nullptr;
246
}
247
 
156 andreas 248
sk_sp<SkData> GetResourceAsData(const char* resource, _RESOURCE_TYPE rs)
4 andreas 249
{
163 andreas 250
    SkString str = GetResourcePath(resource, rs);
262 andreas 251
 
163 andreas 252
    sk_sp<SkData> data = SkData::MakeFromFileName(str.c_str());
253
 
254
    if (data)
4 andreas 255
        return data;
256
 
163 andreas 257
    MSG_ERROR("GetResourceAsData: Resource \"" << str.c_str() << "\" not found.");
4 andreas 258
    TError::setError();
259
#ifdef SK_TOOLS_REQUIRE_RESOURCES
6 andreas 260
    SK_ABORT("GetResourceAsData: missing resource");
4 andreas 261
#endif
262
    return nullptr;
263
}
264
 
156 andreas 265
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex, _RESOURCE_TYPE rs)
4 andreas 266
{
156 andreas 267
    return SkTypeface::MakeFromStream(GetResourceAsStream(resource, rs), ttcIndex);
4 andreas 268
}
6 andreas 269
 
270
/*
271
 * Read the image from a file and save it into a data buffer. This is the base
272
 * to convert the image.
273
 */
274
sk_sp<SkData> readImage(const string& fname)
275
{
276
    sk_sp<SkData> data = GetResourceAsData(fname.c_str());
277
 
278
    if (!data)
279
    {
280
        MSG_ERROR("readImage: Error loading the image " << fname);
281
        TError::setError();
282
    }
283
 
284
    return data;
285
}
11 andreas 286
 
254 andreas 287
SkBitmap *allocPixels(int width, int height, SkBitmap *bm)
288
{
289
    DECL_TRACER("TButton::allocPixels(int width, int height, SkBitmap *bm)");
290
 
291
    if (!bm)
292
        return nullptr;
293
 
294
    // Skia reads image files in the natural byte order of the CPU.
295
    // While on Intel CPUs the byte order is little endian it is
296
    // mostly big endian on other CPUs. This means that the order of
260 andreas 297
    // the colors is RGB on big endian CPUs (ARM, ...) and BGR on others.
298
    // To compensate this, we check the endianess of the CPU and set
299
    // the byte order according.
300
    SkImageInfo info;
301
 
302
    if (isBigEndian())
303
        info = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
304
    else
305
        info = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
306
 
254 andreas 307
    if (!bm->tryAllocPixels(info))
308
    {
309
        MSG_ERROR("Error allocating " << (width * height) << " pixels!");
310
        return nullptr;
311
    }
312
 
313
    return bm;
314
}
315
 
316
SkColor reverseColor(const SkColor& col)
317
{
318
    DECL_TRACER("reverseColor(const SkColor& col)");
319
 
320
    int red = SkColorGetR(col);
321
    int green = SkColorGetG(col);
322
    int blue = SkColorGetB(col);
323
    int alpha = SkColorGetA(col);
324
 
325
    return SkColorSetARGB(alpha, blue, green, red);
326
}
327
 
11 andreas 328
vector<string> StrSplit(const string& str, const string& seps, const bool trimEmpty)
329
{
330
    size_t pos = 0, mark = 0;
331
    vector<string> parts;
332
    string::const_iterator it, sepIt;
333
 
96 andreas 334
    if (str.empty())
335
        return parts;
336
 
11 andreas 337
    for (it = str.begin(); it != str.end(); ++it)
338
    {
339
        for (sepIt = seps.begin(); sepIt != seps.end(); ++sepIt)
340
        {
21 andreas 341
            if (pos > 0 && *it == *sepIt)
11 andreas 342
            {
343
                size_t len = pos - mark;
21 andreas 344
 
96 andreas 345
                if (len > 0 && *sepIt != '\n')
21 andreas 346
                    parts.push_back(str.substr(mark, len));
96 andreas 347
                else if (len > 0)
348
                    parts.push_back(str.substr(mark, len) + "\n");
164 andreas 349
                else if (*sepIt == '\n')
350
                    parts.push_back("\n");
21 andreas 351
                else
352
                    parts.push_back(string());
353
 
11 andreas 354
                mark = pos + 1;
355
                break;
356
            }
21 andreas 357
            else if (*it == *sepIt)
96 andreas 358
            {
359
                if (*sepIt == '\n')
360
                    parts.push_back("\n");
361
 
21 andreas 362
                mark = pos + 1;
96 andreas 363
            }
11 andreas 364
        }
365
 
366
        pos++;
367
    }
368
 
369
    parts.push_back(str.substr(mark));
370
 
371
    if (trimEmpty)
372
    {
373
        vector<string> nparts;
374
 
96 andreas 375
        for (auto iter = parts.begin(); iter != parts.end(); ++iter)
11 andreas 376
        {
96 andreas 377
            if (iter->empty())
11 andreas 378
                continue;
379
 
96 andreas 380
            nparts.push_back(*iter);
11 andreas 381
        }
382
 
383
        return nparts;
384
    }
385
 
386
    return parts;
387
}
388
 
389
string latin1ToUTF8(const string& str)
390
{
391
    DECL_TRACER("NameFormat::latin1ToUTF8(const string& str)");
392
    string out;
393
 
394
    for (size_t i = 0; i < str.length(); i++)
395
    {
396
        uint8_t ch = str.at(i);
397
 
398
        if (ch < 0x80)
399
        {
400
            out.push_back(ch);
401
        }
402
        else
403
        {
404
            out.push_back(0xc0 | ch >> 6);
405
            out.push_back(0x80 | (ch & 0x3f));
406
        }
407
    }
408
 
409
    return out;
410
}
411
 
412
string cp1250ToUTF8(const string& str)
413
{
414
    DECL_TRACER("cp1250ToUTF8(const string& str)");
415
 
416
    string out;
417
 
418
    for (size_t j = 0; j < str.length(); j++)
419
    {
420
        int i = -1;
421
        unsigned char ch = str.at(j);
70 andreas 422
        unsigned int utf = 0x80000000;
11 andreas 423
 
70 andreas 424
        if (ch >= 0x80)
11 andreas 425
        {
426
            do
427
            {
428
                i++;
429
 
430
                if (__cht[i].ch == ch)
431
                {
432
                    utf = __cht[i].byte;
433
                    break;
434
                }
435
            }
436
            while (__cht[i].ch != 0xff);
437
 
70 andreas 438
            if (utf == 0x80000000)
11 andreas 439
                utf = ch;
440
        }
441
        else
442
            utf = ch;
443
 
70 andreas 444
        if (utf > 0x00ffff)
11 andreas 445
        {
70 andreas 446
            out.push_back((utf >> 16) & 0x0000ff);
447
            out.push_back((utf >> 8) & 0x0000ff);
448
            out.push_back(utf & 0x0000ff);
11 andreas 449
        }
70 andreas 450
        else if (utf > 0x0000ff)
451
        {
452
            out.push_back((utf >> 8) & 0x0000ff);
453
            out.push_back(utf & 0x0000ff);
454
        }
11 andreas 455
        else if (ch > 0x7f)
456
        {
457
            out.push_back(0xc0 | ch >> 6);
458
            out.push_back(0x80 | (ch & 0x3f));
459
        }
460
        else
461
            out.push_back(ch);
462
    }
463
 
464
    return out;
465
}
466
 
467
string UTF8ToCp1250(const string& str)
468
{
469
    DECL_TRACER("UTF8ToCp1250(const string& str)");
70 andreas 470
#ifdef __ANDROID__
471
    string out;
472
    string::const_iterator iter;
473
    bool three = false;
11 andreas 474
 
70 andreas 475
    for (iter = str.begin(); iter != str.end(); ++iter)
476
    {
477
        unsigned int uch;
478
 
479
        if ((*iter & 0xc0) == 0xc0)     // If UTF8 then we need the next char also
480
        {
481
            uch = 0;
482
 
483
            if ((*iter & 0xe0) == 0xe0) // UTF8 consists of 3 bytes?
484
            {
485
                uch = (*iter << 16) & 0x00ff0000;
486
                ++iter;
487
                three = true;
488
            }
489
 
490
            uch |= ((*iter << 8) & 0x0000ff00);
491
            ++iter;
492
            uch |= (*iter & 0x000000ff);
493
        }
494
        else
495
            uch = *iter;
496
 
497
        if (three || uch > 0x00ff)
498
        {
499
            int i = 0;
500
            bool found = false;
501
 
502
            while(three && __cht[i].ch != 0xff)
503
            {
504
                if (__cht[i].byte == uch)
505
                {
506
                    out.push_back(__cht[i].ch);
507
                    found = true;
508
                    break;
509
                }
510
 
511
                i++;
512
            }
513
 
514
            three = false;
515
 
516
            if (!found)
517
            {
518
                unsigned ch = ((uch & 0x0300) >> 2) | (uch & 0x003f);
519
                out.push_back(ch);
520
            }
521
        }
522
        else
523
            out.push_back(uch);
524
    }
162 andreas 525
 
70 andreas 526
    return out;
527
#else
11 andreas 528
    char dst[1024];
529
    size_t srclen = 0;
530
    char* pIn, *pInSave;
531
 
532
    srclen = str.length();
533
    memset(&dst[0], 0, sizeof(dst));
534
 
535
    try
536
    {
537
        pIn = new char[srclen + 1];
538
        memcpy(pIn, str.c_str(), srclen);
539
        *(pIn+srclen) = 0;
540
        pInSave = pIn;
541
    }
542
    catch(std::exception& e)
543
    {
544
        MSG_ERROR("Error: " << e.what());
545
        return "";
546
    }
547
 
548
    size_t dstlen = sizeof(dst) - 1;
549
    char* pOut = (char *)dst;
550
 
70 andreas 551
    iconv_t conv = iconv_open("UTF-8", "CP1250");
11 andreas 552
 
553
    if (conv == (iconv_t)-1)
554
    {
70 andreas 555
        MSG_ERROR("Error opening iconv: " << strerror(errno));
11 andreas 556
        delete[] pInSave;
557
        return str;
558
    }
559
 
560
    size_t ret = iconv(conv, &pIn, &srclen, &pOut, &dstlen);
561
    iconv_close(conv);
562
    delete[] pInSave;
563
 
564
    if (ret == (size_t)-1)
565
    {
566
        MSG_ERROR("Error converting a string!");
567
        return str;
568
    }
569
 
570
    return string(dst);
70 andreas 571
#endif
11 andreas 572
}
21 andreas 573
 
263 andreas 574
std::string intToString(int num)
575
{
576
    std::stringstream ss;
577
    ss << num;
578
    return ss.str();
579
}
580
 
93 andreas 581
void *renew(char **mem, size_t old_size, size_t new_size)
21 andreas 582
{
583
    if (old_size == new_size)
93 andreas 584
        return *mem;
21 andreas 585
 
93 andreas 586
    if (!mem || !*mem)
587
        return nullptr;
588
 
21 andreas 589
    try
590
    {
93 andreas 591
        char *memory = new char[new_size];
21 andreas 592
        size_t len = (new_size < old_size) ? new_size : old_size;
93 andreas 593
        memcpy(memory, *mem, len);
594
        delete[] *mem;
595
        *mem = memory;
21 andreas 596
        return memory;
597
    }
598
    catch(std::exception& e)
599
    {
600
        MSG_ERROR(e.what());
93 andreas 601
        throw;
21 andreas 602
    }
603
 
604
    return nullptr;
605
}
606
 
607
string toUpper(string& str)
608
{
609
    string::iterator iter;
610
 
611
    for (iter = str.begin(); iter != str.end(); iter++)
612
        *iter = std::toupper(*iter);
613
 
614
    return str;
615
}
616
 
617
string toLower(string& str)
618
{
619
    string::iterator iter;
620
 
621
    for (iter = str.begin(); iter != str.end(); iter++)
622
        *iter = std::tolower(*iter);
623
 
624
    return str;
625
}
54 andreas 626
 
627
vector<string> splitLine(const string& str)
628
{
629
    DECL_TRACER("splitLine(const string& str)");
630
 
631
    vector<string> lines;
632
    string sl;
96 andreas 633
    string::const_iterator iter;
54 andreas 634
 
96 andreas 635
    if (str.empty())
636
        return lines;
637
 
638
    for (iter = str.begin(); iter != str.end(); iter++)
54 andreas 639
    {
96 andreas 640
        if (*iter == '\r')  // ignore bloating byte coming from brain death windows
54 andreas 641
            continue;
642
 
96 andreas 643
        if (*iter == '\n')
54 andreas 644
        {
645
            lines.push_back(sl);
646
            sl.clear();
647
            continue;
648
        }
649
 
57 andreas 650
        char ch[2];
96 andreas 651
        ch[0] = *iter;
57 andreas 652
        ch[1] = 0;
653
        sl.append(ch);
54 andreas 654
    }
655
 
656
    if (!sl.empty())
657
        lines.push_back(sl);
658
 
659
    return lines;
660
}
661
 
57 andreas 662
vector<string> splitLine(const string& str, int width, int height, SkFont& font, SkPaint& paint)
663
{
664
    DECL_TRACER("splitLine(const string& str, int width, int height, SkFont& font, SkPaint& paint)");
665
 
666
    SkRect rect;
70 andreas 667
    vector<string> lines, words;
57 andreas 668
    SkScalar lnHeight = font.getSize();
669
    int maxLines = (int)((SkScalar)height / lnHeight);
70 andreas 670
    string part, oldPart;
96 andreas 671
 
672
    if (str.empty())
673
        return lines;
674
 
70 andreas 675
    words = StrSplit(str, " \n");
96 andreas 676
    MSG_DEBUG("Found " << words.size() << " words.");
70 andreas 677
    vector<string>::iterator iter;
57 andreas 678
 
96 andreas 679
    if (words.size() == 0)
680
        return lines;
681
 
70 andreas 682
    for (iter = words.begin(); iter != words.end(); ++iter)
57 andreas 683
    {
96 andreas 684
        size_t pos;
685
        bool lineBreak = false;
686
 
687
        if ((pos = iter->find("\n")) != string::npos)
688
        {
689
            if (pos > 0)
164 andreas 690
                *iter = iter->substr(0, pos);
96 andreas 691
            else
692
                *iter = "";
693
 
694
            lineBreak = true;
695
        }
696
 
70 andreas 697
        if (part.empty())
698
            part += *iter;
699
        else
700
            part += " " + *iter;
701
 
57 andreas 702
        font.measureText(part.c_str(), part.length(), SkTextEncoding::kUTF8, &rect, &paint);
703
 
704
        if (rect.width() > (width - 8))
705
        {
70 andreas 706
            if (oldPart.empty())
707
            {
708
                string sample;
709
                size_t len = part.length();
710
                size_t pos = 1, start = 0;
57 andreas 711
 
70 andreas 712
                for (size_t i = 0; i < len; i++)
713
                {
714
                    sample = part.substr(start, pos);
715
                    font.measureText(sample.c_str(), sample.length(), SkTextEncoding::kUTF8, &rect, &paint);
716
 
717
                    if (rect.width() > (width - 8))
718
                    {
163 andreas 719
                        lines.push_back(sample.substr(0, sample.length() - 1)); // Cut off the last character because it is already out of bounds.
70 andreas 720
                        start = i;                                              // Set the new start of the string
721
                        i--;                                                    // We must repeat the last character
722
                        pos = 0;                                                // Reset the position counter
723
                        sample = sample.substr(sample.length() - 1);            // Put the last character into the part.
724
 
725
                        if (lines.size() >= (size_t)maxLines)                   // Break if we've reached the maximum of lines
726
                            return lines;
727
                    }
728
 
729
                    pos++;
730
                }
731
 
732
                oldPart.clear();
733
                part = sample;
734
                continue;
735
            }
736
            else
737
            {
738
                lines.push_back(oldPart);
739
                oldPart.clear();
740
                part = *iter;
741
 
742
                if (lines.size() >= (size_t)maxLines)               // Break if we've reached the maximum of lines
743
                    return lines;
744
 
745
                font.measureText(part.c_str(), part.length(), SkTextEncoding::kUTF8, &rect, &paint);
746
 
747
                if (rect.width() > (width - 8))
748
                    continue;
749
            }
57 andreas 750
        }
96 andreas 751
        else if (lineBreak)
752
        {
753
            lines.push_back(part);
754
            part.clear();
755
        }
57 andreas 756
 
70 andreas 757
        oldPart = part;
57 andreas 758
    }
759
 
70 andreas 760
    if (lines.empty())
761
        lines.push_back(str);
762
    else if (!part.empty())
57 andreas 763
        lines.push_back(part);
764
 
765
    return lines;
766
}
60 andreas 767
 
768
bool isHex(int c)
769
{
770
    if ((c >= '0' && c <= '9') ||
771
        (c >= 'a' && c <= 'f') ||
772
        (c >= 'A' && c <= 'F'))
773
        return true;
774
 
775
    return false;
776
}
777
 
778
int strCaseCompare(const string& str1, const string& str2)
779
{
780
    return strcasecmp(str1.c_str(), str2.c_str());
781
}
63 andreas 782
 
783
std::string fillString(int c, int len)
784
{
785
    if (len < 1 || c < 1 || c > 0x00ffffff)
786
        return string();
787
 
788
    string str;
789
    char ch[4];
790
 
791
    if (c <= 0x00ff)
792
    {
793
        ch[0] = c;
794
        ch[1] = 0;
795
    }
796
    else if (c <= 0x00ffff)
797
    {
798
        ch[0] = c >> 8;
799
        ch[1] = c;
800
        ch[2] = 0;
801
    }
802
    else
803
    {
804
        ch[0] = c >> 16;
805
        ch[1] = c >> 8;
806
        ch[2] = c;
807
        ch[3] = 0;
808
    }
809
 
810
    for (int i = 0; i < len; i++)
811
        str.append(ch);
812
 
813
    return str;
814
}
67 andreas 815
 
816
bool isUTF8(const string& str)
817
{
818
    int c,i,ix,n,j;
819
 
820
    for (i=0, ix = str.length(); i < ix; i++)
821
    {
822
        c = (unsigned char) str[i];
823
 
824
        if (0x00 <= c && c <= 0x7f)
825
            n=0; // 0bbbbbbb
826
        else if ((c & 0xE0) == 0xC0)
827
            n=1; // 110bbbbb
828
        else if ( c==0xed && i<(ix-1) && ((unsigned char)str[i+1] & 0xa0)==0xa0)
829
            return false; //U+d800 to U+dfff
830
        else if ((c & 0xF0) == 0xE0)
831
            n=2; // 1110bbbb
832
        else if ((c & 0xF8) == 0xF0)
833
            n=3; // 11110bbb
834
        else
835
            return false;
836
 
837
        for (j=0; j<n && i<ix; j++) // n bytes matching 10bbbbbb follow ?
838
        {
839
            if ((++i == ix) || (( (unsigned char)str[i] & 0xC0) != 0x80))
840
                return false;
841
        }
842
    }
843
 
844
    return true;
845
}
846
 
847
size_t utf8Strlen(const std::string& str)
848
{
849
    int c,i,ix;
850
    size_t q;
851
 
852
    for (q = 0, i = 0, ix = str.length(); i < ix; i++, q++)
853
    {
157 andreas 854
        c = (unsigned char)str[i];
67 andreas 855
 
157 andreas 856
        if (c >= 0 && c <= 127)
857
            i += 0;
67 andreas 858
        else if ((c & 0xE0) == 0xC0)
157 andreas 859
            i += 1;
67 andreas 860
        else if ((c & 0xF0) == 0xE0)
157 andreas 861
            i += 2;
67 andreas 862
        else if ((c & 0xF8) == 0xF0)
157 andreas 863
            i += 3;
67 andreas 864
        else
865
            return 0;   //invalid utf8
866
    }
867
 
868
    return q;
869
}
870
 
871
uint16_t getUint16(const unsigned char *p, bool big_endian)
872
{
873
    if (!p)
874
        return 0;
875
 
876
    uint16_t num;
877
    memmove(&num, p, sizeof(uint16_t));
878
 
879
    if (!big_endian)
880
    {
881
        uint16_t le_num = ((num >> 8) & 0x00ff) | ((num << 8) & 0xff00);
882
        num = le_num;
883
    }
884
 
885
    return num;
886
}
887
 
888
uint32_t getUint32(const unsigned char *p, bool big_endian)
889
{
890
    if (!p)
891
        return 0;
892
 
893
    uint32_t num;
894
    memmove(&num, p, sizeof(uint32_t));
895
 
896
    if (!big_endian)
897
    {
898
        uint32_t le_num = ((num >> 24) & 0x000000ff) | ((num >> 8) & 0x0000ff00) | ((num << 8) & 0x00ff0000) | ((num << 24) & 0xff000000);
899
        num = le_num;
900
    }
901
 
902
    return num;
903
}
71 andreas 904
 
905
bool endsWith (const std::string &src, const std::string &end)
906
{
907
    vector<string> list;
908
 
909
    if (end.find("|") == string::npos)
910
        list.push_back(end);
911
    else
912
        list = StrSplit(end, "|", false);
913
 
914
    vector<string>::iterator iter;
915
 
916
    for (iter = list.begin(); iter != list.end(); ++iter)
917
    {
918
        size_t len = iter->length();
919
 
920
        if (len > src.length())
921
            continue;
922
 
923
        string part = src.substr(src.length() - len);
924
 
925
        if (part.compare(*iter) == 0)
926
            return true;
927
    }
928
 
929
    return false;
930
}
931
 
932
bool startsWith (const std::string &src, const std::string &start)
933
{
934
    vector<string> list;
935
 
936
    if (start.find("|") == string::npos)
937
        list.push_back(start);
938
    else
939
        list = StrSplit(start, "|", false);
940
 
941
    vector<string>::iterator iter;
942
 
943
    for (iter = list.begin(); iter != list.end(); ++iter)
944
    {
945
        size_t len = iter->length();
946
 
947
        if (len > src.length())
948
            continue;
949
 
950
        string part = src.substr(0, len);
951
 
952
        if (part.compare(*iter) == 0)
953
            return true;
954
    }
955
 
956
    return false;
957
}
958
 
959
std::string dirName (const std::string &path)
960
{
961
    if (path.empty())
962
        return ".";
963
 
964
    size_t len = path.length();
965
 
966
    char *buffer = new char[len+1];
967
    memset(buffer, 0, len+1);
968
    strncpy(buffer, path.c_str(), len);
969
 
970
    char *dir = dirname(buffer);
971
    string ret(dir);
972
    delete[] buffer;
973
    return ret;
974
}
975
 
976
std::string baseName (const std::string &path)
977
{
978
    if (path.empty())
979
        return ".";
980
 
981
    size_t len = path.length();
982
 
983
    char *buffer = new char[len+1];
984
    memset(buffer, 0, len+1);
985
    strncpy(buffer, path.c_str(), len);
986
 
987
    char *dir = basename(buffer);
988
    string ret(dir);
989
    delete[] buffer;
990
    return ret;
991
}
97 andreas 992
 
222 andreas 993
/**
994
 * @brief strnstr - Find the needle in a haystack
995
 * The function searches for a string in a larger string. In case the wanted
996
 * string is found it returns a pointer to the start of the string in \b haystack.
997
 *
998
 * @param haystack  The string who may contain \b needle.
999
 * @param needle    The string to search for in \b haystack.
1000
 * @param len       The length of the \b haystack.
1001
 *
1002
 * @return If the \b needle was found in \b haystack a pointer to the start of
1003
 * the string in \b haystack is returned. Otherwise a NULL pointer is returned.
1004
 */
97 andreas 1005
char *strnstr(const char* haystack, const char* needle, size_t len)
1006
{
1007
    if (!haystack || !needle)
1008
        return nullptr;
1009
 
1010
    size_t needleLen = strlen(needle);
1011
 
1012
    if (needleLen > len)
1013
        return nullptr;
1014
 
1015
    char *start = (char *)haystack;
1016
    size_t pos = 0;
1017
 
1018
    while ((pos + needleLen) < len)
1019
    {
1020
        bool match = true;
1021
 
1022
        for (size_t i = 0; i < needleLen; i++)
1023
        {
1024
            if (*(start + i) != *(needle + i))
1025
            {
1026
                match = false;
1027
                break;
1028
            }
1029
        }
1030
 
1031
        if (match)
1032
            return start;
1033
 
1034
        pos++;
1035
        start++;
1036
    }
1037
 
1038
    return nullptr;
1039
}
104 andreas 1040
 
222 andreas 1041
bool StrContains(const std::string& str, const std::string& part)
1042
{
1043
    return str.find(part) != string::npos;
1044
}
1045
 
268 andreas 1046
std::string ReplaceString(const std::string subject, const std::string& search, const std::string& replace)
1047
{
1048
    size_t pos = 0;
1049
    string sub = subject;
1050
 
1051
    while ((pos = sub.find(search, pos)) != std::string::npos)
1052
    {
1053
        sub.replace(pos, search.length(), replace);
1054
        pos += replace.length();
1055
    }
1056
 
1057
    return sub;
1058
}
1059
 
104 andreas 1060
string getCommand(const string& fullCmd)
1061
{
1062
    DECL_TRACER("getCommand(const string& fullCmd)");
1063
 
1064
    size_t pos = fullCmd.find_first_of("-");
1065
    string cmd;
1066
 
1067
    if (pos != string::npos)
153 andreas 1068
    {
104 andreas 1069
        cmd = fullCmd.substr(0, pos);
153 andreas 1070
        cmd = toUpper(cmd);
1071
    }
104 andreas 1072
    else
153 andreas 1073
    {
104 andreas 1074
        cmd = fullCmd;
153 andreas 1075
        cmd = toUpper(cmd);
1076
    }
104 andreas 1077
 
1078
    return cmd;
1079
}
162 andreas 1080
 
233 andreas 1081
bool isTrue(const std::string &value)
1082
{
1083
    std::string v = value;
1084
    std::string low = toLower(v);
1085
 
1086
    if (low.find("true") != std::string::npos ||
1087
        low.find("on") != std::string::npos ||
1088
        low.find("yes") != std::string::npos ||
1089
        low.find("1") != std::string::npos)
1090
        return true;
1091
 
1092
    return false;
1093
}
1094
 
1095
bool isFalse(const std::string &value)
1096
{
1097
    std::string v = value;
1098
    std::string low = toLower(v);
1099
 
1100
    if (low.find("false") != std::string::npos ||
1101
        low.find("off") != std::string::npos ||
1102
        low.find("no") != std::string::npos ||
1103
        low.find("0") != std::string::npos)
1104
        return true;
1105
 
1106
    return false;
1107
}
1108
 
1109
bool isNumeric(const std::string &str, bool blank)
1110
{
1111
    std::string::const_iterator iter;
1112
 
1113
    for (iter = str.begin(); iter != str.end(); ++iter)
1114
    {
1115
        if (*iter < '0' || *iter > '9')
1116
        {
1117
            if (blank && *iter == ' ')
1118
                continue;
1119
 
1120
            return false;
1121
        }
1122
    }
1123
 
1124
    return true;
1125
}
260 andreas 1126
 
1127
bool isBigEndian()
1128
{
1129
    union
1130
    {
1131
        uint32_t i;
1132
        char c[4];
1133
    } bint = {0x01020304};
1134
 
1135
    return bint.c[0] == 1;
1136
}
271 andreas 1137
 
1138
std::string handleToString(ulong handle)
1139
{
1140
    ulong part1 = (handle >> 16) & 0x0000ffff;
1141
    ulong part2 = handle & 0x0000ffff;
1142
    return std::to_string(part1)+":"+std::to_string(part2);
1143
}
297 andreas 1144
 
1145
ulong extractHandle(const std::string& obname)
1146
{
1147
    size_t pos = obname.rfind("_");
1148
 
1149
    if (pos == std::string::npos)
1150
        return 0;
1151
 
1152
    std::string part = obname.substr(pos + 1);
1153
    ulong handle = 0;
1154
 
1155
    if ((pos = part.find(":")) != std::string::npos)
1156
    {
1157
        std::string slt = part.substr(0, pos);
1158
        std::string srt = part.substr(pos + 1);
1159
        ulong lt = atoi(slt.c_str());
1160
        ulong rt = atoi(srt.c_str());
1161
        handle = ((lt << 16) & 0xffff0000) | (rt & 0x0000ffff);
1162
    }
1163
 
1164
    return handle;
1165
}