File:  [LON-CAPA] / loncom / debugging_tools / create_db.c
Revision 1.1: download - view: text, annotated - select for diffs
Wed Sep 20 18:35:29 2006 UTC (17 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
- c-level db creation

    1: #define _GNU_SOURCE
    2: #include <stdio.h>
    3: #include <stdlib.h>
    4: #include <unistd.h>
    5: #include <gdbm.h>
    6: #include <errno.h>
    7: #include <string.h>
    8: #include <ctype.h>
    9: 
   10: static const char x2c_table[] = "0123456789abcdef";
   11: 
   12: char x2c(char *what,unsigned int offset)
   13: {
   14:   char c=0;
   15:   if (isalpha(what[offset])) {
   16:     c = ((what[offset]-'a'+10) << 4);
   17:   } else {
   18:     c = ((what[offset]-'0') << 4);
   19:   }
   20:   if (isalpha(what[offset+1])) {
   21:     c |= (what[offset+1]-'a'+10);
   22:   } else {
   23:     c |= (what[offset+1]-'0');
   24:   }
   25:   return c;
   26: }
   27: 
   28: datum* http_unescape(datum* escaped_data)
   29: {
   30:   datum *unescaped_data;
   31:   unescaped_data = malloc(sizeof(datum));
   32:   unescaped_data->dsize = escaped_data->dsize;
   33:   unescaped_data->dptr  = malloc(unescaped_data->dsize);
   34:   unsigned int c,i,j;
   35: 
   36:   j=0;
   37:   for (i=0;i<escaped_data->dsize;i++) {
   38:     c = escaped_data->dptr[i];
   39:     if (c == '%') {
   40:       i++;
   41:       unescaped_data->dptr[j] = x2c(escaped_data->dptr,i);
   42:       i+=2;
   43:       j++;
   44:     } else {
   45:       unescaped_data->dptr[j] = c;
   46:       j++;
   47:     }
   48:   }
   49:   unescaped_data->dsize=j;
   50:   return unescaped_data;
   51: }
   52: 
   53: int char_http_unescape(char* escaped_data)
   54: {
   55:   char *unescaped_data=escaped_data;
   56:   unsigned int c,i,j;
   57: 
   58:   j=0;
   59:   for (i=0;escaped_data[i] != '\0';i++) {
   60:     c = escaped_data[i];
   61:     if (c == '%') {
   62:       i++;
   63:       unescaped_data[j] = x2c(escaped_data,i);
   64:       i++;
   65:       j++;
   66:     } else {
   67:       unescaped_data[j] = c;
   68:       j++;
   69:     }
   70:   }
   71:   unescaped_data[j]='\0';
   72:   return j;
   73: }
   74: 
   75: void usage()
   76: {
   77:   printf("\nUsage:\ngdbm_create -f <gdbm file to create>\n");
   78: }
   79: 
   80: void write_db(char *filename)
   81: {
   82:   GDBM_FILE db;
   83:   char* line = NULL;
   84:   char* data = NULL;
   85:   size_t len = 0;
   86:   ssize_t read;
   87:   datum key, content;
   88:   int size;
   89:   db = gdbm_open(filename, 0, GDBM_NEWDB, 0640, 0);
   90: 
   91:   if (db == NULL) {
   92:     printf("ERROR:Unable to open db %s because of %s (%d) -- %s (%d)\n",
   93: 	   filename,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
   94: 	   errno);
   95:     return;
   96:   }
   97: 
   98:   while ( (read = getline(&line,&len,stdin)) != -1 ) {
   99:     line[read-1]='\0';
  100:     data=index(line,' ');
  101:     data[0]='\0';
  102:     data+=4;
  103:     //printf("%s >- %s\n",line,data);
  104:     //printf("%s >- %s\n",char_http_unescape(line),char_http_unescape(data));
  105:     
  106:     size = char_http_unescape(line);
  107:     key.dptr=line;
  108:     key.dsize=size;
  109:     size = char_http_unescape(data);
  110:     content.dptr=data;
  111:     content.dsize=size;
  112:     if (0 != (gdbm_store(db, key, content, GDBM_REPLACE))) {
  113:       printf("ERROR:Unable to create key  %s because of %s (%d) -- %s (%d)\n",
  114: 	     line,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
  115: 	     errno);
  116:       return;
  117:     }
  118:   }
  119:   free(line);
  120: }
  121: 
  122: int main(int argc, char  **argv) 
  123: {
  124:   int c;
  125:   char *filename=NULL;
  126:   while (1) {
  127:     c = getopt(argc,argv,"f:");  
  128:     if (c == -1)
  129:       break;
  130:     switch (c) {
  131:     case 'f':
  132:       filename = optarg;
  133:     }
  134:   }
  135: 
  136:   if (filename == NULL) {
  137:     usage();
  138:     return 1;
  139:   }
  140: 
  141:   write_db(filename);
  142: 
  143:   return 0;
  144: }
  145: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>