Mercurial > libanimone
diff 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 |
line wrap: on
line diff
--- a/include/animone/util.h Sun Feb 09 23:15:55 2025 -0500 +++ b/include/animone/util.h Sun Feb 09 23:18:57 2025 -0500 @@ -1,25 +1,89 @@ #ifndef ANIMONE_ANIMONE_UTIL_H_ #define ANIMONE_ANIMONE_UTIL_H_ -#include <sstream> -#include <string> +#include <stdint.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" { +#endif -namespace animone::internal::util { +int animone_internal_util_ReadFile(const char *path, char **data, size_t *size); +int animone_internal_util_EqualStrings(const char *str1, const char *str2); +char *animone_internal_util_Stem(const char *filename); +int animone_internal_util_CheckPattern(const char *pattern, const char *str); +int animone_internal_util_TrimLeft(char *str, const char* chars); +int animone_internal_util_TrimRight(char *str, const char* chars); + +intmax_t animone_internal_util_StringToInt(const char *ptr, intmax_t def); + +#ifdef __cplusplus +} -bool ReadFile(const std::string& path, std::string& data); -bool EqualStrings(const std::string& str1, const std::string& str2); -bool Stem(const std::string& filename, std::string& stem); -bool CheckPattern(const std::string& pattern, const std::string& str); -bool TrimLeft(std::string& str, const char* chars); -bool TrimRight(std::string& str, const char* chars); +#include <string> +#include <cstdlib> + +namespace animone { +namespace internal { +namespace util { -template<typename T = int, std::enable_if_t<std::is_integral<T>::value, bool> = true> -T StringToInt(const std::string& str, T def = 0) { - std::istringstream s(str); - s >> std::noboolalpha >> def; - return def; +// Compatibility bindings, to keep the existing C++ code working +inline bool ReadFile(const std::string &path, std::string &data) +{ + char *data_c; + size_t size_c; + + int x = ::animone_internal_util_ReadFile(path.c_str(), &data_c, &size_c); + + data.assign(data_c, size_c); + + std::free(data_c); + + return static_cast<bool>(x); } -} // namespace animone::internal::util +inline bool EqualStrings(const std::string &str1, const std::string &str2) +{ + return static_cast<bool>(::animone_internal_util_EqualStrings(str1.c_str(), str2.c_str())); +} + +inline bool Stem(const std::string &filename, std::string &stem) +{ + char *stem_c = ::animone_internal_util_Stem(filename.c_str()); + if (!stem_c) + return 0; + + stem.assign(stem_c); + + std::free(stem_c); + + return 1; +} + +inline bool CheckPattern(const std::string &pattern, const std::string &str) +{ + return static_cast<bool>(::animone_internal_util_CheckPattern(pattern.c_str(), str.c_str())); +} + +inline bool TrimLeft(std::string &str, const char *chars) +{ + return ::animone_internal_util_TrimLeft(str.data(), chars); +} + +inline bool TrimRight(std::string &str, const char* chars) +{ + return ::animone_internal_util_TrimRight(str.data(), chars); +} + +inline intmax_t StringToInt(const std::string &ptr, intmax_t def) +{ + return ::animone_internal_util_StringToInt(ptr.c_str(), def); +} + +} // namespace util +} // namespace internal +} // namespace animone + +#endif #endif // ANIMONE_ANIMONE_UTIL_H_