Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
474 andreas 2
 * Copyright (C) 2020 to 2024 by Andreas Theofilu <andreas@theosys.at>
446 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
#ifndef __TRESOURCES_H__
20
#define __TRESOURCES_H__
21
 
22
#include <deque>
23
#include <vector>
24
#include <string>
25
#include <iostream>
26
#include <stdexcept>
27
#include <type_traits>
28
#include <locale>
29
#include <codecvt>
30
#include <cstdint>
31
#include <clocale>
32
#include <iomanip>
33
#include <sstream>
34
#include <type_traits>
35
 
36
#ifdef __MACH__
37
#include <skia/core/SkImage.h>
38
#include <skia/core/SkString.h>
39
#include <skia/core/SkData.h>
40
#include <skia/core/SkColor.h>
41
#else
42
#include <include/core/SkImage.h>
43
#include <include/core/SkString.h>
44
#include <include/core/SkData.h>
45
#include <include/core/SkColor.h>
46
#endif
47
 
48
//#include "terror.h"
49
 
50
#ifndef ulong
51
typedef unsigned long ulong;
52
#endif
53
 
54
class SkBitmap;
55
class SkData;
56
class SkStreamAsset;
57
class SkFont;
58
class SkTypeface;
59
 
60
typedef enum _RESOURCE_TYPE
61
{
62
    RESTYPE_UNKNOWN,
63
    RESTYPE_IMAGE,
64
    RESTYPE_SYSIMAGE,
65
    RESTYPE_FONT,
66
    RESTYPE_SYSFONT,
67
    RESTYPE_CURSOR,
68
    RESTYPE_SYSCURSOR,
69
    RESTYPE_BORDER,
70
    RESTYPE_SYSBORDER,
71
    RESTYPE_SLIDER,
72
    RESTYPE_SYSSLIDER
73
}_RESOURCE_TYPE;
74
 
75
SkString GetResourcePath(const char* resource = "", _RESOURCE_TYPE rs = RESTYPE_IMAGE);
76
 
77
bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst);
78
 
79
sk_sp<SkData> GetResourceAsData(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
80
 
81
inline bool GetResourceAsBitmap(const char* resource, SkBitmap* dst)
82
{
83
    return DecodeDataToBitmap(GetResourceAsData(resource), dst);
84
}
85
 
86
inline sk_sp<SkImage> GetResourceAsImage(const char* resource)
87
{
88
    return SkImages::DeferredFromEncodedData(GetResourceAsData(resource));
89
}
90
 
91
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, _RESOURCE_TYPE rs = RESTYPE_IMAGE);
92
 
93
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex = 0, _RESOURCE_TYPE rs = RESTYPE_FONT);
94
 
95
sk_sp<SkData> readImage(const std::string& fname);
96
SkBitmap *allocPixels(int width, int height, SkBitmap *bm);
97
SkColor reverseColor(const SkColor& col);
98
 
99
std::string& toLower(std::string& str);
100
std::string& toUpper(std::string& str);
101
std::vector<std::string> StrSplit(const std::string& str, const std::string& seps, const bool trimEmpty=false);
102
std::string UTF8ToCp1250(const std::string& str);
103
std::string cp1250ToUTF8(const std::string& str);
104
std::string latin1ToUTF8(const std::string& str);
105
std::string intToString(int num);
106
std::string ReplaceString(const std::string subject, const std::string& search, const std::string& replace);
107
 
108
void *renew(char **mem, size_t old_size, size_t new_size);
109
std::vector<std::string> splitLine(const std::string& str, bool multiline=false);
110
std::vector<std::string> splitLine(const std::string& str, int width, int height, SkFont& font, SkPaint& paint);
111
bool isHex(int c);
112
int strCaseCompare(const std::string& str1, const std::string& str2);
113
std::string fillString(int c, int len);
114
bool isUTF8(const std::string& str);
115
size_t utf8Strlen(const std::string& str);
116
uint16_t getUint16(const unsigned char *p, bool big_endian=false);
117
uint32_t getUint32(const unsigned char *p, bool big_endian=false);
474 andreas 118
unsigned char *uint16ToBytes(uint16_t num, unsigned char *bytes=nullptr);
119
unsigned char *uint32ToBytes(uint16_t num, unsigned char *bytes=nullptr);
446 andreas 120
std::string UnicodeToUTF8(const std::wstring& ws);
121
bool endsWith (const std::string &src, const std::string &end);
122
bool startsWith (const std::string &src, const std::string &start);
123
std::string dirName (const std::string &path);
124
std::string baseName (const std::string &path);
125
char *strnstr(const char *haystack, const char *needle, size_t len);
126
std::string getCommand(const std::string& fullCmd);
127
bool StrContains(const std::string& str, const std::string& part);
128
bool isTrue(const std::string& value);
129
bool isFalse(const std::string& value);
130
bool isNumeric(const std::string& str, bool blank=false);
131
bool isBigEndian();
132
std::string handleToString(ulong handle);
133
ulong extractHandle(const std::string& obname);
134
uint32_t createButtonID(int type, int ap, int ad, int cp, int ch, int lp=-1, int lv=-1);
135
 
136
/**
137
 * @brief Converts a numeric value of any type into a string.
138
 * This template converts a numeric value of any type into a string with
139
 * hexadezimal representation. The numer of digits is always equal. If
140
 * necessary a 0 is prepended to the string.
141
 * If the type is double or float the result is undefined!
142
 *
143
 * @param i     A numeric value of any type.
144
 *
145
 * @return Returns the hexadezimal string representation of the numeric value.
146
 */
147
template<typename T>
148
inline std::string numberToHex(T i)
149
{
150
    std::stringstream stream;
151
    stream  << std::setfill ('0') << std::setw(sizeof(T)*2)
152
    << std::hex << i;
153
    return stream.str();
154
}
155
 
156
static inline std::string &ltrim(std::string &s)
157
{
158
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
159
    return s;
160
}
161
 
162
static inline std::string &rtrim(std::string &str)
163
{
164
    str.erase(str.find_last_not_of(" \n\r\t")+1);
165
    return str;
166
}
167
 
168
static inline std::string& trim(std::string& str) { return ltrim(rtrim(str)); }
169
 
170
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: " + std::string(msg)); }
171
#define ASSERT(cond) ASSERT_MSG(cond, "")
172
 
173
class CharConvert
174
{
175
    public:
176
        template <typename U8StrT = std::string>
177
        inline static U8StrT Utf32To8(std::u32string const & s)
178
        {
179
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
180
            typedef typename U8StrT::value_type VT;
181
            typedef uint8_t u8;
182
            U8StrT r;
183
 
184
            for (auto c: s)
185
            {
186
                size_t nby = c <= 0x7FU ? 1 : c <= 0x7FFU ? 2 : c <= 0xFFFFU ? 3 : c <= 0x1FFFFFU ? 4 : c <= 0x3FFFFFFU ? 5 : c <= 0x7FFFFFFFU ? 6 : 7;
187
                r.push_back(VT(
188
                    nby <= 1 ? u8(c) : (
189
                        (u8(0xFFU) << (8 - nby)) |
190
                        u8(c >> (6 * (nby - 1)))
191
                    )
192
                ));
193
 
194
                for (size_t i = 1; i < nby; ++i)
195
                    r.push_back(VT(u8(0x80U | (u8(0x3FU) & u8(c >> (6 * (nby - 1 - i)))))));
196
            }
197
 
198
            return r;
199
        }
200
 
201
        template <typename U8StrT>
202
        inline static std::u32string Utf8To32(U8StrT const & s)
203
        {
204
            static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
205
            typedef uint8_t u8;
206
            std::u32string r;
207
            auto it = (u8 const *)s.c_str(), end = (u8 const *)(s.c_str() + s.length());
208
 
209
            while (it < end)
210
            {
211
                char32_t c = 0;
212
                if (*it <= 0x7FU)
213
                {
214
                    c = *it;
215
                    ++it;
216
                }
217
                else
218
                {
219
                    ASSERT((*it & 0xC0U) == 0xC0U);
220
                    size_t nby = 0;
221
 
222
                    for (u8 b = *it; (b & 0x80U) != 0; b <<= 1, ++nby) { (void)0; }
223
                    ASSERT(nby <= 7);
224
                    ASSERT((size_t)(end - it) >= nby);
225
                    c = *it & (u8(0xFFU) >> (nby + 1));
226
 
227
                    for (size_t i = 1; i < nby; ++i)
228
                    {
229
                        ASSERT((it[i] & 0xC0U) == 0x80U);
230
                        c = (c << 6) | (it[i] & 0x3FU);
231
                    }
232
 
233
                    it += nby;
234
                }
235
 
236
                r.push_back(c);
237
            }
238
 
239
            return r;
240
        }
241
 
242
 
243
        template <typename U16StrT = std::u16string>
244
        inline static U16StrT Utf32To16(std::u32string const & s)
245
        {
246
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
247
            typedef typename U16StrT::value_type VT;
248
            typedef uint16_t u16;
249
            U16StrT r;
250
 
251
            for (auto c: s)
252
            {
253
                if (c <= 0xFFFFU)
254
                    r.push_back(VT(c));
255
                else
256
                {
257
                    ASSERT(c <= 0x10FFFFU);
258
                    c -= 0x10000U;
259
                    r.push_back(VT(u16(0xD800U | ((c >> 10) & 0x3FFU))));
260
                    r.push_back(VT(u16(0xDC00U | (c & 0x3FFU))));
261
                }
262
            }
263
 
264
            return r;
265
        }
266
 
267
        template <typename U16StrT>
268
        inline static std::u32string Utf16To32(U16StrT const & s)
269
        {
270
            static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
271
            typedef uint16_t u16;
272
            std::u32string r;
273
            auto it = (u16 const *)s.c_str(), end = (u16 const *)(s.c_str() + s.length());
274
 
275
            while (it < end)
276
            {
277
                char32_t c = 0;
278
 
279
                if (*it < 0xD800U || *it > 0xDFFFU)
280
                {
281
                    c = *it;
282
                    ++it;
283
                }
284
                else if (*it >= 0xDC00U)
285
                {
286
                    ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
287
                }
288
                else
289
                {
290
                    ASSERT(end - it >= 2);
291
                    c = (*it & 0x3FFU) << 10;
292
 
293
                    if ((it[1] < 0xDC00U) || (it[1] > 0xDFFFU))
294
                    {
295
                        ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
296
                    }
297
                    else
298
                    {
299
                        c |= it[1] & 0x3FFU;
300
                        c += 0x10000U;
301
                    }
302
 
303
                    it += 2;
304
                }
305
 
306
                r.push_back(c);
307
            }
308
 
309
            return r;
310
        }
311
 
312
        template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
313
        template <typename StrT> struct UtfHelper<StrT, 1>
314
        {
315
            inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
316
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
317
        };
318
 
319
        template <typename StrT> struct UtfHelper<StrT, 2>
320
        {
321
            inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
322
            inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
323
        };
324
 
325
        template <typename StrT> struct UtfHelper<StrT, 4>
326
        {
327
            inline static std::u32string UtfTo32(StrT const & s)
328
            {
329
                return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
330
            }
331
 
332
            inline static StrT UtfFrom32(std::u32string const & s)
333
            {
334
                return StrT((typename StrT::value_type const *)(s.c_str()),
335
                            (typename StrT::value_type const *)(s.c_str() + s.length()));
336
            }
337
        };
338
 
339
        template <typename StrT> inline static std::u32string UtfTo32(StrT const & s)
340
        {
341
            return UtfHelper<StrT>::UtfTo32(s);
342
        }
343
 
344
        template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s)
345
        {
346
            return UtfHelper<StrT>::UtfFrom32(s);
347
        }
348
 
349
        template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s)
350
        {
351
            return UtfFrom32<StrToT>(UtfTo32(s));
352
        }
353
 
354
    private:
355
        CharConvert() {}    // Never call this!
356
};
357
 
358
#endif  // __TRESOURCES_H__