blob: e396f8b4897f7d6cee8cecfea25a8fcf16f5ee63 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001#include "Values.h"
2#include <stdlib.h>
3
4
5// =====================================================================================
6StringResource::StringResource(const SourcePos& p, const string& f, const Configuration& c,
7 const string& i, int ix, XMLNode* v, const int ve, const string& vs,
8 const string& cmnt)
9 :pos(p),
10 file(f),
11 config(c),
12 id(i),
13 index(ix),
14 value(v),
15 version(ve),
16 versionString(vs),
17 comment(cmnt)
18{
19}
20
21StringResource::StringResource()
22 :pos(),
23 file(),
24 config(),
25 id(),
26 index(-1),
27 value(NULL),
28 version(),
29 versionString(),
30 comment()
31{
32}
33
34StringResource::StringResource(const StringResource& that)
35 :pos(that.pos),
36 file(that.file),
37 config(that.config),
38 id(that.id),
39 index(that.index),
40 value(that.value),
41 version(that.version),
42 versionString(that.versionString),
43 comment(that.comment)
44{
45}
46
47int
48StringResource::Compare(const StringResource& that) const
49{
50 if (file != that.file) {
51 return file < that.file ? -1 : 1;
52 }
53 if (id != that.id) {
54 return id < that.id ? -1 : 1;
55 }
56 if (index != that.index) {
57 return index - that.index;
58 }
59 if (config != that.config) {
60 return config < that.config ? -1 : 1;
61 }
62 if (version != that.version) {
63 return version < that.version ? -1 : 1;
64 }
65 return 0;
66}
67
68string
69StringResource::TypedID() const
70{
71 string result;
72 if (index < 0) {
73 result = "string:";
74 } else {
75 char n[20];
76 sprintf(n, "%d:", index);
77 result = "array:";
78 result += n;
79 }
80 result += id;
81 return result;
82}
83
84static void
85split(const string& raw, vector<string>*parts)
86{
87 size_t index = 0;
88 while (true) {
89 size_t next = raw.find(':', index);
90 if (next != raw.npos) {
91 parts->push_back(string(raw, index, next-index));
92 index = next + 1;
93 } else {
94 parts->push_back(string(raw, index));
95 break;
96 }
97 }
98}
99
100bool
101StringResource::ParseTypedID(const string& raw, string* id, int* index)
102{
103 vector<string> parts;
104 split(raw, &parts);
105
106 const size_t N = parts.size();
107
108 for (size_t i=0; i<N; i++) {
109 if (parts[i].length() == 0) {
110 return false;
111 }
112 }
113
114 if (N == 2 && parts[0] == "string") {
115 *id = parts[1];
116 *index = -1;
117 return true;
118 }
119 else if (N == 3 && parts[0] == "array") {
120 char* p;
121 int n = (int)strtol(parts[1].c_str(), &p, 0);
122 if (*p == '\0') {
123 *id = parts[2];
124 *index = n;
125 return true;
126 } else {
127 return false;
128 }
129 }
130 else {
131 return false;
132 }
133}
134