annotate libutil/dbread.c @ 27:ca8a6f95826a

implemented sws-card-lookup and underlying libutil functions
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 16 Mar 2021 23:22:37 +0000
parents
children fa81221ac9b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements functions for reading key=value database files.
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <ctype.h>
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "dbread.h"
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 static
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 parse_read_line(dbs, filename_for_errs, lineno_for_errs)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 struct dbread_state *dbs;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char *filename_for_errs;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 int lineno_for_errs;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned n;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *cp, *np;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 n = 0;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 for (cp = dbs->linebuf; ; ) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 while (isspace(*cp))
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 cp++;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (!*cp)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 break;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (*cp == '=') {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n",
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 filename_for_errs, lineno_for_errs);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 for (np = cp; ; cp++) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (*cp == '=')
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 break;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (!*cp || isspace(*cp))
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 goto inv_syntax;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 *cp++ = '\0';
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (n >= DBREAD_MAX_KV_PAIRS) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr,
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 "%s line %d: too many key=value pairs\n",
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 filename_for_errs, lineno_for_errs);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 dbs->kv_pairs[n].key = np;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 dbs->kv_pairs[n].value = cp;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 n++;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 while (*cp && !isspace(*cp))
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 cp++;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (*cp)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 *cp++ = '\0';
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 dbs->num_kv_pairs = n;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(0);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 char *
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 dbread_find_key(dbs, sought_key)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 struct dbread_state *dbs;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 char *sought_key;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 unsigned n;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 for (n = 0; n < dbs->num_kv_pairs; n++)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (!strcasecmp(dbs->kv_pairs[n].key, sought_key))
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return dbs->kv_pairs[n].value;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 return 0;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 dbread_find_record(filename, dbs, sought_key, sought_value)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 char *filename, *sought_key, *sought_value;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 struct dbread_state *dbs;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 FILE *inf;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 int lineno;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 char *cp;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 inf = fopen(filename, "r");
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (!inf) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 perror(filename);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 for (lineno = 1; fgets(dbs->linebuf, DBREAD_LINEBUF_SIZE, inf);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 lineno++) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (!index(dbs->linebuf, '\n')) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 fprintf(stderr,
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 "%s line %d: too long or missing newline\n",
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 filename, lineno);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 fclose(inf);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (parse_read_line(dbs, filename, lineno) < 0) {
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 /* error msg already printed */
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 fclose(inf);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 cp = dbread_find_key(dbs, sought_key);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 if (!cp)
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 continue;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (strcmp(cp, sought_value))
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 continue;
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 /* found our record: struct dbread_state is our return */
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 fclose(inf);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 return(0);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 fclose(inf);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fprintf(stderr, "error: %s=%s not found in %s\n", sought_key,
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 sought_value, filename);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 return(-1);
ca8a6f95826a implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }