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
|
/**
* 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.
*/
/**
* @author: Gaspar Sinai <gaspar@yudit.org>
* @version: 2000-04-23
*/
#include "SProperties.h"
#include "SExcept.h"
#include <stdio.h>
#include <string.h>
SProperties::SProperties (void) : SHashtable<SString>()
{
}
SProperties::SProperties (const SString& string)
{
/* split into lines */
SStringVector delimiters("\r\n,\n,\r", ",");
SString current;
unsigned int dsize = delimiters.size();
unsigned int ssize = string.size();
int found = (int) string.size();
unsigned int di=0;
unsigned int from=0;
SString comment;
while (ssize > from)
{
/* This is just a splitting rourtine. We can not use
the Vector's split because that purges empty lines.
*/
for (di=0; di<dsize; di++){
found = string.find (delimiters[di], from);
if (found>=0) break;
}
SString s = (di < dsize)
? SString (&string.array()[from], (unsigned int)found - from)
: SString (&string.array()[from], string.size() - from);
from = (di < dsize)
? (unsigned int) found + delimiters[di].size()
: string.size();
if (s.size()>0 && s[0] == '#')
{
/* only pre-ceeding. */
if (current.size()==0)
{
/* last line is not null terminated. */
if (comment.size())
{
comment.append ("\n");
}
comment.append (s);
continue;
}
s.clear();
}
if (s.size()>0 && s[s.size()-1] == '\\')
{
s.truncate (s.size()-1);
current.append (s);
continue;
}
current.append (s);
bool white = true;
for (unsigned j=0; j<current.size(); j++)
{
if (current[j] != ' ' && current[j] != '\t')
{
white = false;
break;
}
}
if (white)
{
current.clear();
continue;
}
SStringVector kvle(current, "=", true);
if (kvle.size()==0)
{
current.clear();
continue;
}
/* no key-value pairs. nothing at all */
if (kvle.size()==1 && kvle[0] == current)
{
current.clear();
continue;
}
current.clear();
/* save comments under property.key.# */
if (comment.size())
{
SString ckey = kvle[0];
ckey.append (".#");
put (ckey, comment);
comment.clear();
}
/* no value, only key exists */
if (kvle.size()==1)
{
put (kvle[0], "");
continue;
}
put (kvle[0], kvle[1]);
}
}
SProperties::SProperties (const SProperties& base) : SHashtable<SString>(base)
{
}
SProperties::~SProperties ()
{
}
SString
SProperties::toString() const
{
SStringVector keys;
for (unsigned int i=0; i<size(); i++)
{
for (unsigned int j=0; j<size(i); j++)
{
const SString* value;
if ((value=get (i,j))==0) continue;
const SString& k = key (i,j);
// Skip comments
if( k.size() > 1 && k[k.size()-2]== '.' && k[k.size()-1] == '#')
{
continue;
}
keys.append (k);
}
}
/* sort them */
keys.sort();
SStringVector ret;
for (unsigned int k=0; k<keys.size(); k++)
{
const SString& key = keys[k];
const SString* value = get(key);
/* restore comments */
SString ckey = key;
ckey.append (".#");
const SString* comment = get (ckey);
if (comment && comment->size() > 0)
{
ret.append (*comment);
}
SString line (key);
line.append ("=");
line.append (*value);
ret.append (line);
}
return ret.join ("\n");
}
SObject*
SProperties::clone() const
{
SProperties* p = new SProperties (*this);
CHECK_NEW(p);
return p;
}
/**
* split a string into an array
* @param s is the string to split
* @param delim is the delimiter list
* @param once is true if we quit after one spli
* retgurn the key or SStringNull. returning key is not yet implemented
*/
SString&
SProperties::split (const SString& s, const SString& delim)
{
int start = 0;
//unsigned int count =0;
/* Find the first delim. */
SString mykey;
while (start < (int) s.size())
{
int current;
int smallest = s.size();
for (unsigned int i=0; i<delim.size(); i++)
{
current = s.find (delim.array()[i], start);
if (current == -1) continue;
if (current < smallest) smallest = current;
}
if (smallest < 0) break;
if (smallest != start)
{
//sub.trunc (smallest);
if (smallest+1 >= (int) s.size()) break;
mykey = SString(s.array(), start, smallest-start);
SString value = SString(s.array(), smallest+1, (int)s.size()-smallest-1);
//count += 2;
put (mykey, value);
break;
}
start = (smallest+1);
}
//return (count==2) ? *new SString(mykey) : SStringNull;
// Just aint working yet...
return SStringNull;
}
/**
* merge the other property into this property
*/
void
SProperties::merge (const SProperties& other)
{
for (unsigned int i=0; i<other.size(); i++)
{
for (unsigned int j=0; j<other.size(i); j++)
{
const SString* value;
if ((value=other.get (i,j))==0) continue;
const SString& key = other.key (i,j);
put (key, *value);
}
}
}
SString
SProperties::getProperty (const SString& prop, const SString& fallback) const
{
const SString* p = get(prop);
if (p==0) p = &fallback;
return SString (*p);
}
|