forked from config4star/config4cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigItem.cpp
More file actions
222 lines (185 loc) · 4.89 KB
/
ConfigItem.cpp
File metadata and controls
222 lines (185 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//-----------------------------------------------------------------------
// Copyright 2011 Ciaran McHale.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//----------------------------------------------------------------------
//--------
// #include's
//--------
#include <config4cpp/Configuration.h>
#include "ConfigItem.h"
#include "ConfigScope.h"
#include "UidIdentifierProcessor.h"
#include <string.h>
#include <assert.h>
namespace CONFIG4CPP_NAMESPACE {
static char *
copyString(const char * str)
{
char * result;
result = new char[strlen(str) + 1];
strcpy(result, str);
return result;
}
static char *
escapeString(const char * str)
{
int i;
int len;
StringBuffer buf;
char * result;
len = strlen(str);
for (i = 0; i < len; i++) {
switch (str[i]) {
case '\t':
buf.append("%t");
break;
case '\n':
buf.append("%n");
break;
case '%':
buf.append("%%");
break;
case '"':
buf.append("%\"");
break;
default:
buf.append(str[i]);
break;
}
}
result = new char[buf.length() + 1];
strcpy(result, buf.c_str());
return result;
}
static void
printIndent(StringBuffer & buf, int indentLevel)
{
int i;
for (i = 0; i < indentLevel; i++) {
buf.append("\t");
}
}
//----------------------------------------------------------------------
// Function: Constructor (overloaded)
//
// Description:
//----------------------------------------------------------------------
ConfigItem::ConfigItem(const char * name, const char * str)
{
m_type = Configuration::CFG_STRING;
m_name = copyString(name);
m_stringVal = copyString(str);
m_listVal = 0;
m_scope = 0;
}
ConfigItem::ConfigItem(const char * name, const StringVector & list)
{
m_type = Configuration::CFG_LIST;
m_name = copyString(name);
m_listVal = new StringVector(list);
m_scope = 0;
m_stringVal = 0;
}
ConfigItem::ConfigItem(const char * name, const char ** array, int size)
{
int i;
m_type = Configuration::CFG_LIST;
m_name = copyString(name);
m_scope = 0;
m_stringVal = 0;
m_listVal = new StringVector(size);
for (i = 0; i < size; i++) {
m_listVal->add(array[i]);
}
}
ConfigItem::ConfigItem(const char * name, ConfigScope * scope)
{
m_type = Configuration::CFG_SCOPE;
m_name = copyString(name);
m_scope = scope;
m_listVal = 0;
m_stringVal = 0;
}
//----------------------------------------------------------------------
// Function: Destructor
//
// Description:
//----------------------------------------------------------------------
ConfigItem::~ConfigItem()
{
delete m_listVal;
delete m_scope;
delete [] m_stringVal;
delete [] m_name;
}
//----------------------------------------------------------------------
// Function: dump()
//
// Description: Dump out the ConfigItem's contents.
//----------------------------------------------------------------------
void
ConfigItem::dump(
StringBuffer & buf,
const char * name,
bool wantExpandedUidNames,
int indentLevel) const
{
int i;
int len;
char * escStr;
StringBuffer nameBuf;
UidIdentifierProcessor uidIdProc;
if (!wantExpandedUidNames) {
name = uidIdProc.unexpand(name, nameBuf);
}
printIndent(buf, indentLevel);
switch (m_type) {
case Configuration::CFG_STRING:
escStr = escapeString(m_stringVal);
buf << name << " = \"" << escStr << "\";\n";
delete [] escStr;
break;
case Configuration::CFG_LIST:
buf << name << " = [";
len = m_listVal->length();
for (i = 0; i < len; i++) {
escStr = escapeString((*m_listVal)[i]);
buf << "\"" << escStr << "\"";
delete [] escStr;
if (i < len-1) {
buf << ", ";
}
}
buf << "];\n";
break;
case Configuration::CFG_SCOPE:
buf << name << " {\n";
m_scope->dump(buf, wantExpandedUidNames, indentLevel + 1);
printIndent(buf, indentLevel);
buf << "}\n";
break;
default:
assert(0); // Bug
break;
};
}
}; // namespace CONFIG4CPP_NAMESPACE