Annotation of loncom/debugging_tools/create_db.c, revision 1.1
1.1 ! albertel 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>