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