Mercurial > libanimone
annotate src/util.cc @ 32:93224b26a0ee default tip
player: efforts towards C-ization
author | Paper <paper@tflc.us> |
---|---|
date | Mon, 10 Feb 2025 19:17:29 -0500 |
parents | a76e55e098d1 |
children |
rev | line source |
---|---|
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
1 #include "animone/util.h" |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
2 |
30 | 3 #include <string.h> |
4 | |
5 #include <regex> // FIXME use a C library for this | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
6 |
30 | 7 int animone_internal_util_ReadFile(const char *path, char **data, size_t *size) |
8 { | |
9 // whatever | |
10 size_t sz_local; | |
11 if (!size) | |
12 size = &sz_local; | |
13 | |
14 FILE *f = fopen(path, "r"); | |
15 if (!f) | |
16 return 0; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
17 |
30 | 18 fseek(f, 0, SEEK_END); |
19 long end = ftell(f); | |
20 if (end < 0) | |
21 return 0; | |
22 fseek(f, 0, SEEK_SET); | |
23 | |
24 *size = end; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
25 |
30 | 26 if (data) { |
27 // add a NUL terminator anyway | |
28 *data = (char *)malloc(end + 1); | |
29 if (!*data) { | |
30 *size = 0; | |
31 return 0; | |
32 } | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
33 |
30 | 34 *size = fread(*data, 1, *size, f); |
35 (*data)[*size] = '\0'; | |
36 } | |
37 | |
38 return 1; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
39 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
40 |
30 | 41 static inline unsigned char _animone_internal_tolower(unsigned char c) |
42 { | |
43 return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
44 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
45 |
30 | 46 /* this is, in essence, strcasecmp. */ |
47 int animone_internal_util_EqualStrings(const char *str1, const char *str2) | |
48 { | |
49 unsigned char c1, c2; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
50 |
30 | 51 do { |
52 c1 = _animone_internal_tolower(*str1++); | |
53 c2 = _animone_internal_tolower(*str2++); | |
54 } while (c1 == c2 && c1); | |
55 | |
56 return !!(c1 - c2); | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
57 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
58 |
30 | 59 char *animone_internal_Stem(const char *filename) |
60 { | |
61 const char *ptr = strrchr(filename, '.'); | |
62 if (!ptr) | |
63 return NULL; | |
64 | |
65 size_t len = ptr - filename; | |
66 | |
67 char *stem = (char *)malloc(len + 1); | |
68 if (!stem) | |
69 return NULL; | |
70 | |
71 memcpy(stem, filename, len); | |
72 stem[len] = '\0'; | |
73 | |
74 return stem; | |
75 } | |
76 | |
77 int animone_internal_util_CheckPattern(const char *pattern, const char *str) | |
78 { | |
79 switch (*pattern) { | |
80 case '\0': return 0; | |
81 case '^': | |
82 // FIXME: Convert to C | |
83 if (std::regex_match(std::string(str), std::regex(pattern))) | |
84 return 1; | |
85 default: | |
86 break; | |
87 } | |
88 | |
89 return animone_internal_util_EqualStrings(pattern, str); | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
90 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
91 |
30 | 92 /* Modifies `str` inplace */ |
93 int animone_internal_util_TrimLeft(char *str, const char* chars) | |
94 { | |
95 if (!str || !*str) | |
96 return 0; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
97 |
30 | 98 const size_t found = strcspn(str, chars); |
99 const size_t len = strlen(str); | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
100 |
30 | 101 if (found == len) |
102 return 1; // nothing to do | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
103 |
30 | 104 memmove(str, str + found, len - found); |
105 str[len - found] = '\0'; | |
106 | |
107 return 1; | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
108 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
109 |
30 | 110 // reverse version of strcspn |
111 static inline size_t _animone_internal_strrcspn(const char *str1, const char *str2) | |
112 { | |
113 const size_t str1len = strlen(str1); | |
114 ptrdiff_t i; /* FIXME: this should be using size_t */ | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
115 |
30 | 116 for (i = str1len - 1; i >= 0; i--) { |
117 size_t found = strcspn(str1 + i, str2); | |
118 if (found != str1len - i) | |
119 return i; | |
120 } | |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
121 |
30 | 122 return str1len; |
14
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
123 } |
27b988a1048c
*: convert all files CRLF -> LF
Paper <paper@paper.us.eu.org>
parents:
0
diff
changeset
|
124 |
30 | 125 int animone_internal_util_TrimRight(char *str, const char* chars) |
126 { | |
127 if (!str || !*str) | |
128 return 0; | |
129 | |
130 // We can get away without really moving memory around here. | |
131 // The old version simply used std::string::find_last_not_of, | |
132 // which I'm fairly sure isn't the intention of this function | |
133 // (for example find_last_not_of("gb ") returns "gb "). | |
134 const size_t found = _animone_internal_strrcspn(str, chars); | |
135 str[found] = '\0'; | |
136 | |
137 return 1; | |
138 } |