Debian Codegraph @

rudeconfig/src/Section.cpp

raw
1 // Section.cpp
2 //
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Matthew Flood
4 // See file AUTHORS for contact information
5 //
6 // This file is part of RudeConfig.
7 //
8 // RudeConfig is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // RudeConfig is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with RudeConfig; (see COPYING) if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 // 02111-1307, USA.
22 //------------------------------------------------------------------------
23
24 #include "../config.h"
25
26 #include "Section.h"
27
28 #ifndef INCLUDED_RUDE_DataLine_H
29 #include "DataLine.h"
30 #endif
31
32 #ifndef INCLUDED_KeyValue_H
33 #include "KeyValue.h"
34 #endif
35
36 #ifndef INCLUDED_SourceDest_H
37 #include "SourceDest.h"
38 #endif
39
40 #ifndef INCLUDED_WhiteSpace_H
41 #include "WhiteSpace.h"
42 #endif
43
44 #ifndef INCLUDED_Comment_H
45 #include "Comment.h"
46 #endif
47
48 #ifndef INCLUDED_AbstractWriter_H
49 #include "AbstractWriter.h"
50 #endif
51
52 #ifndef INCLUDED_STRING
53 #include <string>
54 #define INCLUDED_STRING
55 #endif
56
57
58 using namespace rude::config;
59
60 namespace rude{
61 namespace config{
62
63 Section::Section(const char *sectionname, const char *sectioncomment)
64 {
65 d_isDeleted = false;
66 d_sectionname = sectionname ? sectionname: "";
67 d_sectioncomment = sectioncomment ? sectioncomment: "";
68 }
69
70 Section::~Section()
71 {
72 std::vector<DataLine*>::iterator iter;
73 for(iter = d_allDataVector.begin(); iter != d_allDataVector.end(); iter++)
74 {
75 delete (*iter);
76 }
77 }
78
79 void Section::acceptWriter(AbstractWriter& writer) const
80 {
81 writer.visitSection(*this);
82
83 // Send the writer to all the children.
84 // The writer will know what to do.
85 //
86 std::vector<DataLine*>::iterator iter;
87 for(iter = d_allDataVector.begin(); iter != d_allDataVector.end(); iter++)
88 {
89 (*iter)->acceptWriter(writer);
90 }
91 }
92
93 const char *Section::getSectionName() const
94 {
95 return d_sectionname.c_str();
96 }
97
98 const char *Section::getSectionComment() const
99 {
100 return d_sectioncomment.c_str();
101 }
102
103 void Section::setSectionComment(const char * newcomment)
104 {
105 d_sectioncomment = newcomment ? newcomment : "";
106 }
107
108 bool Section::isDeleted() const
109 {
110 return d_isDeleted;
111 }
112
113 void Section::isDeleted(bool is_it_or_not)
114 {
115 d_isDeleted = is_it_or_not;
116 if(d_isDeleted)
117 {
118 // Delete all data members
119 //
120 std::vector<DataLine*>::iterator iter;
121 for(iter = d_allDataVector.begin(); iter != d_allDataVector.end(); iter++)
122 {
123 (*iter)->isDeleted(true);
124 }
125 d_kv_vector.clear();
126 d_kv_map.clear();
127 d_sd_vector.clear();
128 }
129 }
130
131 int Section::getNumDataMembers() const
132 {
133
134 return d_kv_vector.size();
135 }
136
137
138 int Section::getNumSourceDestMembers() const
139 {
140 return d_sd_vector.size();
141 }
142
143 const char *Section::getDataNameAt(int index) const
144 {
145 KeyValue *kv = d_kv_vector[index];
146 if(kv)
147 {
148 return kv->getName();
149 }
150 return "";
151 }
152
153 const char *Section::getDestAppAt(int index) const
154 {
155 SourceDest *sd = d_sd_vector[index];
156 if(sd)
157 {
158 return sd->getDestApp();
159 }
160 return "";
161 }
162
163 const char *Section::getSrcAppAt(int index) const
164 {
165 SourceDest *sd = d_sd_vector[index];
166 if(sd)
167 {
168 return sd->getSrcApp();
169 }
170 return "";
171 }
172 const char *Section::getDestObjAt(int index) const
173 {
174 SourceDest *sd = d_sd_vector[index];
175 if(sd)
176 {
177 return sd->getDestObj();
178 }
179 return "";
180 }
181
182 const char *Section::getSrcObjAt(int index) const
183 {
184 SourceDest *sd = d_sd_vector[index];
185 if(sd)
186 {
187 return sd->getSrcObj();
188 }
189 return "";
190 }
191
192 // Returns the concatenation of srcApp.srcObj
193 const char *Section::getWidthAt(int index) const
194 {
195 SourceDest *sd = d_sd_vector[index];
196 if(sd)
197 {
198 return sd->getWidth();
199 }
200 return "";
201 }
202 const char *Section::getCommTypeAt(int index) const
203 {
204 SourceDest *sd = d_sd_vector[index];
205 if(sd)
206 {
207 return sd->getCommType();
208 }
209 return "";
210
211 }
212 const char *Section::getProcMethodAt(int index) const
213 {
214 SourceDest *sd = d_sd_vector[index];
215 if(sd)
216 {
217 return sd->getProcMethod();
218 }
219 return "";
220 }
221 const char *Section::getDataValueAt(int index) const
222 {
223 KeyValue *kv = d_kv_vector[index];
224 if(kv)
225 {
226 return kv->getValue();
227 }
228 return "";
229 }
230
231 // MAPPED
232 //
233 bool Section::exists(const char *name) const
234 {
235 if(name)
236 {
237 std::string myname = name;
238 KeyValue *mydata = d_kv_map[myname];
239 if(mydata)
240 {
241 return true;
242 }
243 }
244 return false;
245 }
246
247 // MAPPED
248 //
249 const char * Section::getValue(const char *name) const
250 {
251 if(name)
252 {
253 std::string myname=name;
254 KeyValue *mydata = d_kv_map[myname];
255 if(mydata)
256 {
257 return mydata->getValue();
258 }
259 }
260 return "";
261 }
262
263 // MAPPED
264 //
265 void Section::setValue(const char *name, const char *value)
266 {
267 if(name)
268 {
269 std::string myname=name;
270 KeyValue *mydata = d_kv_map[myname];
271 if(mydata)
272 {
273 mydata->setValue(value);
274 mydata->isDeleted(false);
275
276 }
277 else
278 {
279 KeyValue *newdata = new KeyValue();
280 newdata->setName(name);
281 newdata->setValue(value);
282 d_allDataVector.push_back(newdata);
283 d_kv_vector.push_back(newdata);
284 d_kv_map[myname] = newdata;
285 }
286 }
287 }
288
289 // MAPPED
290 //
291 const char * Section::getComment(const char *name) const
292 {
293 if(name)
294 {
295 std::string myname=name;
296 KeyValue *mydata = d_kv_map[myname];
297 if(mydata)
298 {
299 return mydata->getComment();
300 }
301 }
302 return "";
303 }
304
305 // MAPPED
306 //
307 void Section::setComment(const char *name, const char *comment)
308 {
309 if(name)
310 {
311 std::string myname=name;
312 KeyValue *mydata = d_kv_map[myname];
313 if(mydata)
314 {
315 mydata->setComment(comment);
316 }
317 }
318 }
319
320 // MAPPED
321 //
322 void Section::setValue(const char *name, const char *value, const char *comment)
323 {
324
325 if(name)
326 {
327 std::string myname=name;
328 KeyValue *mydata = d_kv_map[myname];
329 if(mydata)
330 {
331 mydata->setValue(value);
332 mydata->setComment(comment);
333 mydata->isDeleted(false);
334 }
335 else
336 {
337 KeyValue *newdata = new KeyValue(name, value, comment);
338
339 d_allDataVector.push_back(newdata);
340 d_kv_vector.push_back(newdata);
341 d_kv_map[myname] = newdata;
342 }
343 }
344 }
345
346 // CREATES
347 //
348 void Section::addSourceDest(const char *srcApp, const char *srcObj, const char *destApp, const char *destObj,
349 const char *width, const char *commType, const char *procMethod, const char *comment)
350 {
351
352 SourceDest *newsd = new SourceDest(srcApp, srcObj, destApp, destObj, width, commType, procMethod, comment);
353
354 d_allDataVector.push_back(newsd);
355 d_sd_vector.push_back(newsd);
356
357 }
358
359
360 // MAPPED
361 //
362 void Section::addComment(const char *comment)
363 {
364 Comment *newdata = new Comment(comment);
365 d_allDataVector.push_back(newdata);
366 }
367
368 // MAPPED
369 //
370 void Section::addWhiteSpace(const char *whitespace)
371 {
372 WhiteSpace *newdata = new WhiteSpace(whitespace);
373 d_allDataVector.push_back(newdata);
374 }
375
376 // MAPPED
377 //
378 bool Section::deleteData(const char *name)
379 {
380 // Since we are keeping deleted data in the d_allDataVector,
381 // multiple set/deletes will require lots of memory!!
382 //
383 if(name)
384 {
385 std::string myname=name;
386 KeyValue *mydata = d_kv_map[myname];
387 if(mydata)
388 {
389 // Remain in d_allDataVector, but as deleted
390 //
391 mydata->isDeleted(true);
392
393 // remove from keyvalue vector and map
394 //
395 std::vector<KeyValue*>::iterator iter;
396 for(iter = d_kv_vector.begin(); iter != d_kv_vector.end(); iter ++)
397 {
398 if( *iter == mydata )
399 {
400 d_kv_vector.erase(iter);
401 break;
402 }
403 }
404
405 d_kv_map.erase(myname);
406
407 return true;
408 }
409 }
410 return false;
411 }
412
413 }} // end namespace rude::config
414
415