2 * Copyright (c) 1992, Brian Berliner
4 * You may distribute under the terms of the GNU General Public License as
5 * specified in the README file that comes with the CVS source distribution.
7 * A simple ndbm-emulator for CVS. It parses a text file of the format:
11 * at dbm_open time, and loads the entire file into memory. As such, it is
12 * probably only good for fairly small modules files. Ours is about 30K in
13 * size, and this code works fine.
21 # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
22 # endif /* defined O_ACCMODE */
24 static void mydbm_load_file (FILE *, List *, char *);
26 /* Returns NULL on error in which case errno has been set to indicate
27 the error. Can also call error() itself. */
30 mydbm_open (char *file, int flags, int mode)
35 fp = CVS_FOPEN (file, (flags & O_ACCMODE) != O_RDONLY ?
36 FOPEN_BINARY_READWRITE : FOPEN_BINARY_READ);
37 if (fp == NULL && !(existence_error (errno) && (flags & O_CREAT)))
40 db = (DBM *) xmalloc (sizeof (*db));
41 db->dbm_list = getlist ();
43 db->name = xstrdup (file);
47 mydbm_load_file (fp, db->dbm_list, file);
49 error (0, errno, "cannot close %s", file);
54 static int write_item (Node *, void *);
57 write_item (Node *node, void *data)
59 FILE *fp = (FILE *)data;
60 fputs (node->key, fp);
62 fputs (node->data, fp);
73 fp = CVS_FOPEN (db->name, FOPEN_BINARY_WRITE);
75 error (1, errno, "cannot write %s", db->name);
76 walklist (db->dbm_list, write_item, (void *)fp);
78 error (0, errno, "cannot close %s", db->name);
81 dellist (&db->dbm_list);
86 mydbm_fetch (DBM *db, datum key)
92 /* make sure it's null-terminated */
93 s = xmalloc (key.dsize + 1);
94 (void) strncpy (s, key.dptr, key.dsize);
97 p = findnode (db->dbm_list, s);
101 val.dsize = strlen (p->data);
105 val.dptr = (char *) NULL;
113 mydbm_firstkey (DBM *db)
118 head = db->dbm_list->list;
123 key.dsize = strlen (p->key);
127 key.dptr = (char *) NULL;
130 db->dbm_next = p->next;
135 mydbm_nextkey (DBM *db)
140 head = db->dbm_list->list;
145 key.dsize = strlen (p->key);
149 key.dptr = (char *) NULL;
152 db->dbm_next = p->next;
156 /* Note: only updates the in-memory copy, which is written out at
157 mydbm_close time. Note: Also differs from DBM in that on duplication,
158 it gives a warning, rather than either DBM_INSERT or DBM_REPLACE
161 mydbm_store (DBM *db, datum key, datum value, int flags)
166 node->type = NDBMNODE;
168 node->key = xmalloc (key.dsize + 1);
170 strncat (node->key, key.dptr, key.dsize);
172 node->data = xmalloc (value.dsize + 1);
173 *(char *)node->data = '\0';
174 strncat (node->data, value.dptr, value.dsize);
177 if (addnode (db->dbm_list, node) == -1)
179 error (0, 0, "attempt to insert duplicate key `%s'", node->key);
187 mydbm_load_file (FILE *fp, List *list, char *filename)
190 /* Used in error messages. */
195 size_t value_allocated;
202 value = xmalloc (value_allocated);
206 while( ( line_length =
207 getdelim( &line, &line_size, '\012', fp ) ) >= 0 )
210 if (line_length > 0 && line[line_length - 1] == '\012')
212 /* Strip the newline. */
214 line[line_length] = '\0';
216 if (line_length > 0 && line[line_length - 1] == '\015')
218 /* If the file (e.g. modules) was written on an NT box, it will
219 contain CRLF at the ends of lines. Strip them (we can't do
220 this by opening the file in text mode because we might be
223 line[line_length] = '\0';
227 * Add the line to the value, at the end if this is a continuation
228 * line; otherwise at the beginning, but only after any trailing
229 * backslash is removed.
235 * See if the line we read is a continuation line, and strip the
239 cp = &line[line_length - 1];
252 expand_string (&value,
254 strlen (value) + line_length + 5);
255 strcat (value, line);
258 continue; /* comment line */
260 while (*vp && isspace ((unsigned char) *vp))
263 continue; /* empty line */
266 * If this was not a continuation line, add the entry to the database
270 Node *p = getnode ();
274 while (*vp && !isspace ((unsigned char) *vp))
277 *vp++ = '\0'; /* NULL terminate the key */
279 p->key = xstrdup (kp);
280 while (*vp && isspace ((unsigned char) *vp))
281 vp++; /* skip whitespace to value */
286 "warning: NULL value for key `%s' at line %d of `%s'",
287 p->key, line_num, filename);
291 p->data = xstrdup (vp);
292 if (addnode (list, p) == -1)
296 "duplicate key found for `%s' at line %d of `%s'",
297 p->key, line_num, filename);
302 if (line_length < 0 && !feof (fp))
303 /* FIXME: should give the name of the file. */
304 error (0, errno, "cannot read file in mydbm_load_file");