File: SProperties.cpp

package info (click to toggle)
yudit 2.5.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,528 kB
  • ctags: 8,403
  • sloc: cpp: 59,394; ansic: 2,585; perl: 2,398; makefile: 864; sh: 321
file content (211 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download
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
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 2002  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2001  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2000  Gaspar Sinai <gsinai@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 <gsinai@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;
  while (ssize > from)
  {
    /* This is just a splitting rourtine.  We can not use 
       the Vecotr'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] == '#')
    {
      continue;
    }
    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);
    current.clear();

    if (kvle.size()==0)
    {
      continue;
    }
    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 ret;
  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);
      SString line (k);
      line.append ("=");
      line.append (*value);
      ret.append (line);
    }
  }
  ret.sort();
  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. returnning 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);
}