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