Changeset 27600 for main/trunk/greenstone2
- Timestamp:
- 2013-06-12T17:11:12+12:00 (11 years ago)
- Location:
- main/trunk/greenstone2/common-src/src/gdbmedit/db2txt
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/Makefile.in
r26821 r27600 58 58 db2txt.o 59 59 60 GSDL_LIBS = \ 61 $(COMMON_DIR)/src/lib/gsdllib.a 62 60 63 EXECUTABLE_OBJECTS = \ 61 $(OBJECTS) 64 $(OBJECTS) \ 65 $(GSDL_LIBS) 62 66 63 67 EXECUTABLE = db2txt -
main/trunk/greenstone2/common-src/src/gdbmedit/db2txt/db2txt.cpp
r27597 r27600 39 39 40 40 #include "gsdlconf.h" 41 #include "text_t.h" 41 42 #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 42 61 43 62 #if defined(GSDL_USE_OBJECTSPACE) … … 58 77 #endif 59 78 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 60 83 void 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; 62 87 } 63 88 89 void 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; 64 97 65 #ifdef _MSC32_66 // Windows implementation67 HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);68 98 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]; 81 101 } 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; 96 103 } 97 98 // unlock a file on linux99 // [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 #endif107 104 108 105 … … 113 110 114 111 // sanity check 115 if (argc != 2 ) {112 if (argc != 2 && argc != 3) { 116 113 print_usage (argv[0]); 117 114 exit (0); 118 115 } 119 116 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 120 133 // open the database 121 134 #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); 123 136 #else 124 dbf = gdbm_open ( argv[1], block_size, GDBM_READER, 00664, NULL);137 dbf = gdbm_open (dbname, block_size, GDBM_READER, 00664, NULL); 125 138 #endif 126 139 if (dbf == NULL) { 127 cerr << argv[0] << ": couldn't open " << argv[1]<< endl;140 cerr << argv[0] << ": couldn't open " << dbname << endl; 128 141 exit (0); 129 142 } … … 131 144 key = gdbm_firstkey (dbf); 132 145 while (key.dptr != NULL) { 133 cout << "["; 134 for (i = 0; i < key.dsize; ++i) 135 cout << key.dptr[i]; 136 cout << "]" << endl; 146 137 147 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 141 160 free(value.dptr); 142 161 … … 150 169 key = nextkey; 151 170 } 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 152 194 153 195 gdbm_close (dbf);
Note:
See TracChangeset
for help on using the changeset viewer.