1 /*
2  * This software is licensed under the terms of the MIT License.
3  * See COPYING for further information.
4  * ---
5  * Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
6  * Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
7  */
8 
9 #ifndef IGUARD_util_stringops_h
10 #define IGUARD_util_stringops_h
11 
12 #include "taisei.h"
13 
14 #include <time.h>
15 #include <SDL.h>
16 
17 #include "systime.h"
18 
19 #define UNICODE_UNKNOWN 0xFFFD
20 #define UNICODE_BOM_NATIVE  0xFEFF
21 #define UNICODE_BOM_SWAPPED 0xFFFE
22 #define UNICODE_ELLIPSIS 0x2026
23 
24 #undef strlcat
25 #define strlcat SDL_strlcat
26 
27 #undef strlcpy
28 #define strlcpy SDL_strlcpy
29 
30 #undef strdup
31 #define strdup _ts_strdup
strdup(const char * str)32 INLINE attr_returns_allocated attr_nonnull(1) char *strdup(const char *str) {
33 	size_t sz = strlen(str) + 1;
34 	return memcpy(malloc(sz), str, sz);
35 }
36 
37 #undef strtok_r
38 #define strtok_r _ts_strtok_r
39 
40 #undef strcasecmp
41 #define strcasecmp SDL_strcasecmp
42 
43 bool strendswith(const char *s, const char *e) attr_pure;
44 bool strstartswith(const char *s, const char *p) attr_pure;
45 bool strendswith_any(const char *s, const char **earray) attr_pure;
46 bool strstartswith_any(const char *s, const char **earray) attr_pure;
47 void stralloc(char **dest, const char *src);
48 char* strjoin(const char *first, ...) attr_sentinel attr_returns_allocated;
49 char* vstrfmt(const char *fmt, va_list args) attr_returns_allocated;
50 char* strfmt(const char *fmt, ...) attr_printf(1,  2) attr_returns_allocated;
51 void strip_trailing_slashes(char *buf);
52 char* strtok_r(char *str, const char *delim, char **nextp);
53 char* strappend(char **dst, char *src);
54 char* strftimealloc(const char *fmt, const struct tm *timeinfo) attr_returns_allocated;
55 void expand_escape_sequences(char *str);
56 
57 uint32_t* ucs4chr(const uint32_t *ucs4, uint32_t chr);
58 size_t ucs4len(const uint32_t *ucs4);
59 
60 void utf8_to_ucs4(const char *utf8, size_t bufsize, uint32_t buf[bufsize]) attr_nonnull(1, 3);
61 uint32_t* utf8_to_ucs4_alloc(const char *utf8) attr_nonnull(1) attr_returns_allocated attr_nonnull(1);
62 
63 void ucs4_to_utf8(const uint32_t *ucs4, size_t bufsize, char buf[bufsize]) attr_nonnull(1, 3);
64 char* ucs4_to_utf8_alloc(const uint32_t *ucs4) attr_nonnull(1) attr_returns_allocated attr_nonnull(1);
65 
66 uint32_t utf8_getch(const char **src) attr_nonnull(1);
67 
68 void format_huge_num(uint digits, uint64_t num, size_t bufsize, char *buf);
69 void hexdigest(uint8_t *input, size_t input_size, char *output, size_t output_size);
70 
71 #define FILENAME_TIMESTAMP_MIN_BUF_SIZE 23
72 size_t filename_timestamp(char *buf, size_t buf_size, const SystemTime time) attr_nonnull(1);
73 
74 #endif // IGUARD_util_stringops_h
75