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