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