File:  [LON-CAPA] / loncom / debugging_tools / dump_db.c
Revision 1.2: download - view: text, annotated - select for diffs
Thu Aug 24 22:46:30 2006 UTC (18 years, 4 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_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, 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
- adding scripts to allow morphing 32bit to 64 bit db

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gdbm.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>

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

void c2x(unsigned what, unsigned char prefix, char *where,
	 unsigned int offset)
{
    where[offset] = prefix;
    where[offset+1] = c2x_table[what >> 4];
    where[offset+2] = c2x_table[what & 0xf];
}


datum* http_escape(datum* data)
{
  datum *escaped_data;
  escaped_data = malloc(sizeof(datum));
  escaped_data->dsize =  (3 * data->dsize) + 3;
  escaped_data->dptr  = malloc(escaped_data->dsize);
  unsigned int c,i,j;

  j=0;
  for (i=0;i<data->dsize;i++) {
    c = data->dptr[i];
    if (!isalnum(c)) {
      c2x(c, '%', escaped_data->dptr, j);
      j+=3;
    } else {
      escaped_data->dptr[j] = c;
      j++;
    }
  }
  escaped_data->dsize=j;
  return escaped_data;
}


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

void read_db(char *filename)
{
  GDBM_FILE db;
  datum key, nextkey, content;
  datum *escaped_key, *escaped_content;
  db = gdbm_open(filename, 0, GDBM_READER, 0, 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;
  }
  key = gdbm_firstkey(db);

  while ( key.dptr ) {
    content = gdbm_fetch(db, key);
    escaped_key     = http_escape(&key);
    escaped_content = http_escape(&content);
    fwrite(escaped_key->dptr,
	   escaped_key->dsize, sizeof(char), stdout);
    printf(" -> ");
    fwrite(escaped_content->dptr,
	   escaped_content->dsize, sizeof(char), stdout);
    printf("\n");
    free(content.dptr);
    free(escaped_content->dptr);
    free(escaped_content);
    free(escaped_key->dptr);
    free(escaped_key);
    nextkey = gdbm_nextkey(db, key);
    free(key.dptr);
    key = nextkey;
  }
}

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

  read_db(filename);

  return 0;
}



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