author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 15:42:20 +0100 | |
changeset 30 | eed9f710ba3b |
parent 20 | b94039112f28 |
child 33 | 56571d34d754 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: database.c 108 2008-04-17 01:01:52Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/database.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
#include <gdbm.h> |
|
11 |
||
12 |
#include <database.h> |
|
13 |
||
14 |
char *db_insert (char *file_name, char *name, char *msg, int mode) |
|
15 |
{ |
|
16 |
GDBM_FILE dbf; |
|
17 |
datum key; |
|
18 |
datum content; |
|
19 |
int ret; |
|
20 |
||
21 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRCREAT, 0644, 0)) == NULL) |
|
22 |
return "db_insert error"; |
|
23 |
||
24 |
key.dptr = name; |
|
25 |
key.dsize = strlen (name) + 1; |
|
26 |
||
27 |
content.dptr = msg; |
|
28 |
content.dsize = strlen (msg) + 1; |
|
29 |
||
30 |
if (mode) |
|
31 |
ret = gdbm_store (dbf, key, content, GDBM_REPLACE); |
|
32 |
else |
|
33 |
ret = gdbm_store (dbf, key, content, GDBM_INSERT); |
|
34 |
||
35 |
gdbm_close (dbf); |
|
36 |
||
37 |
if (!ret) |
|
38 |
return "Key added"; |
|
39 |
||
40 |
return "error: key not added"; |
|
41 |
} |
|
42 |
||
43 |
char *db_remove (char *file_name, char *name) |
|
44 |
{ |
|
45 |
GDBM_FILE dbf; |
|
46 |
datum key; |
|
47 |
int ret; |
|
48 |
||
49 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL) |
|
50 |
return "db_remove error"; |
|
51 |
||
52 |
key.dptr = name; |
|
53 |
key.dsize = strlen (name) + 1; |
|
54 |
||
55 |
ret = gdbm_delete (dbf, key); |
|
56 |
||
57 |
gdbm_close (dbf); |
|
58 |
||
59 |
if (!ret) |
|
60 |
return "Key removed"; |
|
61 |
return "error: key not removed"; |
|
62 |
} |
|
63 |
||
64 |
char *db_lookup (char *file_name, char *entry) |
|
65 |
{ |
|
66 |
GDBM_FILE dbf; |
|
67 |
datum content; |
|
68 |
datum key; |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
69 |
static char msg[513] = { 0 }; |
0 | 70 |
|
71 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
72 |
return "db_lookup error"; |
|
73 |
||
74 |
key.dptr = entry; |
|
75 |
key.dsize = strlen (entry) + 1; |
|
76 |
||
77 |
content = gdbm_fetch (dbf, key); |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
78 |
if (content.dptr != NULL) { |
0 | 79 |
snprintf (msg, 512, "%s", content.dptr); |
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
80 |
free (content.dptr); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
81 |
} else |
0 | 82 |
snprintf (msg, 512, "I haven't heard anything about %s.", entry); |
83 |
||
84 |
gdbm_close (dbf); |
|
85 |
return msg; |
|
86 |
} |
|
87 |
||
88 |
char *db_elements (char *file_name) |
|
89 |
{ |
|
90 |
GDBM_FILE dbf; |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
91 |
datum key, nextkey; |
0 | 92 |
int count; |
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
93 |
static char msg[81] = { 0 }; |
0 | 94 |
|
95 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
96 |
return "db_lookup error"; |
|
97 |
||
98 |
key = gdbm_firstkey (dbf); |
|
99 |
||
100 |
count = 0; |
|
101 |
while (key.dptr != NULL) { |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
102 |
nextkey = gdbm_nextkey (dbf, key); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
103 |
free (key.dptr); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
104 |
key = nextkey; |
0 | 105 |
count++; |
106 |
} |
|
107 |
||
108 |
gdbm_close (dbf); |
|
109 |
snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row"); |
|
110 |
return msg; |
|
111 |
} |
|
112 |
||
113 |
char *db_list (char *file_name) |
|
114 |
{ |
|
115 |
GDBM_FILE dbf; |
|
116 |
datum content; |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
117 |
datum key, nextkey; |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
118 |
static char msg[81] = { 0 }; |
0 | 119 |
int count; |
120 |
||
121 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
122 |
return "db_list error"; |
|
123 |
||
124 |
key = gdbm_firstkey (dbf); |
|
125 |
||
126 |
count = 0; |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
127 |
while (key.dptr != NULL) { |
0 | 128 |
content = gdbm_fetch (dbf, key); |
129 |
printf ("%11s: %s\n", key.dptr, content.dptr); |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
130 |
if (content.dptr != NULL) |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
131 |
free (content.dptr); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
132 |
nextkey = gdbm_nextkey (dbf, key); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
133 |
free (key.dptr); |
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
134 |
key = nextkey; |
0 | 135 |
count++; |
136 |
} |
|
137 |
||
138 |
gdbm_close (dbf); |
|
139 |
snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row"); |
|
140 |
return msg; |
|
141 |
} |
|
142 |
||
143 |
char *db_vaccuum (char *file_name) |
|
144 |
{ |
|
145 |
GDBM_FILE dbf; |
|
20
b94039112f28
database.c and config.c were responsible for the leaks...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
146 |
static char msg[81] = { 0 }; |
0 | 147 |
|
148 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL) |
|
149 |
return "db_vaccuum error"; |
|
150 |
||
151 |
gdbm_reorganize (dbf); |
|
152 |
||
153 |
gdbm_close (dbf); |
|
154 |
snprintf (msg, 80, "I reorganized the database."); |
|
155 |
return msg; |
|
156 |
} |