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 (18 years, 3 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_5_msu, version_2_11_5, 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

#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;
}


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