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

albertel lon-capa-cvs@mail.lon-capa.org
Mon, 19 Jun 2006 21:21:47 -0000


albertel		Mon Jun 19 17:21:47 2006 EDT

  Added files:                 
    /loncom/debugging_tools	dump_db.c 
  Log:
  - first pass at a c level database dumper
  
  

Index: loncom/debugging_tools/dump_db.c
+++ loncom/debugging_tools/dump_db.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gdbm.h>
#include <errno.h>
#include <string.h>

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

void read_db(char *filename)
{
  GDBM_FILE db;
  datum key, nextkey, content;
  db = gdbm_open(filename, 0, GDBM_READER, 0, 0);

  if (db == NULL) {
    printf("Unable to open db %s beacsue 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);
    fwrite(key.dptr, key.dsize, sizeof(char), stdout);
    printf(" -> ");
    fwrite(content.dptr, content.dsize, sizeof(char), stdout);
    printf("\n");
    free(content.dptr);
    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;
}