blob: 8792b9e54d3d77350347ddb1c789c5c972286379 [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"
Brian Swetland24bd82a2009-06-04 14:04:53 -07006#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007#include <sys/fcntl.h>
8#include <sys/stat.h>
9#include <errno.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010#include "log.h"
11
Brian Swetland24bd82a2009-06-04 14:04:53 -070012using namespace android;
13using namespace std;
14
15static string
16parent_dir(const string& path)
17{
18 return string(String8(path.c_str()).getPathDir().string());
19}
20
21static int
22mkdirs(const char* last)
23{
24 String8 dest;
25 const char* s = last-1;
26 int err;
27 do {
28 s++;
29 if (s > last && (*s == '.' || *s == 0)) {
30 String8 part(last, s-last);
31 dest.appendPath(part);
32#ifdef HAVE_MS_C_RUNTIME
33 err = _mkdir(dest.string());
34#else
35 err = mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
36#endif
37 if (err != 0) {
38 return err;
39 }
40 last = s+1;
41 }
42 } while (*s);
43 return 0;
44}
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046string
47translated_file_name(const string& file, const string& locale)
48{
49 const char* str = file.c_str();
50 const char* p = str + file.length();
51 const char* rest = NULL;
52 const char* values = p;
53
54 while (p > str) {
55 p--;
56 if (*p == '/') {
57 rest = values;
58 values = p;
59 if (0 == strncmp("values", values+1, rest-values-1)) {
60 break;
61 }
62 }
63 }
64 values++;
65
66 string result(str, values-str);
67 result.append(values, rest-values);
68
69 string language, region;
70 if (locale == "") {
71 language = "";
72 region = "";
73 }
74 else if (!split_locale(locale, &language, &region)) {
75 return "";
76 }
77
78 if (language != "") {
79 result += '-';
80 result += language;
81 }
82 if (region != "") {
83 result += "-r";
84 result += region;
85 }
86
87 result += rest;
88
89 return result;
90}
91
92ValuesFile*
93get_values_file(const string& filename, const Configuration& configuration,
94 int version, const string& versionString, bool printOnFailure)
95{
96 int err;
97 string text;
98
99 log_printf("get_values_file filename=%s\n", filename.c_str());
100 err = Perforce::GetFile(filename, versionString, &text, printOnFailure);
101 if (err != 0 || text == "") {
102 return NULL;
103 }
104
105 ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version,
106 versionString);
107 if (result == NULL) {
108 fprintf(stderr, "unable to parse file: %s\n", filename.c_str());
109 exit(1);
110 }
111 return result;
112}
113
114ValuesFile*
115get_local_values_file(const string& filename, const Configuration& configuration,
116 int version, const string& versionString, bool printOnFailure)
117{
118 int err;
119 string text;
120 char buf[2049];
121 int fd;
122 ssize_t amt;
123
124 fd = open(filename.c_str(), O_RDONLY);
125 if (fd == -1) {
126 fprintf(stderr, "unable to open file: %s\n", filename.c_str());
127 return NULL;
128 }
129
130 while ((amt = read(fd, buf, sizeof(buf)-1)) > 0) {
131 text.append(buf, amt);
132 }
133
134 close(fd);
135
136 if (text == "") {
137 return NULL;
138 }
139
140 ValuesFile* result = ValuesFile::ParseString(filename, text, configuration, version,
141 versionString);
142 if (result == NULL) {
143 fprintf(stderr, "unable to parse file: %s\n", filename.c_str());
144 exit(1);
145 }
146 return result;
147}
148
149void
150print_file_status(size_t j, size_t J, const string& message)
151{
152 printf("\r%s file %zd of %zd...", message.c_str(), j, J);
153 fflush(stdout);
154}
155
156int
157write_to_file(const string& filename, const string& text)
158{
159 mkdirs(parent_dir(filename).c_str());
160 int fd = open(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
161 if (fd < 0) {
162 fprintf(stderr, "unable to open file for write (%s): %s\n", strerror(errno),
163 filename.c_str());
164 return -1;
165 }
166
167 ssize_t amt = write(fd, text.c_str(), text.length());
168
169 close(fd);
170
171 if (amt < 0) {
172 return amt;
173 }
174 return amt == (ssize_t)text.length() ? 0 : -1;
175}
176
177