blob: bb82a9c55f14260e07a33308a39daa1a18c91324 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include <string.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include "file_utils.h"
5#include "Perforce.h"
6#include <sys/fcntl.h>
7#include <sys/stat.h>
8#include <errno.h>
9#include <host/Directories.h>
10#include "log.h"
11
12string
13translated_file_name(const string& file, const string& locale)
14{
15 const char* str = file.c_str();
16 const char* p = str + file.length();
17 const char* rest = NULL;
18 const char* values = p;
19
20 while (p > str) {
21 p--;
22 if (*p == '/') {
23 rest = values;
24 values = p;
25 if (0 == strncmp("values", values+1, rest-values-1)) {
26 break;
27 }
28 }
29 }
30 values++;
31
32 string result(str, values-str);
33 result.append(values, rest-values);
34
35 string language, region;
36 if (locale == "") {
37 language = "";
38 region = "";
39 }
40 else if (!split_locale(locale, &language, &region)) {
41 return "";
42 }
43
44 if (language != "") {
45 result += '-';
46 result += language;
47 }
48 if (region != "") {
49 result += "-r";
50 result += region;
51 }
52
53 result += rest;
54
55 return result;
56}
57
58ValuesFile*
59get_values_file(const string& filename, const Configuration& configuration,
60 int version, const string& versionString, bool printOnFailure)
61{
62 int err;
63 string text;
64
65 log_printf("get_values_file filename=%s\n", filename.c_str());
66 err = Perforce::GetFile(filename, versionString, &text, printOnFailure);
67 if (err != 0 || text == "") {
68 return NULL;
69 }
70
71 ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version,
72 versionString);
73 if (result == NULL) {
74 fprintf(stderr, "unable to parse file: %s\n", filename.c_str());
75 exit(1);
76 }
77 return result;
78}
79
80ValuesFile*
81get_local_values_file(const string& filename, const Configuration& configuration,
82 int version, const string& versionString, bool printOnFailure)
83{
84 int err;
85 string text;
86 char buf[2049];
87 int fd;
88 ssize_t amt;
89
90 fd = open(filename.c_str(), O_RDONLY);
91 if (fd == -1) {
92 fprintf(stderr, "unable to open file: %s\n", filename.c_str());
93 return NULL;
94 }
95
96 while ((amt = read(fd, buf, sizeof(buf)-1)) > 0) {
97 text.append(buf, amt);
98 }
99
100 close(fd);
101
102 if (text == "") {
103 return NULL;
104 }
105
106 ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version,
107 versionString);
108 if (result == NULL) {
109 fprintf(stderr, "unable to parse file: %s\n", filename.c_str());
110 exit(1);
111 }
112 return result;
113}
114
115void
116print_file_status(size_t j, size_t J, const string& message)
117{
118 printf("\r%s file %zd of %zd...", message.c_str(), j, J);
119 fflush(stdout);
120}
121
122int
123write_to_file(const string& filename, const string& text)
124{
125 mkdirs(parent_dir(filename).c_str());
126 int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
127 if (fd < 0) {
128 fprintf(stderr, "unable to open file for write (%s): %s\n", strerror(errno),
129 filename.c_str());
130 return -1;
131 }
132
133 ssize_t amt = write(fd, text.c_str(), text.length());
134
135 close(fd);
136
137 if (amt < 0) {
138 return amt;
139 }
140 return amt == (ssize_t)text.length() ? 0 : -1;
141}
142
143