summaryrefslogtreecommitdiff
path: root/tools/localize/file_utils.cpp
blob: 943170912de019712a439df935621a10e87e06cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#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, &region)) {
        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;
}