Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / api / info_base.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 /*
24  * $XConsortium: info_base.C /main/5 1996/09/04 01:37:05 cde-hal $
25  *
26  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "api/info_base.h"
52 #include "compression/abs_agent.h"
53
54 info_base::info_base(object_dict& obj_dict, 
55                      char** set_name_list, char** list_name_list,
56                      const char* base_dir, const char* base_nm,
57                      const char* base_ds, const char* base_uid, 
58                      const char* base_locale,
59                      const mm_version& v
60                     )
61 : base(&obj_dict, 
62        set_name_list, list_name_list, 
63        base_dir, base_nm, base_ds, base_uid
64       ), f_v(v)
65 {
66 /*
67 debug(cerr, base_dir);
68 debug(cerr, base_nm);
69 debug(cerr, base_ds);
70 */
71    if (base_locale)
72      strcpy(info_base_locale, base_locale);
73    else
74      *info_base_locale = 0;
75
76    char* nm ;
77  
78 //////////////////////
79 // sets
80 //////////////////////
81    info_base_set_ptrs = new cset_handlerPtr[num_cset_ptrs];
82
83    for ( int i=0; i<num_cset_ptrs; i++ ) {
84
85       nm = form("%s.%s", base_nm, info_base_set_names[i]);
86
87       try {
88          info_base_set_ptrs[i] = (cset_handler*)
89               (f_obj_dict -> get_handler(nm));
90       }
91       catch_any()
92           {
93          info_base_set_ptrs[i] = 0;
94           }
95       end_try;
96    }
97
98 //////////////////////
99 // lists
100 //////////////////////
101    info_base_list_ptrs = new dl_list_handlerPtr[num_list_ptrs];
102
103    for ( i=0; i<num_list_ptrs; i++ ) {
104
105       nm = form("%s.%s", base_nm, info_base_list_names[i]);
106
107       try {
108          info_base_list_ptrs[i] = (dl_list_handler*)
109              obj_dict.get_handler(nm);
110       }
111       catch_any()
112           {
113          info_base_list_ptrs[i] = 0;
114           }
115       end_try;
116    }
117
118    MESSAGE(cerr, form("info base %s in %s available.", base_name, base_dir));
119 }
120
121 info_base::~info_base()
122 {
123    delete info_base_list_ptrs;
124    delete info_base_set_ptrs;
125 }
126
127 int info_base::get_set_pos(const char* set_nm)
128 {
129    char* nm = 0;
130    for ( int i=0; i<num_cset_ptrs; i++ ) {
131       if ( strcmp(set_nm,  info_base_set_names[i]) == 0 ) 
132          return i;
133    }
134
135    return -1;
136 }
137
138 cset_handlerPtr info_base::get_set(const char* set_nm)
139 {
140 //MESSAGE(cerr, "in info_base::get_set()");
141    return get_set(get_set_pos(set_nm));
142 }
143
144 cset_handlerPtr info_base::get_set(int i)
145 {
146    if ( !INRANGE(i, 0, num_cset_ptrs-1) )
147       throw (boundaryException(0, num_cset_ptrs-1, i));
148
149    return info_base_set_ptrs[i];
150 }
151
152
153 ///////////////////
154 // funcs about list
155 ///////////////////
156 int info_base::get_list_pos(const char* list_nm)
157 {
158    for ( int i=0; i<num_list_ptrs; i++ ) {
159       if ( strcmp(list_nm,  info_base_list_names[i]) == 0 ) 
160          return i;
161    }
162
163    return -1;
164 }
165
166 dl_list_handlerPtr info_base::get_list(const char* list_nm)
167 {
168    return ( get_list(get_list_pos(list_nm)) );
169 }
170
171 dl_list_handlerPtr info_base::get_list(int i)
172 {
173    if ( !INRANGE(i, 0, num_list_ptrs-1) ) {
174       throw(boundaryException(0, num_list_ptrs-1, i));
175    }
176
177    return info_base_list_ptrs[i];
178 }
179
180 int info_base::num_of_docs()
181 {
182 // the implementation is temp. It should take
183 // a hint as where to obtain the information.
184 // like from a set named so and so.
185
186    cset_handler* x = get_set("doc");
187    if (x) {
188       return (*x) -> count();
189    } else {
190       return 0;
191    }
192 }
193
194 ////////////////////////////////////////////////////////////////////
195 //
196 ////////////////////////////////////////////////////////////////////
197
198 iterator::iterator( handler* x, c_code_t y) :
199    collection_hd(x), instance_c_code(y), ind(0)
200 {
201 }
202
203 iterator::~iterator()
204 {
205 }
206
207 iterator::operator void*()
208
209    return ( ind == 0 ) ? 0 : (void*)1; 
210 }
211
212
213
214 iterator* info_base::first(char* col_nm, c_code_t code)
215 {
216    handler* x = get_set(col_nm);
217
218    if ( x == 0 ) {
219       handler* x = get_list(col_nm);
220    }
221
222    if ( x == 0 ) {
223       throw(stringException("unknown collection name"));
224    }
225
226    page_storage *s = (page_storage*)(x -> its_store());
227
228    if ( s == 0 ) {
229       throw(stringException("collection has no store"));
230    }
231
232    if ( s -> my_oid().ccode() != PAGE_STORAGE_CODE ) {
233       throw(stringException("non page store no supported"));
234    }
235
236    iterator* it = new iterator(x, code);
237
238    it -> ind = s -> first_loc();
239
240    if ( managers::template_mgr -> peek_slot(s, it->ind) != code ) {
241       this -> next(*it);
242    }
243
244    return it;
245 }
246
247 oid_t info_base::get_oid(const iterator& it)
248 {
249    page_storage *s = (page_storage*)( it.collection_hd -> its_store() );
250
251    root *r = 0;
252    managers::template_mgr -> init_obj(s, it.ind, r);
253    if (r == 0)
254        throw(stringException("null root pointer"));
255
256    return r -> my_oid();
257 }
258
259 void info_base::next(iterator& it)
260 {
261    page_storage *s = (page_storage*)( it.collection_hd -> its_store());
262
263    while ( s -> seek_loc( it.ind, positive, spointer_t::IS_OBJECT ) == true ) {
264      if ( managers::template_mgr -> peek_slot(s, it.ind) == it.instance_c_code ) {
265         return;
266      }
267    }
268
269    it.ind = 0;
270 }
271
272 int stdin_sgml_data_getchar(unsigned char* buf, int max_sz)
273 {
274    static int remain_chars = 0;
275    static int chars_to_read;
276    static char loc_buf[LBUFSIZ];
277
278    if ( remain_chars == 0 ) {
279       if ( fgets(loc_buf, LBUFSIZ, stdin) == NULL )
280          return 0;
281
282       if ( fgets(loc_buf, LBUFSIZ, stdin) == NULL )
283          throw(stringException("no locator line"));
284
285       if ( fscanf(stdin, "%d", &remain_chars) != 1 )
286          throw(stringException("sgml_data_getchar(): fscanf() failed"));
287
288       if ( getc(stdin) != '\t' ) {
289          debug(cerr, remain_chars);
290          throw(stringException("'\\t' expected"));
291       }
292
293    }
294
295    chars_to_read = MIN(max_sz, remain_chars);
296
297    if ( fread((char*)buf, 1, chars_to_read, stdin) != chars_to_read ) {
298       throw(stringException("sgml_data_getchar(): fread() failed"));
299    }
300
301    remain_chars -= chars_to_read;
302
303    if ( remain_chars == 0 ) {
304       if ( getc(stdin) != '\n' )
305          throw(stringException("'\\n' expected"));
306    }
307
308 /*
309 for ( int i=0; i<chars_to_read; i++ ) {
310    cerr << buf[i];
311 }
312 MESSAGE(cerr, "\n");
313 debug(cerr, max_sz);
314 debug(cerr, chars_to_read);
315 */
316
317    return chars_to_read;
318 }
319
320 int stdin_ps_data_getchar(unsigned char* buf, int max_sz)
321 {
322    return fread((char*)buf, 1, max_sz, stdin);
323 }
324
325 extern int sgmllex();
326 extern int pslex();
327
328 void info_base::build_dict(char* agent_name)
329 {
330    compress_agent_handler* x = (compress_agent_handler*)
331                   (f_obj_dict -> get_handler(agent_name));
332
333    if ( x == 0 )
334       throw(stringException("info_base::build_dict(): unknown compress agent"));
335
336    if ( strstr(agent_name, ".sgml.") != NULL ) {
337       (*x) -> build_dict(sgmllex, stdin_sgml_data_getchar);
338    } else
339    if ( strstr(agent_name, ".ps.") != NULL ) {
340       (*x) -> build_dict(pslex, stdin_ps_data_getchar);
341    } else
342       throw(stringException("info_base::build_dict(): unknown compress target"));
343 }
344