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