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