This commit is contained in:
Ken Van Hoeylandt 2025-08-24 01:03:50 +02:00
parent f28c76eb53
commit 9fd344be1b
5 changed files with 51 additions and 27 deletions

View File

@ -12,6 +12,7 @@
- Create more unit tests for `tactility-core` - Create more unit tests for `tactility-core`
- Make a URL handler. Use it for handling local files. Match file types with apps. - 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` - 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 ## Lower Priority

View File

@ -31,6 +31,7 @@ bool loadPropertiesFile(const std::string& filePath, std::function<void(const st
const std::string input_string = input_start; const std::string input_string = input_start;
uint16_t line_count = 0; uint16_t line_count = 0;
// TODO: Rewrite to use file::readLines()
string::split(input_string, "\n", [&line_count, &filePath, &callback](auto token) { string::split(input_string, "\n", [&line_count, &filePath, &callback](auto token) {
line_count++; line_count++;
std::string key, value; std::string key, value;

View File

@ -3,6 +3,7 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <Tactility/file/File.h>
namespace tt::i18n { namespace tt::i18n {
@ -28,7 +29,7 @@ public:
static std::string getDesiredLocale() { static std::string getDesiredLocale() {
// TODO: Implement locale settings // TODO: Implement locale settings
return "nl-NL"; return "en-GB";
} }
static std::string getFallbackLocale() { static std::string getFallbackLocale() {
@ -36,42 +37,37 @@ static std::string getFallbackLocale() {
return "en-GB"; return "en-GB";
} }
static FILE* openI18nFile(const std::string& path) { static std::string getI18nDataFilePath(const std::string& path) {
auto locale = getDesiredLocale(); auto locale = getDesiredLocale();
auto desired_file_path = std::format("{}/{}.i18n", path, locale); auto desired_file_path = std::format("{}/{}.i18n", path, locale);
auto* file = fopen(desired_file_path.c_str(), "r"); if (file::isFile(desired_file_path)) {
if (file == nullptr) { return desired_file_path;
auto fallback_locale = getFallbackLocale(); } else {
TT_LOG_W(TAG, "Translations not found for %s at %s", locale.c_str(), desired_file_path.c_str()); TT_LOG_W(TAG, "Translations not found for %s at %s", locale.c_str(), desired_file_path.c_str());
}
auto fallback_locale = getFallbackLocale();
auto fallback_file_path = std::format("{}/{}.i18n", path, getFallbackLocale()); auto fallback_file_path = std::format("{}/{}.i18n", path, getFallbackLocale());
file = fopen(fallback_file_path.c_str(), "r"); if (file::isFile(fallback_file_path)) {
if (file == nullptr) { 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()); TT_LOG_W(TAG, "Fallback translations not found for %s at %s", fallback_locale.c_str(), fallback_file_path.c_str());
return "";
} }
} }
return file;
}
std::shared_ptr<IndexedText> loadIndexedText(const std::string& path) { std::shared_ptr<IndexedText> loadIndexedText(const std::string& path) {
std::vector<std::string> data; std::vector<std::string> data;
auto file_path = getI18nDataFilePath(path);
if (file_path.empty()) {
return nullptr;
}
// We lock on folder level, because file is TBD // We lock on folder level, because file is TBD
file::withLock<void>(path, [&path, &data] { file::withLock<void>(path, [&file_path, &data] {
auto* file = openI18nFile(path); file::readLines(file_path, true, [&data](const char* line) {
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); data.push_back(line);
} });
fclose(file);
}
}); });
if (data.empty()) { if (data.empty()) {

View File

@ -110,4 +110,6 @@ int scandir(
ScandirSort _Nullable sort ScandirSort _Nullable sort
); );
bool readLines(const std::string& filePath, bool stripNewLine, std::function<void(const char* line)> callback);
} }

View File

@ -222,4 +222,28 @@ bool isDirectory(const std::string& path) {
return stat(path.c_str(), &stat_result) == 0 && S_ISDIR(stat_result.st_mode); return stat(path.c_str(), &stat_result) == 0 && S_ISDIR(stat_result.st_mode);
} }
bool readLines(const std::string& filepath, bool stripNewLine, std::function<void(const char* line)> 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;
}
} }