author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 27 Dec 2013 12:24:39 +0100 | |
changeset 61 | fb2cfcee38bd |
parent 57 | ca4e10daa1c9 |
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> |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
13 |
#include "common.h" |
0 | 14 |
|
15 |
char *db_insert (char *file_name, char *name, char *msg, int mode) |
|
16 |
{ |
|
17 |
GDBM_FILE dbf; |
|
18 |
datum key; |
|
19 |
datum content; |
|
20 |
int ret; |
|
21 |
||
22 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRCREAT, 0644, 0)) == NULL) |
|
23 |
return "db_insert error"; |
|
24 |
||
25 |
key.dptr = name; |
|
26 |
key.dsize = strlen (name) + 1; |
|
27 |
||
28 |
content.dptr = msg; |
|
29 |
content.dsize = strlen (msg) + 1; |
|
30 |
||
31 |
if (mode) |
|
32 |
ret = gdbm_store (dbf, key, content, GDBM_REPLACE); |
|
33 |
else |
|
34 |
ret = gdbm_store (dbf, key, content, GDBM_INSERT); |
|
35 |
||
36 |
gdbm_close (dbf); |
|
37 |
||
38 |
if (!ret) |
|
39 |
return "Key added"; |
|
40 |
||
41 |
return "error: key not added"; |
|
42 |
} |
|
43 |
||
44 |
char *db_remove (char *file_name, char *name) |
|
45 |
{ |
|
46 |
GDBM_FILE dbf; |
|
47 |
datum key; |
|
48 |
int ret; |
|
49 |
||
50 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL) |
|
51 |
return "db_remove error"; |
|
52 |
||
53 |
key.dptr = name; |
|
54 |
key.dsize = strlen (name) + 1; |
|
55 |
||
56 |
ret = gdbm_delete (dbf, key); |
|
57 |
||
58 |
gdbm_close (dbf); |
|
59 |
||
60 |
if (!ret) |
|
61 |
return "Key removed"; |
|
62 |
return "error: key not removed"; |
|
63 |
} |
|
64 |
||
65 |
char *db_lookup (char *file_name, char *entry) |
|
66 |
{ |
|
67 |
GDBM_FILE dbf; |
|
68 |
datum content; |
|
69 |
datum key; |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
70 |
static char msg[DEFAULT_BUF_SIZE] = { 0 }; |
0 | 71 |
|
72 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
73 |
return "db_lookup error"; |
|
74 |
||
75 |
key.dptr = entry; |
|
76 |
key.dsize = strlen (entry) + 1; |
|
77 |
||
78 |
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
|
79 |
if (content.dptr != NULL) { |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
80 |
snprintf (msg, sizeof (msg), "%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
|
81 |
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
|
82 |
} else |
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
83 |
snprintf (msg, sizeof (msg), "I haven't heard anything about %s.", entry); |
0 | 84 |
|
85 |
gdbm_close (dbf); |
|
86 |
return msg; |
|
87 |
} |
|
88 |
||
89 |
char *db_elements (char *file_name) |
|
90 |
{ |
|
91 |
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
|
92 |
datum key, nextkey; |
0 | 93 |
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
|
94 |
static char msg[81] = { 0 }; |
0 | 95 |
|
96 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
97 |
return "db_lookup error"; |
|
98 |
||
99 |
key = gdbm_firstkey (dbf); |
|
100 |
||
101 |
count = 0; |
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
key = nextkey; |
0 | 106 |
count++; |
107 |
} |
|
108 |
||
109 |
gdbm_close (dbf); |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
110 |
snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row"); |
0 | 111 |
return msg; |
112 |
} |
|
113 |
||
114 |
char *db_list (char *file_name) |
|
115 |
{ |
|
116 |
GDBM_FILE dbf; |
|
117 |
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
|
118 |
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
|
119 |
static char msg[81] = { 0 }; |
0 | 120 |
int count; |
121 |
||
122 |
if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL) |
|
123 |
return "db_list error"; |
|
124 |
||
125 |
key = gdbm_firstkey (dbf); |
|
126 |
||
127 |
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
|
128 |
while (key.dptr != NULL) { |
0 | 129 |
content = gdbm_fetch (dbf, key); |
130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
key = nextkey; |
0 | 136 |
count++; |
137 |
} |
|
138 |
||
139 |
gdbm_close (dbf); |
|
33
56571d34d754
safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
20
diff
changeset
|
140 |
snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row"); |
0 | 141 |
return msg; |
142 |
} |
|
143 |
||
144 |
char *db_vaccuum (char *file_name) |
|
145 |
{ |
|
146 |
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
|
147 |
static char msg[81] = { 0 }; |
0 | 148 |
|
149 |
if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL) |
|
150 |
return "db_vaccuum error"; |
|
151 |
||
152 |
gdbm_reorganize (dbf); |
|
153 |
||
154 |
gdbm_close (dbf); |
|
57
ca4e10daa1c9
vaccuum spelling error
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
155 |
snprintf (msg, sizeof (msg), "I have reorganized the database."); |
0 | 156 |
return msg; |
157 |
} |