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