1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * txt2db.cpp --
|
---|
4 | * A component of the Greenstone digital library software
|
---|
5 | * from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
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 |
|
---|
27 | #ifdef __WIN32__
|
---|
28 | #include "autoconf.h"
|
---|
29 | #include "systems.h"
|
---|
30 | #include "gdbmconst.h"
|
---|
31 | #include "gdbm.h"
|
---|
32 |
|
---|
33 | #else
|
---|
34 | #include <gdbm.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include "gsdlconf.h"
|
---|
38 | #include "text_t.h"
|
---|
39 | #include <stdlib.h>
|
---|
40 |
|
---|
41 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
42 | # include <ospace\std\iostream>
|
---|
43 | #elif defined(GSDL_USE_IOS_H)
|
---|
44 | # include <iostream.h>
|
---|
45 | #else
|
---|
46 | # include <iostream>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | void print_usage (char *program_name) {
|
---|
51 | cerr << "usage: " << program_name << " database-name\n\n";
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | int main (int argc, char *argv[]) {
|
---|
56 | int block_size = 0;
|
---|
57 | GDBM_FILE dbf;
|
---|
58 | char c;
|
---|
59 | text_t key;
|
---|
60 | text_t value;
|
---|
61 | text_t tmp;
|
---|
62 | int num_dashes = 0;
|
---|
63 |
|
---|
64 | // sanity check
|
---|
65 | if (argc != 2) {
|
---|
66 | print_usage (argv[0]);
|
---|
67 | exit (0);
|
---|
68 | }
|
---|
69 |
|
---|
70 | // open the database
|
---|
71 | dbf = gdbm_open (argv[1], block_size, GDBM_NEWDB | GDBM_FAST, 00664, NULL);
|
---|
72 | if (dbf == NULL) {
|
---|
73 | cerr << "couldn't create " << argv[1] << "\n";
|
---|
74 | exit (0);
|
---|
75 | }
|
---|
76 |
|
---|
77 | cin.get(c);
|
---|
78 | while (!cin.eof()) {
|
---|
79 | num_dashes = 0;
|
---|
80 | key = "";
|
---|
81 | value = "";
|
---|
82 |
|
---|
83 | // look for [key]\n
|
---|
84 | while (!cin.eof() && c != '[') cin.get(c);
|
---|
85 | if (!cin.eof()) cin.get(c); // skip [
|
---|
86 | while (!cin.eof() && c != ']') {
|
---|
87 | key.push_back ((unsigned char)c);
|
---|
88 | cin.get(c);
|
---|
89 | }
|
---|
90 | if (!cin.eof()) cin.get(c); // skip ]
|
---|
91 | while (!cin.eof() && (c == '\n' || c == 'r')) cin.get(c);
|
---|
92 |
|
---|
93 | // look for 70 dashes
|
---|
94 | tmp = "";
|
---|
95 | while (!cin.eof() && (num_dashes < 70)) {
|
---|
96 | if (c == '\n' || c == '\r') {
|
---|
97 | tmp.push_back ((unsigned char)c);
|
---|
98 | num_dashes = 0;
|
---|
99 |
|
---|
100 | } else if (c == '-') {
|
---|
101 | tmp.push_back ((unsigned char)c);
|
---|
102 | num_dashes++;
|
---|
103 |
|
---|
104 | } else {
|
---|
105 | value += tmp;
|
---|
106 | value.push_back ((unsigned char)c);
|
---|
107 | tmp = "";
|
---|
108 | num_dashes = 0;
|
---|
109 | }
|
---|
110 | cin.get(c);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // if the key is not an empty string store this key-value pair
|
---|
114 | if (!key.empty()) {
|
---|
115 | // convert key to a datum datatype
|
---|
116 | datum key_data;
|
---|
117 | key_data.dptr = key.getcstr();
|
---|
118 | if (key_data.dptr == NULL) {
|
---|
119 | cerr << "NULL key_data.dptr\n";
|
---|
120 | exit (0);
|
---|
121 | }
|
---|
122 | key_data.dsize = strlen(key_data.dptr);
|
---|
123 |
|
---|
124 | // convert value to a datum datatype
|
---|
125 | datum value_data;
|
---|
126 | value_data.dptr = value.getcstr();
|
---|
127 | if (value_data.dptr == NULL) {
|
---|
128 | cerr << "NULL value_data.dptr\n";
|
---|
129 | exit (0);
|
---|
130 | }
|
---|
131 | value_data.dsize = strlen(value_data.dptr);
|
---|
132 |
|
---|
133 | // store the value
|
---|
134 | if (gdbm_store (dbf, key_data, value_data, GDBM_REPLACE) < 0) {
|
---|
135 | cerr << "gdbm_store returned an error\n";
|
---|
136 | exit (0);
|
---|
137 | }
|
---|
138 |
|
---|
139 | delete key_data.dptr;
|
---|
140 | delete value_data.dptr;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | gdbm_close (dbf);
|
---|
145 |
|
---|
146 | return 0;
|
---|
147 | }
|
---|