OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / mmdb.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: mmdb.C /main/7 1996/10/26 18:19:36 cde-hal $
24
25 #include "api/utility.h"
26 #include "utility/db_version.h"
27 #include "misc/unique_id.h"
28 #include "oliasdb/olias_funcs.h"
29 #include "oliasdb/collectionIterator.h"
30 #include "oliasdb/mmdb.h"
31 #include "mgrs/managers.h"
32 #include "api/base.h" // for UIDSIZ define
33
34 OLIAS_DB::OLIAS_DB() : infolib_array(20)
35 {
36 //fprintf(stderr, "OLIAS_DB::OLIAS_DB()");
37    managers::template_mgr -> insert_template(&dlp_template);
38    managers::template_mgr -> insert_template(&doc_template);
39    managers::template_mgr -> insert_template(&graphic_template);
40    managers::template_mgr -> insert_template(&locator_template); 
41    managers::template_mgr -> insert_template(&node_template); 
42    managers::template_mgr -> insert_template(&toc_template); 
43    managers::template_mgr -> insert_template(&mark_template); 
44    //managers::template_mgr -> insert_template(&pref_template); 
45    managers::template_mgr -> insert_template(&stylesheet_template); 
46
47 ///////////////////////////////
48 //
49 ///////////////////////////////
50
51    info_base_set_names = new charPtr[7];
52
53    info_base_set_names[0] = NODE_SET_NAME;
54    info_base_set_names[1] = TOC_SET_NAME;
55    info_base_set_names[2] = LOCATOR_SET_NAME;
56    info_base_set_names[3] = GRAPHIC_SET_NAME,
57    info_base_set_names[4] = DOC_SET_NAME;
58    info_base_set_names[5] = STYLESHEET_SET_NAME;
59    info_base_set_names[6] = 0;
60
61    info_base_list_names = new charPtr[2];
62    info_base_list_names[0] = DLP_LIST_NAME;
63    info_base_list_names[1] = 0;
64 }
65
66 OLIAS_DB::~OLIAS_DB()
67 {
68    info_lib* x = 0;
69
70    for ( int i=0; i<infolib_array.no_elmts(); i++ ) {
71       x = (info_lib*)(infolib_array)[i];
72       delete x;
73    }
74
75    delete info_base_set_names;
76    delete info_base_list_names;
77 }
78
79 int OLIAS_DB::validInfoLibPath(const char* path)
80 {
81   mtry
82    {
83      if ( exist_dir(path) == false )
84        return 0;
85       
86      if ( exist_file(MAP_FILE, path) == true )
87        return 1;
88       
89      if ( exist_file(MAP_FILE_8_3, path) == true )
90        return 1;
91    
92    }
93
94    mcatch (mmdbException &,e)
95    {
96       return 0;
97    }
98    end_try;
99
100    return 0;
101 }
102
103 info_lib* 
104 OLIAS_DB::openInfoLib(const char* infoLibPath, const char* selectedBaseName,
105                   const char* infoLibName) 
106 {
107    int i;
108    for ( i=0; i<infolib_array.no_elmts(); i++ ) {
109       if ( infolib_array[i] == 0 )
110         break;
111    }
112
113    info_lib* x = new info_lib(
114                        info_base_set_names, info_base_list_names,
115                        (char*)infoLibPath, (char*)selectedBaseName,
116                        (char*)infoLibName, i 
117                       );
118
119   
120    // see if infolib already exists in mmdb, if it does, just
121    // return a pointer to it.
122    int idx = 0;
123    info_lib* y;
124
125    y = getInfoLib(x->get_info_lib_uid(), idx);
126    if (y)
127    {
128      delete x;
129      return y;
130    }
131
132    infolib_array.insert(x, i);
133    infolib_array.reset_elmts(i+1);
134
135    return x;
136 }
137
138 void OLIAS_DB::closeInfoLib(const char* infoLibUid)
139 {
140    int i = 0;
141    info_lib* x = getInfoLib(infoLibUid, i);
142
143    if ( x ) {
144       infolib_array.insert(0, i);
145       delete x;
146    }
147 }
148
149 info_lib* OLIAS_DB::getInfoLib(int i)
150 {
151    if ( 0 <= i && i < infolib_array.no_elmts() )
152    {
153       return (info_lib*)infolib_array[i];
154    } else
155       return 0;
156 }
157
158 info_lib* OLIAS_DB::getInfoLib(const char* infoLibUid, int& i)
159 {
160    info_lib* x = 0;
161    for ( i=0; i<infolib_array.no_elmts(); i++ ) {
162       x = (info_lib*)(infolib_array)[i];
163       //if ( x && strcmp(x -> get_info_lib_name(), infoLibName) == 0 ) 
164       if ( x && strcmp(x -> get_info_lib_uid(), infoLibUid) == 0 ) 
165          return x;
166    }
167    return 0;
168 }
169
170 char* OLIAS_DB::getInfoLibUid(char* infoLibPath)
171 {
172    static char info_lib_uid[UIDSIZ];
173    char info_lib_name[PATHSIZ];
174
175    if ( exist_file(MAP_FILE_8_3, infoLibPath) == true ) {
176       fstream map_in(form("%s/%s", infoLibPath, MAP_FILE_8_3), ios::in);
177
178       if ( !map_in.getline(info_lib_name, PATHSIZ, '\t') ||
179            !map_in.getline(info_lib_uid, UIDSIZ, '\n') )
180         return 0;
181
182       return info_lib_uid;
183    }
184   return 0;
185 }
186
187       
188 Boolean
189 OLIAS_DB::real_destroy_info_base(const char* info_lib_path, const char* base_name)
190 {
191    if ( unlink(form("%s/%s/%s.%s", info_lib_path, base_name, base_name, DATA_FILE_SUFFIX)) != 0 )
192       return false;
193
194    if ( unlink(form("%s/%s/%s.%s", info_lib_path, base_name, base_name, INDEX_FILE_SUFFIX)) != 0 )
195       return false;
196
197    if ( unlink(form("%s/%s/%s.%s", info_lib_path, base_name, base_name, SCHEMA_FILE_SUFFIX)) != 0 )
198       return false;
199
200    if ( rmdir(form("%s/%s", info_lib_path, base_name)) != 0 )
201       return false;
202
203    return true;
204 }
205
206 Boolean OLIAS_DB::real_destroy(const char* infoLibPath, const char* infoLibName)
207 {
208 // remove all infobases in the dir
209
210    char base_name[PATHSIZ];
211    char base_desc[PATHSIZ];
212    char buf[PATHSIZ];
213
214    int major_mm_version = 0;
215    int minor_mm_version = 0;
216
217    snprintf(buf, sizeof(buf), "%s/%s", infoLibPath, MAP_FILE_8_3);
218    fstream in(buf, ios::in);
219
220    while ( in.getline(base_name, PATHSIZ, '\t') ) {
221    
222          in.getline(base_desc, PATHSIZ, '\t');
223    
224          in >> major_mm_version >> minor_mm_version;
225    
226          in.get();
227    
228          if ( real_destroy_info_base(infoLibPath, base_name) != true )
229             return false;
230    }
231    
232 // remove map file
233    if ( unlink(buf) != 0 )
234       return false;
235    else
236       return true;
237 }
238
239 Boolean OLIAS_DB::destroy(const char* infoLibPath, const char* infoLibName)
240 {
241    int i = 0;
242    if ( getInfoLib(infoLibName, i) ) {
243       if ( infolib_array[i] )
244          return false;
245    
246       return real_destroy(infoLibPath, infoLibName);
247    }
248
249    return real_destroy(infoLibPath, infoLibName);
250 }
251
252 int OLIAS_DB::major_code_version()
253 {
254    return MAJOR;
255 }
256
257 int OLIAS_DB::minor_code_version()
258 {
259    return MINOR;
260 }
261
262 mm_version OLIAS_DB::code_version()
263 {
264    return mm_version(MAJOR, MINOR);
265 }
266