The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SWConfig Class Reference

#include <swconfig.h>

+ Collaboration diagram for SWConfig:

Public Member Functions

virtual void augment (const SWConfig &addFrom)
 
SWBuf getFileName () const
 
ConfigEntMapgetSection (const char *section)
 
virtual SectionMapgetSections ()
 
const SectionMapgetSections () const
 
SWBuf getValue (const char *section, const char *key)
 
virtual void load ()
 
SWDEPRECATED void Load ()
 
SWConfigoperator+= (const SWConfig &addFrom)
 
ConfigEntMapoperator[] (const char *section)
 
virtual void save () const
 
SWDEPRECATED void Save ()
 
void setValue (const char *section, const char *key, const char *value)
 
 SWConfig (const char *fileName)
 
 SWConfig ()
 
virtual ~SWConfig ()
 

Public Attributes

SWDEPRECATED SWBuf filename
 
SWDEPRECATED SectionMap Sections
 

Detailed Description

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

Definition at line 41 of file swconfig.h.

Constructor & Destructor Documentation

SWConfig::SWConfig ( const char *  fileName)

Map of available sections The map of available sections. Constructor of SWConfig

Parameters
fileNameThe storage path for this config.

Definition at line 41 of file swconfig.cpp.

41  {
42  filename = ifilename;
43  load();
44 }
virtual void load()
Definition: swconfig.cpp:55
SWDEPRECATED SWBuf filename
Definition: swconfig.h:130
SWORD_NAMESPACE_START SWConfig::SWConfig ( )

Definition at line 37 of file swconfig.cpp.

37  {
38 }
SWConfig::~SWConfig ( )
virtual

Definition at line 47 of file swconfig.cpp.

47  {
48 }

Member Function Documentation

void SWConfig::augment ( const SWConfig addFrom)
virtual

Merges into this config the values from addFrom

Parameters
addFromThe config which values should be merged to this config object. Already existing values will be overwritten.

Definition at line 148 of file swconfig.cpp.

148  {
149 
150  SectionMap::const_iterator section;
151  ConfigEntMap::const_iterator entry, start, end;
152 
153  for (section = addFrom.getSections().begin(); section != addFrom.getSections().end(); ++section) {
154  for (entry = (*section).second.begin(); entry != (*section).second.end(); ++entry) {
155  start = getSections()[section->first].lower_bound(entry->first);
156  end = getSections()[section->first].upper_bound(entry->first);
157  // do we have multiple instances of the same key?
158  if (start != end) {
159  // TODO: what is this?
160  ConfigEntMap::const_iterator x = addFrom.getSections().find(section->first)->second.lower_bound(entry->first);
161  ConfigEntMap::const_iterator y = addFrom.getSections().find(section->first)->second.upper_bound(entry->first);
162  ++x;
163  if (((++start) != end) || (x != y)) {
164  for (--start; start != end; ++start) {
165  if (!strcmp(start->second.c_str(), entry->second.c_str()))
166  break;
167  }
168  if (start == end)
169  getSections()[(*section).first].insert(ConfigEntMap::value_type((*entry).first, (*entry).second));
170  }
171  else getSections()[section->first][entry->first.c_str()] = entry->second.c_str();
172  }
173  else getSections()[section->first][entry->first.c_str()] = entry->second.c_str();
174  }
175  }
176 }
virtual SectionMap & getSections()
Definition: swconfig.cpp:186
SWBuf SWConfig::getFileName ( ) const

The storage path used by this SWConfig object

Definition at line 191 of file swconfig.cpp.

191 { return filename; }
SWDEPRECATED SWBuf filename
Definition: swconfig.h:130
ConfigEntMap& SWConfig::getSection ( const char *  section)
inline

Get a specified section from config, creating the section if needed There is no const version of this method because it returns a ConfigEntMap reference, creating the requested section if it doesn't exist.

Parameters
sectionsection name to retrieve

Definition at line 76 of file swconfig.h.

76 { return getSections()[section]; }
virtual SectionMap & getSections()
Definition: swconfig.cpp:186
SectionMap & SWConfig::getSections ( )
virtual

Get the section map for the config

Definition at line 186 of file swconfig.cpp.

186 { return Sections; }
SWDEPRECATED SectionMap Sections
Definition: swconfig.h:125
const SectionMap& SWConfig::getSections ( ) const
inline

Definition at line 57 of file swconfig.h.

57 { return const_cast<SWConfig *>(this)->getSections(); }
virtual SectionMap & getSections()
Definition: swconfig.cpp:186
SWBuf SWConfig::getValue ( const char *  section,
const char *  key 
)
inline

get a value from a [section] key=value

Parameters
sectionthe section name containing the key
keythe key to which the value is associated
Returns
the value associated with the key in the provided section

Definition at line 102 of file swconfig.h.

102  {
103  return (*this)[section][key];
104  }
void SWConfig::load ( )
virtual

Load the content from datastore

Definition at line 55 of file swconfig.cpp.

55  {
56 
57  if (!getFileName().size()) return; // assert we have a filename
58 
59  FileDesc *cfile;
60  char *buf, *data;
61  SWBuf line;
62  ConfigEntMap cursect;
63  SWBuf sectname;
64  bool first = true;
65 
66  getSections().erase(getSections().begin(), getSections().end());
67 
69  if (cfile->getFd() > 0) {
70  bool goodLine = FileMgr::getLine(cfile, line);
71 
72  // clean UTF encoding tags at start of file
73  while (goodLine && line.length() &&
74  ((((unsigned char)line[0]) == 0xEF) ||
75  (((unsigned char)line[0]) == 0xBB) ||
76  (((unsigned char)line[0]) == 0xBF))) {
77  line << 1;
78  }
79 
80  while (goodLine) {
81  // ignore commented lines
82  if (!line.startsWith("#")) {
83  buf = new char [ line.length() + 1 ];
84  strcpy(buf, line.c_str());
85  if (*strstrip(buf) == '[') {
86  if (!first)
87  getSections().insert(SectionMap::value_type(sectname, cursect));
88  else first = false;
89 
90  cursect.erase(cursect.begin(), cursect.end());
91 
92  strtok(buf, "]");
93  sectname = buf+1;
94  }
95  else {
96  strtok(buf, "=");
97  if ((*buf) && (*buf != '=')) {
98  if ((data = strtok(NULL, "")))
99  cursect.insert(ConfigEntMap::value_type(buf, strstrip(data)));
100  else cursect.insert(ConfigEntMap::value_type(buf, ""));
101  }
102  }
103  delete [] buf;
104  }
105  goodLine = FileMgr::getLine(cfile, line);
106  }
107  if (!first)
108  getSections().insert(SectionMap::value_type(sectname, cursect));
109 
111  }
112 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
virtual SectionMap & getSections()
Definition: swconfig.cpp:186
static unsigned int RDONLY
Definition: filemgr.h:75
SWBuf getFileName() const
Definition: swconfig.cpp:191
int getFd()
Definition: filemgr.h:231
void close(FileDesc *file)
Definition: filemgr.cpp:196
return NULL
Definition: regex.c:7953
int size
Definition: regex.c:5043
SWORD_NAMESPACE_START typedef multimapwithdefault< SWBuf, SWBuf, std::less< SWBuf > > ConfigEntMap
Definition: swconfig.h:35
char * strstrip(char *istr)
Definition: utilstr.cpp:118
static char getLine(FileDesc *fDesc, SWBuf &line)
Definition: filemgr.cpp:527
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
SWDEPRECATED void SWConfig::Load ( )
inline
Deprecated:
Use load() instead.

Definition at line 135 of file swconfig.h.

135 { load(); }
virtual void load()
Definition: swconfig.cpp:55
SWConfig& SWConfig::operator+= ( const SWConfig addFrom)
inline

shorthand operator for augment

Definition at line 95 of file swconfig.h.

95 { augment(addFrom); return *this; }
virtual void augment(const SWConfig &addFrom)
Definition: swconfig.cpp:148
ConfigEntMap& SWConfig::operator[] ( const char *  section)
inline

This operator provides a conventient syntax to get and store config values

config[section][key] = value; value = config[section][key];

The following will work:

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

Definition at line 91 of file swconfig.h.

91 { return getSection(section); }
ConfigEntMap & getSection(const char *section)
Definition: swconfig.h:76
void SWConfig::save ( ) const
virtual

Save the content of this config object to the datastore

Definition at line 115 of file swconfig.cpp.

115  {
116 
117  if (!getFileName().size()) return; // assert we have a filename
118 
119  FileDesc *cfile;
120  SWBuf buf;
121  SectionMap::const_iterator sit;
122  ConfigEntMap::const_iterator entry;
123  SWBuf sectname;
124 
126  if (cfile->getFd() > 0) {
127 
128  for (sit = getSections().begin(); sit != getSections().end(); ++sit) {
129  buf = "\n[";
130  buf += (*sit).first.c_str();
131  buf += "]\n";
132  cfile->write(buf.c_str(), buf.length());
133  for (entry = (*sit).second.begin(); entry != (*sit).second.end(); ++entry) {
134  buf = (*entry).first.c_str();
135  buf += "=";
136  buf += (*entry).second.c_str();
137  buf += "\n";
138  cfile->write(buf.c_str(), buf.length());
139  }
140  }
141  buf = "\n";
142  cfile->write(buf.c_str(), buf.length());
144  }
145 }
FileDesc * open(const char *path, int mode, bool tryDowngrade)
Definition: filemgr.cpp:175
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
virtual SectionMap & getSections()
Definition: swconfig.cpp:186
static unsigned int RDWR
Definition: filemgr.h:76
static unsigned int TRUNC
Definition: filemgr.h:74
SWBuf getFileName() const
Definition: swconfig.cpp:191
int getFd()
Definition: filemgr.h:231
long write(const void *buf, long count)
Definition: filemgr.cpp:153
void close(FileDesc *file)
Definition: filemgr.cpp:196
const char * c_str() const
Definition: swbuf.h:158
int size
Definition: regex.c:5043
static unsigned int CREAT
Definition: filemgr.h:72
static FileMgr * getSystemFileMgr()
Definition: filemgr.cpp:101
SWDEPRECATED void SWConfig::Save ( )
inline
Deprecated:
Use save() instead.

Definition at line 140 of file swconfig.h.

140 { save(); }
virtual void save() const
Definition: swconfig.cpp:115
void SWConfig::setValue ( const char *  section,
const char *  key,
const char *  value 
)
inline

set a value for a specified key in a [section]

Parameters
sectionthe section name to contain the key
keythe key to which to associate the value
valuethe value to associated with the key

Definition at line 111 of file swconfig.h.

111  {
112  (*this)[section][key] = value;
113  }

Member Data Documentation

SWDEPRECATED SWBuf SWConfig::filename
Deprecated:
Use getFileName() instead.

Definition at line 130 of file swconfig.h.

SWDEPRECATED SectionMap SWConfig::Sections
Deprecated:
Use getSections() instead.

Definition at line 125 of file swconfig.h.


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