Mercurial > libanimone
comparison include/animone/util.h @ 30:a76e55e098d1
util: rewrite functions in C-ish
there are C++ bindings still put in place. the code should be valid
C, except for the use of <regex>, which ought to go anyway. eventually
I'll actually *test* this stuff aside from the TrimRight crap
author | Paper <paper@tflc.us> |
---|---|
date | Sun, 09 Feb 2025 23:18:57 -0500 |
parents | 27b988a1048c |
children |
comparison
equal
deleted
inserted
replaced
29:40fd3776ce9b | 30:a76e55e098d1 |
---|---|
1 #ifndef ANIMONE_ANIMONE_UTIL_H_ | 1 #ifndef ANIMONE_ANIMONE_UTIL_H_ |
2 #define ANIMONE_ANIMONE_UTIL_H_ | 2 #define ANIMONE_ANIMONE_UTIL_H_ |
3 | 3 |
4 #include <sstream> | 4 #include <stdint.h> |
5 #include <string> | 5 #include <stddef.h> |
6 | 6 |
7 namespace animone::internal::util { | 7 #ifdef __cplusplus |
8 extern "C" { | |
9 #endif | |
8 | 10 |
9 bool ReadFile(const std::string& path, std::string& data); | 11 int animone_internal_util_ReadFile(const char *path, char **data, size_t *size); |
10 bool EqualStrings(const std::string& str1, const std::string& str2); | 12 int animone_internal_util_EqualStrings(const char *str1, const char *str2); |
11 bool Stem(const std::string& filename, std::string& stem); | 13 char *animone_internal_util_Stem(const char *filename); |
12 bool CheckPattern(const std::string& pattern, const std::string& str); | 14 int animone_internal_util_CheckPattern(const char *pattern, const char *str); |
13 bool TrimLeft(std::string& str, const char* chars); | 15 int animone_internal_util_TrimLeft(char *str, const char* chars); |
14 bool TrimRight(std::string& str, const char* chars); | 16 int animone_internal_util_TrimRight(char *str, const char* chars); |
15 | 17 |
16 template<typename T = int, std::enable_if_t<std::is_integral<T>::value, bool> = true> | 18 intmax_t animone_internal_util_StringToInt(const char *ptr, intmax_t def); |
17 T StringToInt(const std::string& str, T def = 0) { | 19 |
18 std::istringstream s(str); | 20 #ifdef __cplusplus |
19 s >> std::noboolalpha >> def; | |
20 return def; | |
21 } | 21 } |
22 | 22 |
23 } // namespace animone::internal::util | 23 #include <string> |
24 #include <cstdlib> | |
25 | |
26 namespace animone { | |
27 namespace internal { | |
28 namespace util { | |
29 | |
30 // Compatibility bindings, to keep the existing C++ code working | |
31 inline bool ReadFile(const std::string &path, std::string &data) | |
32 { | |
33 char *data_c; | |
34 size_t size_c; | |
35 | |
36 int x = ::animone_internal_util_ReadFile(path.c_str(), &data_c, &size_c); | |
37 | |
38 data.assign(data_c, size_c); | |
39 | |
40 std::free(data_c); | |
41 | |
42 return static_cast<bool>(x); | |
43 } | |
44 | |
45 inline bool EqualStrings(const std::string &str1, const std::string &str2) | |
46 { | |
47 return static_cast<bool>(::animone_internal_util_EqualStrings(str1.c_str(), str2.c_str())); | |
48 } | |
49 | |
50 inline bool Stem(const std::string &filename, std::string &stem) | |
51 { | |
52 char *stem_c = ::animone_internal_util_Stem(filename.c_str()); | |
53 if (!stem_c) | |
54 return 0; | |
55 | |
56 stem.assign(stem_c); | |
57 | |
58 std::free(stem_c); | |
59 | |
60 return 1; | |
61 } | |
62 | |
63 inline bool CheckPattern(const std::string &pattern, const std::string &str) | |
64 { | |
65 return static_cast<bool>(::animone_internal_util_CheckPattern(pattern.c_str(), str.c_str())); | |
66 } | |
67 | |
68 inline bool TrimLeft(std::string &str, const char *chars) | |
69 { | |
70 return ::animone_internal_util_TrimLeft(str.data(), chars); | |
71 } | |
72 | |
73 inline bool TrimRight(std::string &str, const char* chars) | |
74 { | |
75 return ::animone_internal_util_TrimRight(str.data(), chars); | |
76 } | |
77 | |
78 inline intmax_t StringToInt(const std::string &ptr, intmax_t def) | |
79 { | |
80 return ::animone_internal_util_StringToInt(ptr.c_str(), def); | |
81 } | |
82 | |
83 } // namespace util | |
84 } // namespace internal | |
85 } // namespace animone | |
86 | |
87 #endif | |
24 | 88 |
25 #endif // ANIMONE_ANIMONE_UTIL_H_ | 89 #endif // ANIMONE_ANIMONE_UTIL_H_ |