diff options
Diffstat (limited to 'tools/localize/file_utils.cpp')
-rw-r--r-- | tools/localize/file_utils.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/tools/localize/file_utils.cpp b/tools/localize/file_utils.cpp new file mode 100644 index 0000000..bb82a9c --- /dev/null +++ b/tools/localize/file_utils.cpp @@ -0,0 +1,143 @@ +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include "file_utils.h" +#include "Perforce.h" +#include <sys/fcntl.h> +#include <sys/stat.h> +#include <errno.h> +#include <host/Directories.h> +#include "log.h" + +string +translated_file_name(const string& file, const string& locale) +{ + const char* str = file.c_str(); + const char* p = str + file.length(); + const char* rest = NULL; + const char* values = p; + + while (p > str) { + p--; + if (*p == '/') { + rest = values; + values = p; + if (0 == strncmp("values", values+1, rest-values-1)) { + break; + } + } + } + values++; + + string result(str, values-str); + result.append(values, rest-values); + + string language, region; + if (locale == "") { + language = ""; + region = ""; + } + else if (!split_locale(locale, &language, ®ion)) { + return ""; + } + + if (language != "") { + result += '-'; + result += language; + } + if (region != "") { + result += "-r"; + result += region; + } + + result += rest; + + return result; +} + +ValuesFile* +get_values_file(const string& filename, const Configuration& configuration, + int version, const string& versionString, bool printOnFailure) +{ + int err; + string text; + + log_printf("get_values_file filename=%s\n", filename.c_str()); + err = Perforce::GetFile(filename, versionString, &text, printOnFailure); + if (err != 0 || text == "") { + return NULL; + } + + ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version, + versionString); + if (result == NULL) { + fprintf(stderr, "unable to parse file: %s\n", filename.c_str()); + exit(1); + } + return result; +} + +ValuesFile* +get_local_values_file(const string& filename, const Configuration& configuration, + int version, const string& versionString, bool printOnFailure) +{ + int err; + string text; + char buf[2049]; + int fd; + ssize_t amt; + + fd = open(filename.c_str(), O_RDONLY); + if (fd == -1) { + fprintf(stderr, "unable to open file: %s\n", filename.c_str()); + return NULL; + } + + while ((amt = read(fd, buf, sizeof(buf)-1)) > 0) { + text.append(buf, amt); + } + + close(fd); + + if (text == "") { + return NULL; + } + + ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version, + versionString); + if (result == NULL) { + fprintf(stderr, "unable to parse file: %s\n", filename.c_str()); + exit(1); + } + return result; +} + +void +print_file_status(size_t j, size_t J, const string& message) +{ + printf("\r%s file %zd of %zd...", message.c_str(), j, J); + fflush(stdout); +} + +int +write_to_file(const string& filename, const string& text) +{ + mkdirs(parent_dir(filename).c_str()); + int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666); + if (fd < 0) { + fprintf(stderr, "unable to open file for write (%s): %s\n", strerror(errno), + filename.c_str()); + return -1; + } + + ssize_t amt = write(fd, text.c_str(), text.length()); + + close(fd); + + if (amt < 0) { + return amt; + } + return amt == (ssize_t)text.length() ? 0 : -1; +} + + |