* Copyright (c) 1992, Brian Berliner
* You may distribute under the terms of the GNU General Public License as
* specified in the README file that comes with the CVS 1.3 kit.
* A simple ndbm-emulator for CVS. It parses a text file of the format:
* at dbm_open time, and loads the entire file into memory. As such, it is
* probably only good for fairly small modules files. Ours is about 30K in
* size, and this code works fine.
static char rcsid
[] = "@(#)myndbm.c 1.5 92/03/31";
static void mydbm_load_file ();
mydbm_open (file
, flags
, mode
)
if ((fp
= fopen (file
, "r")) == NULL
)
db
= (DBM
*) xmalloc (sizeof (*db
));
db
->dbm_list
= getlist ();
mydbm_load_file (fp
, db
->dbm_list
);
/* make sure it's null-terminated */
s
= xmalloc (key
.dsize
+ 1);
(void) strncpy (s
, key
.dptr
, key
.dsize
);
p
= findnode (db
->dbm_list
, s
);
val
.dsize
= strlen (p
->data
);
val
.dptr
= (char *) NULL
;
head
= db
->dbm_list
->list
;
key
.dsize
= strlen (p
->key
);
key
.dptr
= (char *) NULL
;
head
= db
->dbm_list
->list
;
key
.dsize
= strlen (p
->key
);
key
.dptr
= (char *) NULL
;
mydbm_load_file (fp
, list
)
char line
[MAXLINELEN
], value
[MAXLINELEN
];
for (cont
= 0; fgets (line
, sizeof (line
), fp
) != NULL
;)
if ((cp
= rindex (line
, '\n')) != NULL
)
*cp
= '\0'; /* strip the newline */
* Add the line to the value, at the end if this is a continuation
* line; otherwise at the beginning, but only after any trailing
* See if the line we read is a continuation line, and strip the
(void) strcpy (vp
, line
);
continue; /* comment line */
while (*vp
&& isspace (*vp
))
continue; /* empty line */
* If this was not a continuation line, add the entry to the database
while (*vp
&& !isspace (*vp
))
*vp
++ = '\0'; /* NULL terminate the key */
while (*vp
&& isspace (*vp
))
vp
++; /* skip whitespace to value */
error (0, 0, "warning: NULL value for key `%s'", p
->key
);
if (addnode (list
, p
) == -1)
error (0, 0, "duplicate key found for `%s'", p
->key
);