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