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 _MSC_VER
|
---|
27 | #include "autoconf.h"
|
---|
28 | #include "systems.h"
|
---|
29 | #include "gdbmconst.h"
|
---|
30 | #include "gdbm.h"
|
---|
31 |
|
---|
32 | #else
|
---|
33 |
|
---|
34 | /* header file for mkdir */
|
---|
35 | #include <sys/stat.h>
|
---|
36 |
|
---|
37 | #include <gdbm.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include "gsdlconf.h"
|
---|
41 | #include <stdlib.h>
|
---|
42 |
|
---|
43 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
44 | #include <ospace\std\iostream>
|
---|
45 | #elif defined(GSDL_USE_IOS_H)
|
---|
46 | #include <iostream.h>
|
---|
47 | #else
|
---|
48 | #include <iostream>
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | // use the standard namespace
|
---|
52 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
53 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
54 | using namespace ospace::std;
|
---|
55 | #else
|
---|
56 | using namespace std;
|
---|
57 | #endif
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | void print_usage (char *program_name) {
|
---|
61 | cerr << "usage: " << program_name << " database-name" << endl << endl;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | #ifdef _MSC32_
|
---|
66 | // Windows implementation
|
---|
67 | HANDLE hFile = CreateFile(_T("c:\\file.txt"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
|
---|
68 |
|
---|
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;
|
---|
81 | }
|
---|
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;
|
---|
96 | }
|
---|
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
|
---|
107 |
|
---|
108 |
|
---|
109 | int main (int argc, char *argv[]) {
|
---|
110 | int block_size = 0, i;
|
---|
111 | GDBM_FILE dbf;
|
---|
112 | datum key, value, nextkey;
|
---|
113 |
|
---|
114 | // sanity check
|
---|
115 | if (argc != 2) {
|
---|
116 | print_usage (argv[0]);
|
---|
117 | exit (0);
|
---|
118 | }
|
---|
119 |
|
---|
120 | // open the database
|
---|
121 | #ifdef _MSC_VER
|
---|
122 | dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0);
|
---|
123 | #else
|
---|
124 | dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL);
|
---|
125 | #endif
|
---|
126 | if (dbf == NULL) {
|
---|
127 | cerr << argv[0] << ": couldn't open " << argv[1] << endl;
|
---|
128 | exit (0);
|
---|
129 | }
|
---|
130 |
|
---|
131 | key = gdbm_firstkey (dbf);
|
---|
132 | while (key.dptr != NULL) {
|
---|
133 | cout << "[";
|
---|
134 | for (i = 0; i < key.dsize; ++i)
|
---|
135 | cout << key.dptr[i];
|
---|
136 | cout << "]" << endl;
|
---|
137 | value = gdbm_fetch (dbf, key);
|
---|
138 | for (i = 0; i < value.dsize; ++i)
|
---|
139 | cout << value.dptr[i];
|
---|
140 | cout << endl << "----------------------------------------------------------------------" << endl;
|
---|
141 | free(value.dptr);
|
---|
142 |
|
---|
143 | /* get next key */
|
---|
144 | nextkey = gdbm_nextkey (dbf, key);
|
---|
145 |
|
---|
146 | /* free old key's dptr, otherwise causes memory leak */
|
---|
147 | free(key.dptr);
|
---|
148 |
|
---|
149 | /* can now safely copy content of nextkey into key */
|
---|
150 | key = nextkey;
|
---|
151 | }
|
---|
152 |
|
---|
153 | gdbm_close (dbf);
|
---|
154 | return 0;
|
---|
155 | }
|
---|