src/database.c
changeset 0 586472add385
child 20 b94039112f28
equal deleted inserted replaced
-1:000000000000 0:586472add385
       
     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;
       
    69     static char msg[513];
       
    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);
       
    78     if (content.dptr != NULL)
       
    79         snprintf (msg, 512, "%s", content.dptr);
       
    80     else
       
    81         snprintf (msg, 512, "I haven't heard anything about %s.", entry);
       
    82 
       
    83     gdbm_close (dbf);
       
    84     return msg;
       
    85 }
       
    86 
       
    87 char *db_elements (char *file_name)
       
    88 {
       
    89     GDBM_FILE dbf;
       
    90     datum key;
       
    91     int count;
       
    92     static char msg[81];
       
    93 
       
    94     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
       
    95         return "db_lookup error";
       
    96 
       
    97     key = gdbm_firstkey (dbf);
       
    98 
       
    99     count = 0;
       
   100     while (key.dptr != NULL) {
       
   101         key = gdbm_nextkey (dbf, key);
       
   102         count++;
       
   103     }
       
   104 
       
   105     gdbm_close (dbf);
       
   106     snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
       
   107     return msg;
       
   108 }
       
   109 
       
   110 char *db_list (char *file_name)
       
   111 {
       
   112     GDBM_FILE dbf;
       
   113     datum content;
       
   114     datum key;
       
   115     static char msg[81];
       
   116     int count;
       
   117 
       
   118     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
       
   119         return "db_list error";
       
   120 
       
   121     key = gdbm_firstkey (dbf);
       
   122 
       
   123     count = 0;
       
   124     while (key.dptr) {
       
   125         content = gdbm_fetch (dbf, key);
       
   126         printf ("%11s: %s\n", key.dptr, content.dptr);
       
   127         free (content.dptr);
       
   128         key = gdbm_nextkey (dbf, key);
       
   129         count++;
       
   130     }
       
   131 
       
   132     gdbm_close (dbf);
       
   133     snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
       
   134     return msg;
       
   135 }
       
   136 
       
   137 char *db_vaccuum (char *file_name)
       
   138 {
       
   139     GDBM_FILE dbf;
       
   140     static char msg[81];
       
   141 
       
   142     if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL)
       
   143         return "db_vaccuum error";
       
   144 
       
   145     gdbm_reorganize (dbf);
       
   146 
       
   147     gdbm_close (dbf);
       
   148     snprintf (msg, 80, "I reorganized the database.");
       
   149     return msg;
       
   150 }