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