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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SConfig.h"
#include "SIOStream.h"
#include <stdio.h>
/**
*----------------------------------------------------------------------------
* This file is obsolete. 2001-01-12
*----------------------------------------------------------------------------
* @author: Gaspar Sinai <gaspar@yudit.org>
* @version: 2000-04-23
*/
/**
* A config file consists of sections in brackets
* [section first section]
* Inside a section (below the [] thingy:
* key 1=value 1
* key 2=value 2
* key-value pairs are
* Sections starting with '#' are comments.
*/
#ifdef USE_WINAPI
SString SLineSep("\r\n");
#else
SString SLineSep("\n");
#endif
SConfig::SConfig (const SFile& config) : file (config)
{
}
SConfig::SConfig (const SConfig& config) : file (config.file)
{
sectHashtable = config.sectHashtable;
}
SConfig&
SConfig::operator = (const SConfig& config)
{
file = config.file;
sectHashtable = config.sectHashtable;
return *this;
}
void
SConfig::setFile(const SFile& newFile)
{
file = newFile;
}
SConfig::~SConfig()
{
}
/**
* Write out the config in the file.
* @return false on error.
*/
bool
SConfig::write()
{
SInputStream o = file.getOutputStream();
if (!o.isOK()) return false;
SWriter w(o);
SStringVector sections = keys();
SString banner;
banner << "# Property file fo Yudit" << SLineSep;
w.write (banner);
for (unsigned int i=0; i<sections.size(); i++)
{
SString sl;
sl << SLineSep << "[" << sections[i] << "]" << SLineSep;
w.write (sl);
SStringVector props = keys(sections[i]);
for (unsigned int j=0; j<props.size(); j++)
{
SString line;
line << props[j] << "=" << get (sections[i], props[j]) << SLineSep;
w.write (line);
}
if (!w.isOK()) break;
}
if (!w.isOK()) return false;
if (!o.close()) return false;
return true;
}
/**
* Read the config in the file.
* @return 0 on ok or line number on bad file or -1 on read error
*/
int
SConfig::read ()
{
SInputStream i = file.getInputStream();
if (!i.isOK()) return -1;
SStringVector sepVector("\n,\r\n,\r");
SReader r(i, sepVector);
SString line;
SString section;
int linenum =0;
SProperties list;
while (true)
{
linenum++;
bool ret = r.read(&line);
if (ret == false) return -1;
// EOF
if (line.size()==0)
{
if (section.size()!=0)
{
set (section, list);
}
break;
}
sepVector.trim (&line);
if (line[0] == '#') continue;
if (line[0] == '!') continue;
if (line[0] == '[')
{
int ends = line.find("]");
if (ends <= 0) return linenum;
SString sub(line.array(), 1, ends-1);
if (sub.size()==0) return linenum;
if (section.size()!=0)
{
set (section, list);
}
list.clear();
section = sub;
continue;
}
SStringVector l;
if (l.split (line, "=", true) !=2) continue;
list.put (l[0], l[0]);
}
return 0;
}
/**
*@return the section keys
*/
SStringVector
SConfig::keys()
{
SStringVector k;
sectHashtable.keys (&k);
return SStringVector(k);
}
/**
*@return the keys
*/
SStringVector
SConfig::keys(const SString& section)
{
SStringVector k;
if (sectHashtable.get(section)!=0)
{
sectHashtable[section].keys (&k);
}
return SStringVector(k);
}
/**
* Get the value in section with a key.
*/
const SString&
SConfig::get (const SString& section, const SString& key)
{
if (sectHashtable.get(section)==0)
{
return SStringNull;
}
const SString* vle = sectHashtable[section].get (key);
if (vle ==0) return SStringNull;
return sectHashtable[section][key];
}
/**
* Set the appropriate value.
* create the section if it does not exist.
* @param section is the section to set.
* @param key is the key in the section
* @param vle is the value
*/
void
SConfig::set(const SString& section, const SString& key, const SString& vle)
{
if (sectHashtable.get(section)==0)
{
SProperties n;
sectHashtable.put (section, n);
}
/**
* This is crying for optimization
*/
SProperties l = sectHashtable[section];
l.put (key, vle);
sectHashtable.put (section, l);
}
/**
* A bit higher level set.
* Set the appropriate value.
* create the section if it does not exist.
* @param section is the section to set.
* @param list is the list to replace.
*/
void
SConfig::set(const SString& section, const SProperties& list)
{
sectHashtable.put (section, list);
}
/**
* Erase a key from the config.
*/
void
SConfig::remove (const SString& section, const SString& key)
{
if (sectHashtable.get (section)==0) return;
/**
* This is crying for optimization
*/
SProperties l = sectHashtable[section];
l.remove (key);
if (l.size()==0)
{
sectHashtable.remove (section);
}
else
{
sectHashtable.put (section, l);
}
}
/**
* Erase a whole section from the config.
*/
void
SConfig::remove (const SString& section)
{
sectHashtable.remove (section);
}
|