src/database.c
changeset 33 56571d34d754
parent 20 b94039112f28
child 57 ca4e10daa1c9
equal deleted inserted replaced
32:89fb9c6611ca 33:56571d34d754
     8 #include <stdlib.h>
     8 #include <stdlib.h>
     9 #include <string.h>
     9 #include <string.h>
    10 #include <gdbm.h>
    10 #include <gdbm.h>
    11 
    11 
    12 #include <database.h>
    12 #include <database.h>
       
    13 #include "common.h"
    13 
    14 
    14 char *db_insert (char *file_name, char *name, char *msg, int mode)
    15 char *db_insert (char *file_name, char *name, char *msg, int mode)
    15 {
    16 {
    16     GDBM_FILE dbf;
    17     GDBM_FILE dbf;
    17     datum key;
    18     datum key;
    64 char *db_lookup (char *file_name, char *entry)
    65 char *db_lookup (char *file_name, char *entry)
    65 {
    66 {
    66     GDBM_FILE dbf;
    67     GDBM_FILE dbf;
    67     datum content;
    68     datum content;
    68     datum key;
    69     datum key;
    69     static char msg[513] = { 0 };
    70     static char msg[DEFAULT_BUF_SIZE] = { 0 };
    70 
    71 
    71     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
    72     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
    72         return "db_lookup error";
    73         return "db_lookup error";
    73 
    74 
    74     key.dptr = entry;
    75     key.dptr = entry;
    75     key.dsize = strlen (entry) + 1;
    76     key.dsize = strlen (entry) + 1;
    76 
    77 
    77     content = gdbm_fetch (dbf, key);
    78     content = gdbm_fetch (dbf, key);
    78     if (content.dptr != NULL) {
    79     if (content.dptr != NULL) {
    79         snprintf (msg, 512, "%s", content.dptr);
    80         snprintf (msg, sizeof (msg), "%s", content.dptr);
    80         free (content.dptr);
    81         free (content.dptr);
    81     } else
    82     } else
    82         snprintf (msg, 512, "I haven't heard anything about %s.", entry);
    83         snprintf (msg, sizeof (msg), "I haven't heard anything about %s.", entry);
    83 
    84 
    84     gdbm_close (dbf);
    85     gdbm_close (dbf);
    85     return msg;
    86     return msg;
    86 }
    87 }
    87 
    88 
   104         key = nextkey;
   105         key = nextkey;
   105         count++;
   106         count++;
   106     }
   107     }
   107 
   108 
   108     gdbm_close (dbf);
   109     gdbm_close (dbf);
   109     snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
   110     snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
   110     return msg;
   111     return msg;
   111 }
   112 }
   112 
   113 
   113 char *db_list (char *file_name)
   114 char *db_list (char *file_name)
   114 {
   115 {
   134         key = nextkey;
   135         key = nextkey;
   135         count++;
   136         count++;
   136     }
   137     }
   137 
   138 
   138     gdbm_close (dbf);
   139     gdbm_close (dbf);
   139     snprintf (msg, 80, "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
   140     snprintf (msg, sizeof (msg), "I am holding %d %s in my database.", count, (count > 0) ? "rows" : "row");
   140     return msg;
   141     return msg;
   141 }
   142 }
   142 
   143 
   143 char *db_vaccuum (char *file_name)
   144 char *db_vaccuum (char *file_name)
   144 {
   145 {
   149         return "db_vaccuum error";
   150         return "db_vaccuum error";
   150 
   151 
   151     gdbm_reorganize (dbf);
   152     gdbm_reorganize (dbf);
   152 
   153 
   153     gdbm_close (dbf);
   154     gdbm_close (dbf);
   154     snprintf (msg, 80, "I reorganized the database.");
   155     snprintf (msg, sizeof (msg), "I reorganized the database.");
   155     return msg;
   156     return msg;
   156 }
   157 }