summaryrefslogtreecommitdiffstats
path: root/tools/localize/file_utils.cpp
blob: 84081f5bed20869df5e26d75d6fdf5c10e787163 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "file_utils.h"
#include "Perforce.h"
#include <utils/String8.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <cstdio>
#include "log.h"

using namespace android;
using namespace std;

static string
parent_dir(const string& path)
{
    return string(String8(path.c_str()).getPathDir().string());
}

static int
mkdirs(const char* last)
{
    String8 dest;
    const char* s = last-1;
    int err;
    do {
        s++;
        if (s > last && (*s == '.' || *s == 0)) {
            String8 part(last, s-last);
            dest.appendPath(part);
#ifdef HAVE_MS_C_RUNTIME
            err = _mkdir(dest.string());
#else                    
            err = mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
#endif                    
            if (err != 0) {
                return err;
            }
            last = s+1;
        }
    } while (*s);
    return 0;
}

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;
}