Index: main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/Makefile.in
===================================================================
--- main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/Makefile.in	(revision 27597)
+++ main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/Makefile.in	(revision 27600)
@@ -58,6 +58,10 @@
 	db2txt.o
 
+GSDL_LIBS = \
+	$(COMMON_DIR)/src/lib/gsdllib.a 
+
 EXECUTABLE_OBJECTS = \
-	$(OBJECTS)
+	$(OBJECTS) \
+	$(GSDL_LIBS)
 
 EXECUTABLE = db2txt
Index: main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp
===================================================================
--- main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp	(revision 27597)
+++ main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp	(revision 27600)
@@ -39,5 +39,24 @@
 
 #include "gsdlconf.h"
+#include "text_t.h"
 #include <stdlib.h>
+#include <cstring>
+
+// for sorting
+#if defined(GSDL_USE_OBJECTSPACE)
+#  include <ospace\std\vector>
+#  include <ospace\std\algorithm>
+#elif defined(GSDL_USE_STL_H)
+#  include <vector.h>
+#  if defined(GSDL_USE_ALGO_H)
+#    include <algo.h>
+#  else
+#    include <algorithm.h>
+#  endif
+#else
+#  include <vector>
+#  include <algorithm>
+#endif
+
 
 #if defined(GSDL_USE_OBJECTSPACE)
@@ -58,51 +77,29 @@
 #endif
 
+// John Thompson's lock() and unlock() for windows
+// parked in separate file to help readability of main code
+#include "winlock.cpp"
+
 void print_usage (char *program_name) {
-  cerr << "usage: " << program_name << " database-name" << endl << endl;
+  cerr << "usage: " << program_name << " [options] database-name" << endl << endl;
+  cerr << "options:" << endl;
+  cerr << " -sort        sort the keys to the database before output" << endl << endl;
 }
 
+void print_entry(datum& key, datum& value)
+{
+  int i;
+  cout << "[";
+  for (i = 0; i < key.dsize; ++i) {
+    cout << key.dptr[i];
+  }
+  cout << "]" << endl;
 
-#ifdef _MSC32_
-// Windows implementation
-HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
 
-#else
-// assume Unix
-// lock a file on linux
-// [hs, 2 july 2010]
-// - modified to create a locl file local to the collection [jmt12]
-int lock ()
-{
-  string file_path ("");
-  char *collect_dir = getenv ("GSDLCOLLECTDIR");
-  if (collect_dir != NULL)
-  {
-    file_path += collect_dir;
+  for (i = 0; i < value.dsize; ++i) {
+    cout << value.dptr[i];
   }
-  file_path += "/tmp";
-  if ( access( file_path.c_str(), 00 ) != 0 )
-  {
-    mkdir(file_path.c_str(), 00777);
-  }
-  file_path += "/gdb.lock";
-  ///out << "txt2dbl::lock(" << file_path << ") => ";
-  int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
-  close (fd2);
-  int fd = open (file_path.c_str(), O_RDWR);
-  flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
-  fcntl (fd, F_SETLKW, &lock);
-  ///out << "locked!" << endl;
-  return fd;
+  cout << endl << "----------------------------------------------------------------------" << endl; 
 }
-
-// unlock a file on linux
-// [hs, 2 july 2010]
-int unlock ( int fd )
-{
-  flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
-  fcntl (fd, F_SETLKW, &lock1);
-  return 0;
-}
-#endif
 
 
@@ -113,17 +110,33 @@
       
   // sanity check
-  if (argc != 2) {
+  if (argc != 2 && argc != 3) {
     print_usage (argv[0]);
     exit (0);
   }
   
+  char *dbname;
+  int sort = 0;
+
+  if (argc == 3) {
+    if (strcmp (argv[1], "-sort") == 0) {
+      sort = 1;
+      dbname = argv[2];
+    } else {
+      cerr << argv[1] << " is not a valid option." << endl << endl;
+      print_usage (argv[0]);
+      exit (0);
+    }
+  } else dbname = argv[1];
+
+  vector<text_t> key_list;
+
   // open the database
 #ifdef _MSC_VER
-  dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0);
+  dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL, 0);
 #else
-  dbf = gdbm_open  (argv[1], block_size, GDBM_READER, 00664, NULL);
+  dbf = gdbm_open  (dbname, block_size, GDBM_READER, 00664, NULL);
 #endif
   if (dbf == NULL) {
-    cerr << argv[0] << ": couldn't open " << argv[1] << endl;
+    cerr << argv[0] << ": couldn't open " << dbname << endl;
     exit (0);
   }
@@ -131,12 +144,18 @@
   key = gdbm_firstkey (dbf);
   while (key.dptr != NULL) {
-    cout << "[";
-    for (i = 0; i < key.dsize; ++i)
-      cout << key.dptr[i];
-    cout << "]" << endl;
+
     value = gdbm_fetch (dbf, key);
-    for (i = 0; i < value.dsize; ++i)
-      cout << value.dptr[i];
-    cout << endl << "----------------------------------------------------------------------" << endl;
+    
+    if(sort) { // store in vector, will sort and then print later
+      text_t key_str;
+      for (i = 0; i < key.dsize; ++i) {
+	key_str.push_back ((unsigned char)key.dptr[i]);
+      }
+      key_list.push_back(key_str); // STL makes a copy on push_back
+
+    } else {
+      print_entry(key, value);
+    }
+
     free(value.dptr);
 
@@ -150,4 +169,27 @@
     key = nextkey;
   }
+
+  if(sort) {    
+    std::sort(key_list.begin(), key_list.end());
+    
+    vector<text_t>::const_iterator this_key = key_list.begin();
+    vector<text_t>::const_iterator end_key = key_list.end();
+    while (this_key != end_key) {
+      const text_t& key_str = *this_key;
+      char* key_cstr = key_str.getcstr();
+
+      key.dsize = strlen(key_cstr);
+      key.dptr = key_cstr;
+
+      value = gdbm_fetch (dbf, key);
+
+      print_entry(key,value); // print in sorted order now
+
+      free(value.dptr);      
+    
+      this_key++;
+    }
+  }
+  
   
   gdbm_close (dbf);
Index: main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/winlock.cpp
===================================================================
--- main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/winlock.cpp	(revision 27600)
+++ main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/winlock.cpp	(revision 27600)
@@ -0,0 +1,43 @@
+
+#ifdef _MSC32_
+// Windows implementation
+HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
+
+#else
+// assume Unix
+// lock a file on linux
+// [hs, 2 july 2010]
+// - modified to create a locl file local to the collection [jmt12]
+int lock ()
+{
+  string file_path ("");
+  char *collect_dir = getenv ("GSDLCOLLECTDIR");
+  if (collect_dir != NULL)
+  {
+    file_path += collect_dir;
+  }
+  file_path += "/tmp";
+  if ( access( file_path.c_str(), 00 ) != 0 )
+  {
+    mkdir(file_path.c_str(), 00777);
+  }
+  file_path += "/gdb.lock";
+  ///out << "txt2dbl::lock(" << file_path << ") => ";
+  int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
+  close (fd2);
+  int fd = open (file_path.c_str(), O_RDWR);
+  flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
+  fcntl (fd, F_SETLKW, &lock);
+  ///out << "locked!" << endl;
+  return fd;
+}
+
+// unlock a file on linux
+// [hs, 2 july 2010]
+int unlock ( int fd )
+{
+  flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
+  fcntl (fd, F_SETLKW, &lock1);
+  return 0;
+}
+#endif
