Changeset 27600

Show
Ignore:
Timestamp:
12.06.2013 17:11:12 (6 years ago)
Author:
ak19
Message:

Two things 1. Moving John's windows (un)locking to new file winlock.cpp and 2. Adding sorting minus option to db2txt to help with diffcol.pl

Location:
main/trunk/greenstone2/common-src/src/gdbmedit/db2txt
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/Makefile.in

    r26821 r27600  
    5858    db2txt.o 
    5959 
     60GSDL_LIBS = \ 
     61    $(COMMON_DIR)/src/lib/gsdllib.a  
     62 
    6063EXECUTABLE_OBJECTS = \ 
    61     $(OBJECTS) 
     64    $(OBJECTS) \ 
     65    $(GSDL_LIBS) 
    6266 
    6367EXECUTABLE = db2txt 
  • main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp

    r27597 r27600  
    3939 
    4040#include "gsdlconf.h" 
     41#include "text_t.h" 
    4142#include <stdlib.h> 
     43#include <cstring> 
     44 
     45// for sorting 
     46#if defined(GSDL_USE_OBJECTSPACE) 
     47#  include <ospace\std\vector> 
     48#  include <ospace\std\algorithm> 
     49#elif defined(GSDL_USE_STL_H) 
     50#  include <vector.h> 
     51#  if defined(GSDL_USE_ALGO_H) 
     52#    include <algo.h> 
     53#  else 
     54#    include <algorithm.h> 
     55#  endif 
     56#else 
     57#  include <vector> 
     58#  include <algorithm> 
     59#endif 
     60 
    4261 
    4362#if defined(GSDL_USE_OBJECTSPACE) 
     
    5877#endif 
    5978 
     79// John Thompson's lock() and unlock() for windows 
     80// parked in separate file to help readability of main code 
     81#include "winlock.cpp" 
     82 
    6083void print_usage (char *program_name) { 
    61   cerr << "usage: " << program_name << " database-name" << endl << endl; 
     84  cerr << "usage: " << program_name << " [options] database-name" << endl << endl; 
     85  cerr << "options:" << endl; 
     86  cerr << " -sort        sort the keys to the database before output" << endl << endl; 
    6287} 
    6388 
     89void print_entry(datum& key, datum& value) 
     90{ 
     91  int i; 
     92  cout << "["; 
     93  for (i = 0; i < key.dsize; ++i) { 
     94    cout << key.dptr[i]; 
     95  } 
     96  cout << "]" << endl; 
    6497 
    65 #ifdef _MSC32_ 
    66 // Windows implementation 
    67 HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); 
    6898 
    69 #else 
    70 // assume Unix 
    71 // lock a file on linux 
    72 // [hs, 2 july 2010] 
    73 // - modified to create a locl file local to the collection [jmt12] 
    74 int lock () 
    75 { 
    76   string file_path (""); 
    77   char *collect_dir = getenv ("GSDLCOLLECTDIR"); 
    78   if (collect_dir != NULL) 
    79   { 
    80     file_path += collect_dir; 
     99  for (i = 0; i < value.dsize; ++i) { 
     100    cout << value.dptr[i]; 
    81101  } 
    82   file_path += "/tmp"; 
    83   if ( access( file_path.c_str(), 00 ) != 0 ) 
    84   { 
    85     mkdir(file_path.c_str(), 00777); 
    86   } 
    87   file_path += "/gdb.lock"; 
    88   ///out << "txt2dbl::lock(" << file_path << ") => "; 
    89   int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644); 
    90   close (fd2); 
    91   int fd = open (file_path.c_str(), O_RDWR); 
    92   flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0}; 
    93   fcntl (fd, F_SETLKW, &lock); 
    94   ///out << "locked!" << endl; 
    95   return fd; 
     102  cout << endl << "----------------------------------------------------------------------" << endl;  
    96103} 
    97  
    98 // unlock a file on linux 
    99 // [hs, 2 july 2010] 
    100 int unlock ( int fd ) 
    101 { 
    102   flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0}; 
    103   fcntl (fd, F_SETLKW, &lock1); 
    104   return 0; 
    105 } 
    106 #endif 
    107104 
    108105 
     
    113110       
    114111  // sanity check 
    115   if (argc != 2) { 
     112  if (argc != 2 && argc != 3) { 
    116113    print_usage (argv[0]); 
    117114    exit (0); 
    118115  } 
    119116   
     117  char *dbname; 
     118  int sort = 0; 
     119 
     120  if (argc == 3) { 
     121    if (strcmp (argv[1], "-sort") == 0) { 
     122      sort = 1; 
     123      dbname = argv[2]; 
     124    } else { 
     125      cerr << argv[1] << " is not a valid option." << endl << endl; 
     126      print_usage (argv[0]); 
     127      exit (0); 
     128    } 
     129  } else dbname = argv[1]; 
     130 
     131  vector<text_t> key_list; 
     132 
    120133  // open the database 
    121134#ifdef _MSC_VER 
    122   dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0); 
     135  dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL, 0); 
    123136#else 
    124   dbf = gdbm_open  (argv[1], block_size, GDBM_READER, 00664, NULL); 
     137  dbf = gdbm_open  (dbname, block_size, GDBM_READER, 00664, NULL); 
    125138#endif 
    126139  if (dbf == NULL) { 
    127     cerr << argv[0] << ": couldn't open " << argv[1] << endl; 
     140    cerr << argv[0] << ": couldn't open " << dbname << endl; 
    128141    exit (0); 
    129142  } 
     
    131144  key = gdbm_firstkey (dbf); 
    132145  while (key.dptr != NULL) { 
    133     cout << "["; 
    134     for (i = 0; i < key.dsize; ++i) 
    135       cout << key.dptr[i]; 
    136     cout << "]" << endl; 
     146 
    137147    value = gdbm_fetch (dbf, key); 
    138     for (i = 0; i < value.dsize; ++i) 
    139       cout << value.dptr[i]; 
    140     cout << endl << "----------------------------------------------------------------------" << endl; 
     148     
     149    if(sort) { // store in vector, will sort and then print later 
     150      text_t key_str; 
     151      for (i = 0; i < key.dsize; ++i) { 
     152    key_str.push_back ((unsigned char)key.dptr[i]); 
     153      } 
     154      key_list.push_back(key_str); // STL makes a copy on push_back 
     155 
     156    } else { 
     157      print_entry(key, value); 
     158    } 
     159 
    141160    free(value.dptr); 
    142161 
     
    150169    key = nextkey; 
    151170  } 
     171 
     172  if(sort) {     
     173    std::sort(key_list.begin(), key_list.end()); 
     174     
     175    vector<text_t>::const_iterator this_key = key_list.begin(); 
     176    vector<text_t>::const_iterator end_key = key_list.end(); 
     177    while (this_key != end_key) { 
     178      const text_t& key_str = *this_key; 
     179      char* key_cstr = key_str.getcstr(); 
     180 
     181      key.dsize = strlen(key_cstr); 
     182      key.dptr = key_cstr; 
     183 
     184      value = gdbm_fetch (dbf, key); 
     185 
     186      print_entry(key,value); // print in sorted order now 
     187 
     188      free(value.dptr);       
     189     
     190      this_key++; 
     191    } 
     192  } 
     193   
    152194   
    153195  gdbm_close (dbf);