1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * db2txt.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 | #ifdef __WIN32__
|
---|
27 | #include "autoconf.h"
|
---|
28 | #include "systems.h"
|
---|
29 | #include "gdbmconst.h"
|
---|
30 | #include "gdbm.h"
|
---|
31 |
|
---|
32 | #else
|
---|
33 | #include <gdbm.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include "gsdlconf.h"
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <fcntl.h>
|
---|
39 |
|
---|
40 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
41 | #include <ospace\std\iostream>
|
---|
42 | #elif defined(GSDL_USE_IOS_H)
|
---|
43 | #include <iostream.h>
|
---|
44 | #else
|
---|
45 | #include <iostream>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | // use the standard namespace
|
---|
49 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
50 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
51 | using namespace ospace::std;
|
---|
52 | #else
|
---|
53 | using namespace std;
|
---|
54 | #endif
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | void print_usage (char *program_name) {
|
---|
58 | cerr << "usage: " << program_name << " database-name" << endl << endl;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // lock a file on linux
|
---|
62 | // [hs, 2 july 2010]
|
---|
63 | int lock ( char *filename )
|
---|
64 | {
|
---|
65 | int fd2 = open (filename, O_CREAT|O_RDWR, 00644);
|
---|
66 | close (fd2);
|
---|
67 | int fd = open (filename, O_RDWR);
|
---|
68 | flock lock = {F_RDLCK, SEEK_SET, 0, 0, 0};
|
---|
69 | fcntl (fd, F_SETLKW, &lock);
|
---|
70 | return fd;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // unlock a file on linux
|
---|
74 | // [hs, 2 july 2010]
|
---|
75 | int unlock ( int fd )
|
---|
76 | {
|
---|
77 | flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
|
---|
78 | fcntl (fd, F_SETLKW, &lock1);
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int main (int argc, char *argv[]) {
|
---|
83 | int block_size = 0, i;
|
---|
84 | GDBM_FILE dbf;
|
---|
85 | datum key, value, nextkey;
|
---|
86 |
|
---|
87 | // sanity check
|
---|
88 | if (argc != 2) {
|
---|
89 | print_usage (argv[0]);
|
---|
90 | exit (0);
|
---|
91 | }
|
---|
92 |
|
---|
93 | // open the database
|
---|
94 | // - acquire a lock over the database so we can read from it.
|
---|
95 | int thelock = lock ("gdb.lock");
|
---|
96 | #ifdef __WIN32__
|
---|
97 | dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0);
|
---|
98 | #else
|
---|
99 | dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL);
|
---|
100 | #endif
|
---|
101 | if (dbf == NULL) {
|
---|
102 | cerr << argv[0] << ": couldn't open " << argv[1] << endl;
|
---|
103 | exit (0);
|
---|
104 | }
|
---|
105 |
|
---|
106 | key = gdbm_firstkey (dbf);
|
---|
107 | while (key.dptr != NULL) {
|
---|
108 | cout << "[";
|
---|
109 | for (i = 0; i < key.dsize; ++i)
|
---|
110 | cout << key.dptr[i];
|
---|
111 | cout << "]" << endl;
|
---|
112 | value = gdbm_fetch (dbf, key);
|
---|
113 | for (i = 0; i < value.dsize; ++i)
|
---|
114 | cout << value.dptr[i];
|
---|
115 | cout << endl << "----------------------------------------------------------------------" << endl;
|
---|
116 | free(value.dptr);
|
---|
117 |
|
---|
118 | /* get next key */
|
---|
119 | nextkey = gdbm_nextkey (dbf, key);
|
---|
120 |
|
---|
121 | /* free old key's dptr, otherwise causes memory leak */
|
---|
122 | free(key.dptr);
|
---|
123 |
|
---|
124 | /* can now safely copy content of nextkey into key */
|
---|
125 | key = nextkey;
|
---|
126 | }
|
---|
127 |
|
---|
128 | gdbm_close (dbf);
|
---|
129 |
|
---|
130 | // Unlock now that we've written the entire database
|
---|
131 | unlock (thelock);
|
---|
132 |
|
---|
133 | return 0;
|
---|
134 | }
|
---|