Debian Codegraph @

libhylafax/FaxDB.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "FaxDB.h"
27 #include "RE.h"
28 #include "NLS.h"
29
30
31 FaxDBRecord::FaxDBRecord()
32 {
33 parent = 0;
34 }
35
36 FaxDBRecord::FaxDBRecord(FaxDBRecord* other)
37 {
38 if ((parent = other))
39 parent->inc();
40 }
41
42 FaxDBRecord::~FaxDBRecord()
43 {
44 if (parent)
45 parent->dec();
46 }
47
48 const char* FaxDBRecord::className() const { return "FaxDBRecord"; }
49
50 const fxStr FaxDBRecord::nullStr("");
51
52 const fxStr&
53 FaxDBRecord::find(const fxStr& key)
54 {
55 const fxStr* s = 0;
56 FaxDBRecord* rec = this;
57 for (; rec && !(s = rec->dict.find(key)); rec = rec->parent)
58 ;
59 return (s ? *s : nullStr);
60 }
61
62 void FaxDBRecord::set(const fxStr& key, const fxStr& value)
63 { dict[key] = value; }
64
65 fxIMPLEMENT_StrKeyObjValueDictionary(FaxValueDict, fxStr)
66
67 FaxDB::FaxDB(const fxStr& file) : filename(file)
68 {
69 FILE* fd = fopen(file, "r");
70 if (fd) {
71 lineno = 0;
72 parseDatabase(fd, 0);
73 fclose(fd);
74 }
75 }
76
77 FaxDB::~FaxDB()
78 {
79 }
80
81 const char* FaxDB::className() const { return "FaxDB"; }
82
83 fxStr FaxDB::nameKey("Name");
84 fxStr FaxDB::numberKey("FAX-Number");
85 fxStr FaxDB::companyKey("Company");
86 fxStr FaxDB::locationKey("Location");
87 fxStr FaxDB::phoneKey("Voice-Number");
88 fxStr FaxDB::mailAddrKey("Mail-Address");
89
90 FaxDBRecord*
91 FaxDB::find(const fxStr& s, fxStr* name)
92 {
93 fxStr canon(s);
94 canon.lowercase();
95 for (u_int l = 0; l < canon.length(); l = canon.next(l, "+?*[].\\")) {
96 canon.insert('\\', l);
97 l += 2;
98 }
99 RE pat(canon);
100 for (FaxInfoDictIter iter(dict); iter.notDone(); iter++) {
101 fxStr t(iter.key());
102 t.lowercase();
103 if (pat.Find(t)) {
104 if (name)
105 *name = iter.key();
106 return (iter.value());
107 }
108 }
109 return (0);
110 }
111
112 FaxDBRecord* FaxDB::operator[](const fxStr& name) { return dict[name]; }
113 const fxStr& FaxDB::getFilename() { return filename; }
114 FaxInfoDict& FaxDB::getDict() { return dict; }
115 void FaxDB::add(const fxStr& key, FaxDBRecord* r) { dict[key] = r; }
116
117 void
118 FaxDB::parseDatabase(FILE* fd, FaxDBRecord* parent)
119 {
120 FaxDBRecordPtr rec(new FaxDBRecord(parent));
121 fxStr key;
122 while (getToken(fd, key)) {
123 if (key == "]") {
124 if (parent == 0)
125 fprintf(stderr, NLS::TEXT("%s: line %d: Unmatched \"]\".\n"),
126 (const char*) filename, lineno);
127 break;
128 }
129 if (key == "[") {
130 parseDatabase(fd, rec); // recurse to form hierarchy
131 continue;
132 }
133 fxStr value;
134 if (!getToken(fd, value))
135 break;
136 if (value != ":") {
137 fprintf(stderr, NLS::TEXT("%s: line %d: Missing \":\" separator.\n"),
138 (const char*) filename, lineno);
139 continue;
140 }
141 if (!getToken(fd, value))
142 break;
143 rec->set(key, value);
144 if (key == nameKey) // XXX what about duplicates?
145 add(value, rec);
146 }
147 }
148
149 #include "StackBuffer.h"
150 #include <ctype.h>
151
152 bool
153 FaxDB::getToken(FILE* fd, fxStr& token)
154 {
155 int c;
156 top:
157 if ((c = getc(fd)) == EOF)
158 return (false);
159 while (isspace(c)) {
160 if (c == '\n')
161 lineno++;
162 c = getc(fd);
163 }
164 if (c == '#') {
165 while ((c = getc(fd)) != EOF && c != '\n')
166 ;
167 if (c == EOF)
168 return (false);
169 lineno++;
170 goto top;
171 }
172 if (c == '[' || c == ']' || c == ':') {
173 char buf[2];
174 buf[0] = c;
175 buf[1] = '\0';
176 token = buf;
177 return (true);
178 }
179 fxStackBuffer buf;
180 if (c == '"') {
181 while ((c = getc(fd)) != EOF) {
182 if (c == '\\') {
183 c = getc(fd);
184 if (c == EOF) {
185 fprintf(stderr, NLS::TEXT("%s: Premature EOF.\n"), (const char*) filename);
186 return (false);
187 }
188 // XXX handle standard escapes
189 if (c == '\n')
190 lineno++;
191 } else {
192 if (c == '"')
193 break;
194 if (c == '\n')
195 lineno++;
196 }
197 buf.put(c);
198 }
199 } else {
200 do
201 buf.put(c);
202 while ((c = getc(fd)) != EOF && !isspace(c) &&
203 c != ':' && c != ']' && c != '[' && c != '#');
204 if (c != EOF)
205 ungetc(c, fd);
206 }
207 buf.set('\0');
208 token = (const char*) buf;
209 return (true);
210 }
211
212 #ifdef notdef
213 void
214 FaxDB::write()
215 {
216 fxStr temp(filename | "#");
217 FILE* fp = fopen(temp, "w");
218 if (fp) {
219 write(fp);
220 fclose(fp);
221 ::rename(temp, filename);
222 }
223 }
224
225 void
226 FaxDB::write(FILE* fp)
227 {
228 for (FaxInfoDictIter iter(dict); iter.notDone(); iter++) {
229 fprintf(fp, "[ Name: \"%s\"\n", (char*) iter.key());
230 FaxDBRecord* r = iter.value();
231 const fxStr&
232 fprintf(fp, "]\n");
233 }
234 }
235 #endif
236
237 fxIMPLEMENT_StrKeyPtrValueDictionary(FaxInfoDict, FaxDBRecord*)