[LON-CAPA-cvs] cvs: loncom /debugging_tools create_db.c

albertel lon-capa-cvs@mail.lon-capa.org
Wed, 20 Sep 2006 18:35:29 -0000


albertel		Wed Sep 20 14:35:29 2006 EDT

  Added files:                 
    /loncom/debugging_tools	create_db.c 
  Log:
  - c-level db creation 
  
  

Index: loncom/debugging_tools/create_db.c
+++ loncom/debugging_tools/create_db.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gdbm.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>

static const char x2c_table[] = "0123456789abcdef";

char x2c(char *what,unsigned int offset)
{
  char c=0;
  if (isalpha(what[offset])) {
    c = ((what[offset]-'a'+10) << 4);
  } else {
    c = ((what[offset]-'0') << 4);
  }
  if (isalpha(what[offset+1])) {
    c |= (what[offset+1]-'a'+10);
  } else {
    c |= (what[offset+1]-'0');
  }
  return c;
}

datum* http_unescape(datum* escaped_data)
{
  datum *unescaped_data;
  unescaped_data = malloc(sizeof(datum));
  unescaped_data->dsize = escaped_data->dsize;
  unescaped_data->dptr  = malloc(unescaped_data->dsize);
  unsigned int c,i,j;

  j=0;
  for (i=0;i<escaped_data->dsize;i++) {
    c = escaped_data->dptr[i];
    if (c == '%') {
      i++;
      unescaped_data->dptr[j] = x2c(escaped_data->dptr,i);
      i+=2;
      j++;
    } else {
      unescaped_data->dptr[j] = c;
      j++;
    }
  }
  unescaped_data->dsize=j;
  return unescaped_data;
}

int char_http_unescape(char* escaped_data)
{
  char *unescaped_data=escaped_data;
  unsigned int c,i,j;

  j=0;
  for (i=0;escaped_data[i] != '\0';i++) {
    c = escaped_data[i];
    if (c == '%') {
      i++;
      unescaped_data[j] = x2c(escaped_data,i);
      i++;
      j++;
    } else {
      unescaped_data[j] = c;
      j++;
    }
  }
  unescaped_data[j]='\0';
  return j;
}

void usage()
{
  printf("\nUsage:\ngdbm_create -f <gdbm file to create>\n");
}

void write_db(char *filename)
{
  GDBM_FILE db;
  char* line = NULL;
  char* data = NULL;
  size_t len = 0;
  ssize_t read;
  datum key, content;
  int size;
  db = gdbm_open(filename, 0, GDBM_NEWDB, 0640, 0);

  if (db == NULL) {
    printf("ERROR:Unable to open db %s because of %s (%d) -- %s (%d)\n",
	   filename,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
	   errno);
    return;
  }

  while ( (read = getline(&line,&len,stdin)) != -1 ) {
    line[read-1]='\0';
    data=index(line,' ');
    data[0]='\0';
    data+=4;
    //printf("%s >- %s\n",line,data);
    //printf("%s >- %s\n",char_http_unescape(line),char_http_unescape(data));
    
    size = char_http_unescape(line);
    key.dptr=line;
    key.dsize=size;
    size = char_http_unescape(data);
    content.dptr=data;
    content.dsize=size;
    if (0 != (gdbm_store(db, key, content, GDBM_REPLACE))) {
      printf("ERROR:Unable to create key  %s because of %s (%d) -- %s (%d)\n",
	     line,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
	     errno);
      return;
    }
  }
  free(line);
}

int main(int argc, char  **argv) 
{
  int c;
  char *filename=NULL;
  while (1) {
    c = getopt(argc,argv,"f:");  
    if (c == -1)
      break;
    switch (c) {
    case 'f':
      filename = optarg;
    }
  }

  if (filename == NULL) {
    usage();
    return 1;
  }

  write_db(filename);

  return 0;
}