1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * userdb.cpp -- functions to handle a language macro database
|
---|
4 | * Copyright (C) 1999 DigiLib Systems Limited, New Zealand
|
---|
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 | #include "gsdlconf.h"
|
---|
27 | #include "langdb.h"
|
---|
28 | #include "gsdltimes.h"
|
---|
29 | #include "fileutil.h"
|
---|
30 | #include <stdlib.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | ////////////////
|
---|
34 | // macroinfo_t
|
---|
35 | ////////////////
|
---|
36 |
|
---|
37 | void macroinfo_t::clear () {
|
---|
38 | macroname.clear();
|
---|
39 | macrotext.clear();
|
---|
40 | }
|
---|
41 |
|
---|
42 | macroinfo_t ¯oinfo_t::operator=(const macroinfo_t &x) {
|
---|
43 | macroname = x.macroname;
|
---|
44 | macrotext = x.macrotext;
|
---|
45 |
|
---|
46 | return *this;
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | // functions dealing with language macro databases
|
---|
51 |
|
---|
52 | // returns true on success (in which case macroinfo will contain
|
---|
53 | // the information for this macro)
|
---|
54 | bool get_macro_info (gdbmclass &langdb, const text_t ¯oname,
|
---|
55 | macroinfo_t ¯oinfo) {
|
---|
56 | macroinfo.clear();
|
---|
57 |
|
---|
58 | infodbclass info;
|
---|
59 | if (langdb.getinfo (macroname, info)) {
|
---|
60 | macroinfo.macroname = info["macroname"];
|
---|
61 | macroinfo.macrotext = info["macrotext"];
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool get_macro_info (const text_t &langdbfile, const text_t ¯oname,
|
---|
69 | macroinfo_t ¯oinfo) {
|
---|
70 | // THIS IS GOING TO READ FROM THE MACRO FILE FIRST TIME...
|
---|
71 |
|
---|
72 | gdbmclass langdb;
|
---|
73 | if (!langdb.opendatabase(langdbfile, GDBM_READER, 1000, true)) {
|
---|
74 | /* if (!langdbfile.empty() && !file_exists (langdbfile) &&
|
---|
75 | macroname == "admin") {*/
|
---|
76 | if (!langdbfile.empty()) {
|
---|
77 | // no database -- create a database with an initial account
|
---|
78 | macroinfo.clear();
|
---|
79 | macroinfo.macroname = "language";
|
---|
80 | macroinfo.macrotext = "language"; // this needs to be extracted from langdbfile
|
---|
81 | return set_macro_info (langdbfile, macroname, macroinfo);
|
---|
82 | }
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool success = get_macro_info (langdb, macroname, macroinfo);
|
---|
87 | langdb.closedatabase();
|
---|
88 |
|
---|
89 | return success;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // returns true on success
|
---|
93 | bool set_macro_info (gdbmclass &langdb, const text_t ¯oname,
|
---|
94 | const macroinfo_t ¯oinfo) {
|
---|
95 | infodbclass info;
|
---|
96 | info["macroname"] = macroinfo.macroname;
|
---|
97 | info["macrotext"] = macroinfo.macrotext;
|
---|
98 |
|
---|
99 | return langdb.setinfo (macroname, info);
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool set_macro_info (const text_t &langdbfile, const text_t ¯oname,
|
---|
103 | const macroinfo_t ¯oinfo) {
|
---|
104 | gdbmclass langdb;
|
---|
105 | if (!langdb.opendatabase(langdbfile, GDBM_WRCREAT, 1000, true)) return false;
|
---|
106 |
|
---|
107 | bool success = set_macro_info (langdb, macroname, macroinfo);
|
---|
108 | langdb.closedatabase();
|
---|
109 |
|
---|
110 | return success;
|
---|
111 | }
|
---|