Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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