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