| 1 |
// File.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 |
|
| 25 |
#include "../config.h" |
| 26 |
|
| 27 |
#include "File.h" |
| 28 |
|
| 29 |
#ifndef INCLUDED_Section_H |
| 30 |
#include "Section.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifndef INCLUDED_AbstractWriter_H |
| 34 |
#include "AbstractWriter.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
using namespace std; |
| 38 |
|
| 39 |
namespace rude{ |
| 40 |
namespace config{ |
| 41 |
|
| 42 |
File::File() |
| 43 |
{ |
| 44 |
d_currentSection = new Section("", 0); |
| 45 |
d_sections.push_back(d_currentSection); |
| 46 |
std::string empty = ""; |
| 47 |
d_sectionmap[empty] = d_currentSection; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
File::~File() |
| 52 |
{ |
| 53 |
std::vector<Section*>::iterator iter; |
| 54 |
for(iter = d_sections.begin(); iter != d_sections.end(); iter++) |
| 55 |
{ |
| 56 |
delete (*iter); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void File::acceptWriter(AbstractWriter& writer) const |
| 61 |
{ |
| 62 |
writer.visitFile(*this); |
| 63 |
|
| 64 |
// All the data members are responsible for printing themselves out. |
| 65 |
// They are also responsible for knowing what to do when they are |
| 66 |
// marked as deleted and when commentchar / delimiter are 0 |
| 67 |
// |
| 68 |
std::vector<Section*>::iterator iter; |
| 69 |
for(iter = d_sections.begin(); iter != d_sections.end(); iter++) |
| 70 |
{ |
| 71 |
(*iter)->acceptWriter(writer); |
| 72 |
} |
| 73 |
} |
| 74 |
void File::clear() |
| 75 |
{ |
| 76 |
std::vector<Section*>::iterator iter; |
| 77 |
for(iter = d_sections.begin(); iter != d_sections.end(); iter++) |
| 78 |
{ |
| 79 |
delete (*iter); |
| 80 |
} |
| 81 |
d_sections.clear(); |
| 82 |
d_sectionmap.clear(); |
| 83 |
|
| 84 |
|
| 85 |
// We create one to begin with, swo that we always |
| 86 |
// have a valid d_currentSection to work with. |
| 87 |
d_currentSection = new Section("", 0); |
| 88 |
d_sections.push_back(d_currentSection); |
| 89 |
std::string empty = ""; |
| 90 |
d_sectionmap[empty] = d_currentSection; |
| 91 |
|
| 92 |
} |
| 93 |
const char * File::getStringValue(const char *name) const |
| 94 |
{ |
| 95 |
if(d_currentSection) |
| 96 |
{ |
| 97 |
return d_currentSection->getValue(name); |
| 98 |
} |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
bool File::deleteData(const char *name) |
| 103 |
{ |
| 104 |
if(d_currentSection) |
| 105 |
{ |
| 106 |
return d_currentSection->deleteData(name); |
| 107 |
} |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
void File::setStringValue(const char *name, const char *value) |
| 112 |
{ |
| 113 |
if(d_currentSection) |
| 114 |
{ |
| 115 |
d_currentSection->setValue(name, value); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
int File::getNumDataMembers() const |
| 120 |
{ |
| 121 |
if(d_currentSection) |
| 122 |
{ |
| 123 |
return d_currentSection->getNumDataMembers(); |
| 124 |
} |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
const char *File::getDataNameAt(int index) const |
| 129 |
{ |
| 130 |
if(d_currentSection) |
| 131 |
{ |
| 132 |
return d_currentSection->getDataNameAt(index); |
| 133 |
} |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
const char *File::getDataValueAt(int index) const |
| 138 |
{ |
| 139 |
if(d_currentSection) |
| 140 |
{ |
| 141 |
return d_currentSection->getDataValueAt(index); |
| 142 |
} |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
int File::getNumSourceDestMembers() const |
| 148 |
{ |
| 149 |
if(d_currentSection) |
| 150 |
{ |
| 151 |
return d_currentSection->getNumSourceDestMembers(); |
| 152 |
} |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
const char *File::getSrcAppAt(int index) const |
| 157 |
{ |
| 158 |
if(d_currentSection) |
| 159 |
{ |
| 160 |
return d_currentSection->getSrcAppAt(index); |
| 161 |
} |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
const char *File::getDestAppAt(int index) const |
| 166 |
{ |
| 167 |
if(d_currentSection) |
| 168 |
{ |
| 169 |
return d_currentSection->getDestAppAt(index); |
| 170 |
} |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
const char *File::getSrcObjAt(int index) const |
| 175 |
{ |
| 176 |
if(d_currentSection) |
| 177 |
{ |
| 178 |
return d_currentSection->getSrcObjAt(index); |
| 179 |
} |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
const char *File::getDestObjAt(int index) const |
| 184 |
{ |
| 185 |
if(d_currentSection) |
| 186 |
{ |
| 187 |
return d_currentSection->getDestObjAt(index); |
| 188 |
} |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
const char *File::getWidthAt(int index) const |
| 193 |
{ |
| 194 |
if(d_currentSection) |
| 195 |
{ |
| 196 |
return d_currentSection->getWidthAt(index); |
| 197 |
} |
| 198 |
return 0; |
| 199 |
} |
| 200 |
const char *File::getCommTypeAt(int index) const |
| 201 |
{ |
| 202 |
if(d_currentSection) |
| 203 |
{ |
| 204 |
return d_currentSection->getCommTypeAt(index); |
| 205 |
} |
| 206 |
return 0; |
| 207 |
|
| 208 |
} |
| 209 |
const char *File::getProcMethodAt(int index) const |
| 210 |
{ |
| 211 |
if(d_currentSection) |
| 212 |
{ |
| 213 |
return d_currentSection->getProcMethodAt(index); |
| 214 |
} |
| 215 |
return 0; |
| 216 |
} |
| 217 |
bool File::exists(const char *name) const |
| 218 |
{ |
| 219 |
if(d_currentSection) |
| 220 |
{ |
| 221 |
return d_currentSection->exists(name); |
| 222 |
} |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
int File::getNumSections() const |
| 227 |
{ |
| 228 |
int len = d_sections.size(); |
| 229 |
int count = 0; |
| 230 |
for(int x=0; x< len; x++) |
| 231 |
{ |
| 232 |
Section *theSection = d_sections[x]; |
| 233 |
if(!theSection->isDeleted()) |
| 234 |
{ |
| 235 |
count++; |
| 236 |
} |
| 237 |
} |
| 238 |
return count; |
| 239 |
} |
| 240 |
|
| 241 |
const char *File::getSectionNameAt(int index) const |
| 242 |
{ |
| 243 |
int len = d_sections.size(); |
| 244 |
int position = 0; |
| 245 |
for(int x=0; x< len; x++) |
| 246 |
{ |
| 247 |
Section *theSection = d_sections[x]; |
| 248 |
if(!theSection->isDeleted()) |
| 249 |
{ |
| 250 |
if(position == index) |
| 251 |
{ |
| 252 |
return theSection->getSectionName(); |
| 253 |
} |
| 254 |
position++; |
| 255 |
} |
| 256 |
} |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
bool File::setSection(const char *sectionname, bool shouldCreate) |
| 261 |
{ |
| 262 |
if(sectionname) |
| 263 |
{ |
| 264 |
std::string name=sectionname; |
| 265 |
Section *thesection = d_sectionmap[name]; |
| 266 |
if(thesection) |
| 267 |
{ |
| 268 |
if(!thesection->isDeleted()) |
| 269 |
{ |
| 270 |
d_currentSection = thesection; |
| 271 |
return true; |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
if(shouldCreate) |
| 276 |
{ |
| 277 |
// revive the deleted section |
| 278 |
// |
| 279 |
thesection->isDeleted(false); |
| 280 |
d_currentSection = thesection; |
| 281 |
return true; |
| 282 |
} |
| 283 |
return false; |
| 284 |
} |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
// The section could not be found..... |
| 289 |
// |
| 290 |
if(shouldCreate) |
| 291 |
{ |
| 292 |
d_currentSection = new Section(sectionname, 0); |
| 293 |
std::string stringname = sectionname; |
| 294 |
d_sections.push_back(d_currentSection); |
| 295 |
d_sectionmap[stringname] = d_currentSection; |
| 296 |
return true; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
Section *File::getSection(const char *sectionname) |
| 304 |
{ |
| 305 |
setSection(sectionname, true); |
| 306 |
return d_currentSection; |
| 307 |
} |
| 308 |
|
| 309 |
bool File::deleteSection(const char *sectionname) |
| 310 |
{ |
| 311 |
if(sectionname) |
| 312 |
{ |
| 313 |
std::string name=sectionname; |
| 314 |
Section *thesection = d_sectionmap[name]; |
| 315 |
if(thesection) |
| 316 |
{ |
| 317 |
thesection->isDeleted(true); |
| 318 |
// Here is the interesting part, |
| 319 |
// By deleting the current section, we have effectively |
| 320 |
// deleted all of its data members... |
| 321 |
// but if we are deleting the current section, |
| 322 |
// then we need to change the current section to |
| 323 |
// the default one - the "" section, |
| 324 |
// and make sure that that section is undeleted.... |
| 325 |
// because we always need a current section. |
| 326 |
if(thesection == d_currentSection) |
| 327 |
{ |
| 328 |
return setSection("", true); |
| 329 |
} |
| 330 |
return true; |
| 331 |
|
| 332 |
} |
| 333 |
// The section could not be found..... |
| 334 |
} |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
}} // end namespace rude::config |
| 341 |
|