SWConfig Class Reference

#include <swconfig.h>

Collaboration diagram for SWConfig:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual void augment (SWConfig &addFrom)
virtual void Load ()
virtual SWConfigoperator+= (SWConfig &addFrom)
virtual ConfigEntMapoperator[] (const char *section)
virtual void Save ()
 SWConfig ()
 SWConfig (const char *ifilename)
virtual ~SWConfig ()

Public Attributes

SWBuf filename
SectionMap Sections

Private Member Functions

char getline (int fd, SWBuf &line)

Detailed Description

The class to read and save settings using a file on disk.

Definition at line 40 of file swconfig.h.


Constructor & Destructor Documentation

SWConfig::SWConfig ( const char *  ifilename  ) 

Constructor of SWConfig

Parameters:
ifilename The file, which should be used for this config.

Definition at line 34 of file swconfig.cpp.

00034                                          {
00035     filename = ifilename;
00036     Load();
00037 }

SWORD_NAMESPACE_START SWConfig::SWConfig (  ) 

Definition at line 31 of file swconfig.cpp.

00031                    {
00032 }

SWConfig::~SWConfig (  )  [virtual]

Definition at line 40 of file swconfig.cpp.

00040                     {
00041 }


Member Function Documentation

void SWConfig::augment ( SWConfig addFrom  )  [virtual]

Merges the values of addFrom

Parameters:
addFrom The config which values should be merged to this config object. Already existing values will be overwritten.

Definition at line 136 of file swconfig.cpp.

00136                                         {
00137 
00138     SectionMap::iterator section;
00139     ConfigEntMap::iterator entry, start, end;
00140 
00141     for (section = addFrom.Sections.begin(); section != addFrom.Sections.end(); section++) {
00142         for (entry = (*section).second.begin(); entry != (*section).second.end(); entry++) {
00143             start = Sections[section->first].lower_bound(entry->first);
00144             end   = Sections[section->first].upper_bound(entry->first);
00145             if (start != end) {
00146                 if (((++start) != end)
00147                         || ((++(addFrom.Sections[section->first].lower_bound(entry->first))) != addFrom.Sections[section->first].upper_bound(entry->first))) {
00148                     for (--start; start != end; start++) {
00149                         if (!strcmp(start->second.c_str(), entry->second.c_str()))
00150                             break;
00151                     }
00152                     if (start == end)
00153                         Sections[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second));
00154                 }
00155                 else    Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00156             }       
00157             else    Sections[section->first][entry->first.c_str()] = entry->second.c_str();
00158         }
00159     }
00160 }

char SWConfig::getline ( int  fd,
SWBuf line 
) [private]
void SWConfig::Load (  )  [virtual]

Load from disk Load the content from disk.

Definition at line 43 of file swconfig.cpp.

00043                     {
00044 
00045     if (!filename.size()) return;   // assert we have a filename
00046 
00047     FileDesc *cfile;
00048     char *buf, *data;
00049     SWBuf line;
00050     ConfigEntMap cursect;
00051     SWBuf sectname;
00052     bool first = true;
00053     
00054     Sections.erase(Sections.begin(), Sections.end());
00055     
00056     cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDONLY);
00057     if (cfile->getFd() > 0) {
00058         bool goodLine = FileMgr::getLine(cfile, line);
00059 
00060         // clean UTF encoding tags at start of file
00061         while (goodLine && line.length() && 
00062                 ((((unsigned char)line[0]) == 0xEF) ||
00063                  (((unsigned char)line[0]) == 0xBB) ||
00064                  (((unsigned char)line[0]) == 0xBF))) {
00065             line << 1;
00066         }
00067         
00068         while (goodLine) {
00069             // ignore commented lines
00070             if (!line.startsWith("#")) {
00071                 buf = new char [ line.length() + 1 ];
00072                 strcpy(buf, line.c_str());
00073                 if (*strstrip(buf) == '[') {
00074                     if (!first)
00075                         Sections.insert(SectionMap::value_type(sectname, cursect));
00076                     else first = false;
00077                     
00078                     cursect.erase(cursect.begin(), cursect.end());
00079 
00080                     strtok(buf, "]");
00081                     sectname = buf+1;
00082                 }
00083                 else {
00084                     strtok(buf, "=");
00085                     if ((*buf) && (*buf != '=')) {
00086                         if ((data = strtok(NULL, "")))
00087                             cursect.insert(ConfigEntMap::value_type(buf, strstrip(data)));
00088                         else cursect.insert(ConfigEntMap::value_type(buf, ""));
00089                     }
00090                 }
00091                 delete [] buf;
00092             }
00093             goodLine = FileMgr::getLine(cfile, line);
00094         }
00095         if (!first)
00096             Sections.insert(SectionMap::value_type(sectname, cursect));
00097 
00098         FileMgr::getSystemFileMgr()->close(cfile);
00099     }
00100 }

virtual SWConfig& SWConfig::operator+= ( SWConfig addFrom  )  [inline, virtual]

Definition at line 74 of file swconfig.h.

00074 { augment(addFrom); return *this; }

ConfigEntMap & SWConfig::operator[] ( const char *  section  )  [virtual]

Get a section This is an easy way to get and store config values. The following will work:

 SWConfig config("/home/user/.setttings");
 config["Colors"]["Background"] = "red";

Definition at line 163 of file swconfig.cpp.

00163                                                          {
00164     return Sections[section];
00165 }

void SWConfig::Save (  )  [virtual]

Save to disk Save the content of this config object to disk.

Definition at line 103 of file swconfig.cpp.

00103                     {
00104 
00105     if (!filename.size()) return;   // assert we have a filename
00106 
00107     FileDesc *cfile;
00108     SWBuf buf;
00109     SectionMap::iterator sit;
00110     ConfigEntMap::iterator entry;
00111     SWBuf sectname;
00112     
00113     cfile = FileMgr::getSystemFileMgr()->open(filename.c_str(), FileMgr::RDWR|FileMgr::CREAT|FileMgr::TRUNC);
00114     if (cfile->getFd() > 0) {
00115         
00116         for (sit = Sections.begin(); sit != Sections.end(); sit++) {
00117             buf =  "\n[";
00118             buf += (*sit).first.c_str();
00119             buf += "]\n";
00120             cfile->write(buf.c_str(), buf.length());
00121             for (entry = (*sit).second.begin(); entry != (*sit).second.end(); entry++) {
00122                 buf = (*entry).first.c_str();
00123                 buf += "=";
00124                 buf += (*entry).second.c_str();
00125                 buf += "\n";
00126                 cfile->write(buf.c_str(), buf.length());
00127             }
00128         }
00129         buf = "\n";
00130         cfile->write(buf.c_str(), buf.length());
00131         FileMgr::getSystemFileMgr()->close(cfile);
00132     }
00133 }


Member Data Documentation

The filename used by this SWConfig object

Definition at line 47 of file swconfig.h.

Map of available sections The map of available sections.

Definition at line 51 of file swconfig.h.


The documentation for this class was generated from the following files:

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1