diff --git a/Documentation/ideas.md b/Documentation/ideas.md index 66419c9d..a5b6b370 100644 --- a/Documentation/ideas.md +++ b/Documentation/ideas.md @@ -12,6 +12,7 @@ - Create more unit tests for `tactility-core` - Make a URL handler. Use it for handling local files. Match file types with apps. - Fix Development service: when no SD card is present, the app fails to install. Consider installing to `/data` +- Refactor `PropertiesFile.cpp` to use `tt::file::readLines()` (see TODO in code) ## Lower Priority diff --git a/Tactility/Source/file/PropertiesFile.cpp b/Tactility/Source/file/PropertiesFile.cpp index 283fa903..088f44d8 100644 --- a/Tactility/Source/file/PropertiesFile.cpp +++ b/Tactility/Source/file/PropertiesFile.cpp @@ -31,6 +31,7 @@ bool loadPropertiesFile(const std::string& filePath, std::function #include +#include namespace tt::i18n { @@ -28,7 +29,7 @@ public: static std::string getDesiredLocale() { // TODO: Implement locale settings - return "nl-NL"; + return "en-GB"; } static std::string getFallbackLocale() { @@ -36,42 +37,37 @@ static std::string getFallbackLocale() { return "en-GB"; } -static FILE* openI18nFile(const std::string& path) { +static std::string getI18nDataFilePath(const std::string& path) { auto locale = getDesiredLocale(); auto desired_file_path = std::format("{}/{}.i18n", path, locale); - auto* file = fopen(desired_file_path.c_str(), "r"); - if (file == nullptr) { - auto fallback_locale = getFallbackLocale(); + if (file::isFile(desired_file_path)) { + return desired_file_path; + } else { TT_LOG_W(TAG, "Translations not found for %s at %s", locale.c_str(), desired_file_path.c_str()); - auto fallback_file_path = std::format("{}/{}.i18n", path, getFallbackLocale()); - file = fopen(fallback_file_path.c_str(), "r"); - if (file == nullptr) { - TT_LOG_W(TAG, "Fallback translations not found for %s at %s", fallback_locale.c_str(), fallback_file_path.c_str()); - } } - return file; + + auto fallback_locale = getFallbackLocale(); + auto fallback_file_path = std::format("{}/{}.i18n", path, getFallbackLocale()); + if (file::isFile(fallback_file_path)) { + return fallback_file_path; + } else { + TT_LOG_W(TAG, "Fallback translations not found for %s at %s", fallback_locale.c_str(), fallback_file_path.c_str()); + return ""; + } } std::shared_ptr loadIndexedText(const std::string& path) { std::vector data; + auto file_path = getI18nDataFilePath(path); + if (file_path.empty()) { + return nullptr; + } // We lock on folder level, because file is TBD - file::withLock(path, [&path, &data] { - auto* file = openI18nFile(path); - if (file != nullptr) { - char line[1024]; - // TODO: move to file::readLines(filePath, skipEndline, callback) - while (fgets(line, sizeof(line), file) != nullptr) { - // Strip newline - size_t line_length = strlen(line); - if (line_length > 0 && line[line_length - 1] == '\n') { - line[line_length - 1] = '\0'; - } - // Publish - data.push_back(line); - } - fclose(file); - } + file::withLock(path, [&file_path, &data] { + file::readLines(file_path, true, [&data](const char* line) { + data.push_back(line); + }); }); if (data.empty()) { diff --git a/TactilityCore/Include/Tactility/file/File.h b/TactilityCore/Include/Tactility/file/File.h index 76808394..a96a9f0c 100644 --- a/TactilityCore/Include/Tactility/file/File.h +++ b/TactilityCore/Include/Tactility/file/File.h @@ -110,4 +110,6 @@ int scandir( ScandirSort _Nullable sort ); +bool readLines(const std::string& filePath, bool stripNewLine, std::function callback); + } diff --git a/TactilityCore/Source/file/File.cpp b/TactilityCore/Source/file/File.cpp index 3d778e61..b79fde56 100644 --- a/TactilityCore/Source/file/File.cpp +++ b/TactilityCore/Source/file/File.cpp @@ -222,4 +222,28 @@ bool isDirectory(const std::string& path) { return stat(path.c_str(), &stat_result) == 0 && S_ISDIR(stat_result.st_mode); } +bool readLines(const std::string& filepath, bool stripNewLine, std::function callback) { + auto* file = fopen(filepath.c_str(), "r"); + if (file == nullptr) { + return false; + } + + char line[1024]; + + while (fgets(line, sizeof(line), file) != nullptr) { + // Strip newline + if (stripNewLine) { + size_t line_length = strlen(line); + if (line_length > 0 && line[line_length - 1] == '\n') { + line[line_length - 1] = '\0'; + } + } + // Publish + callback(line); + } + + fclose(file); + return true; +} + }