OpenIndiana and Solaris port
[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 #ifndef C_API
52 #include <sstream>
53 using namespace std;
54 #endif
55 #include "api/info_base.h"
56 #include "compression/abs_agent.h"
57
58 info_base::info_base(object_dict& obj_dict, 
59                      char** set_name_list, char** list_name_list,
60                      const char* base_dir, const char* base_nm,
61                      const char* base_ds, const char* base_uid, 
62                      const char* base_locale,
63                      const mm_version& v
64                     )
65 : base(&obj_dict, 
66        set_name_list, list_name_list, 
67        base_dir, base_nm, base_ds, base_uid
68       ), f_v(v)
69 {
70 /*
71 debug(cerr, base_dir);
72 debug(cerr, base_nm);
73 debug(cerr, base_ds);
74 */
75    int len = MIN(strlen(base_locale), PATHSIZ - 1);
76    if (base_locale)
77      *((char *) memcpy (info_base_locale, base_locale, len) + len) = '\0';
78    else
79      *info_base_locale = 0;
80
81    char* nm ;
82    int i;
83  
84 //////////////////////
85 // sets
86 //////////////////////
87    info_base_set_ptrs = new cset_handlerPtr[num_cset_ptrs];
88
89    for ( i=0; i<num_cset_ptrs; i++ ) {
90
91       nm = form("%s.%s", base_nm, info_base_set_names[i]);
92
93       mtry {
94          info_base_set_ptrs[i] = (cset_handler*)
95               (f_obj_dict -> get_handler(nm));
96       }
97       mcatch_any()
98           {
99          info_base_set_ptrs[i] = 0;
100           }
101       end_try;
102    }
103
104 //////////////////////
105 // lists
106 //////////////////////
107    info_base_list_ptrs = new dl_list_handlerPtr[num_list_ptrs];
108
109    for ( i=0; i<num_list_ptrs; i++ ) {
110
111       nm = form("%s.%s", base_nm, info_base_list_names[i]);
112
113       mtry {
114          info_base_list_ptrs[i] = (dl_list_handler*)
115              obj_dict.get_handler(nm);
116       }
117       mcatch_any()
118           {
119          info_base_list_ptrs[i] = 0;
120           }
121       end_try;
122    }
123
124    MESSAGE(cerr, form("info base %s in %s available.", base_name, base_dir));
125 }
126
127 info_base::~info_base()
128 {
129    delete info_base_list_ptrs;
130    delete info_base_set_ptrs;
131 }
132
133 int info_base::get_set_pos(const char* set_nm)
134 {
135    for ( int i=0; i<num_cset_ptrs; i++ ) {
136       if ( strcmp(set_nm,  info_base_set_names[i]) == 0 ) 
137          return i;
138    }
139
140    return -1;
141 }
142
143 cset_handlerPtr info_base::get_set(const char* set_nm)
144 {
145 //MESSAGE(cerr, "in info_base::get_set()");
146    return get_set(get_set_pos(set_nm));
147 }
148
149 cset_handlerPtr info_base::get_set(int i)
150 {
151    if ( !INRANGE(i, 0, num_cset_ptrs-1) )
152       throw (boundaryException(0, num_cset_ptrs-1, i));
153
154    return info_base_set_ptrs[i];
155 }
156
157
158 ///////////////////
159 // funcs about list
160 ///////////////////
161 int info_base::get_list_pos(const char* list_nm)
162 {
163    for ( int i=0; i<num_list_ptrs; i++ ) {
164       if ( strcmp(list_nm,  info_base_list_names[i]) == 0 ) 
165          return i;
166    }
167
168    return -1;
169 }
170
171 dl_list_handlerPtr info_base::get_list(const char* list_nm)
172 {
173    return ( get_list(get_list_pos(list_nm)) );
174 }
175
176 dl_list_handlerPtr info_base::get_list(int i)
177 {
178    if ( !INRANGE(i, 0, num_list_ptrs-1) ) {
179       throw(boundaryException(0, num_list_ptrs-1, i));
180    }
181
182    return info_base_list_ptrs[i];
183 }
184
185 int info_base::num_of_docs()
186 {
187 // the implementation is temp. It should take
188 // a hint as where to obtain the information.
189 // like from a set named so and so.
190
191    cset_handler* x = get_set("doc");
192    if (x) {
193       return (*x) -> count();
194    } else {
195       return 0;
196    }
197 }
198
199 ////////////////////////////////////////////////////////////////////
200 //
201 ////////////////////////////////////////////////////////////////////
202
203 Iterator::Iterator( handler* x, c_code_t y) :
204    collection_hd(x), instance_c_code(y), ind(0)
205 {
206 }
207
208 Iterator::~Iterator()
209 {
210 }
211
212 Iterator::operator void*()
213
214    return ( ind == 0 ) ? 0 : (void*)1; 
215 }
216
217
218
219 Iterator* info_base::first(char* col_nm, c_code_t code)
220 {
221    handler* x = get_set(col_nm);
222
223    if ( x == 0 ) {
224       x = get_list(col_nm);
225    }
226
227    if ( x == 0 ) {
228       throw(stringException("unknown collection name"));
229    }
230
231    page_storage *s = (page_storage*)(x -> its_store());
232
233    if ( s == 0 ) {
234       throw(stringException("collection has no store"));
235    }
236
237    if ( s -> my_oid().ccode() != PAGE_STORAGE_CODE ) {
238       throw(stringException("non page store no supported"));
239    }
240
241    Iterator* it = new Iterator(x, code);
242
243    it -> ind = s -> first_loc();
244
245    if ( managers::template_mgr -> peek_slot(s, it->ind) != code ) {
246       this -> next(*it);
247    }
248
249    return it;
250 }
251
252 oid_t info_base::get_oid(const Iterator& it)
253 {
254    page_storage *s = (page_storage*)( it.collection_hd -> its_store() );
255
256    root *r = 0;
257    managers::template_mgr -> init_obj(s, it.ind, r);
258    if (r == 0)
259        throw(stringException("null root pointer"));
260
261    return r -> my_oid();
262 }
263
264 void info_base::next(Iterator& it)
265 {
266    page_storage *s = (page_storage*)( it.collection_hd -> its_store());
267
268    while ( s -> seek_loc( it.ind, positive, spointer_t::IS_OBJECT ) == true ) {
269      if ( managers::template_mgr -> peek_slot(s, it.ind) == it.instance_c_code ) {
270         return;
271      }
272    }
273
274    it.ind = 0;
275 }
276
277 int stdin_sgml_data_getchar(unsigned char* buf, int max_sz)
278 {
279    static int remain_chars = 0;
280    static int chars_to_read;
281    static char loc_buf[LBUFSIZ];
282
283    if ( remain_chars == 0 ) {
284       if ( fgets(loc_buf, LBUFSIZ, stdin) == NULL )
285          return 0;
286
287       if ( fgets(loc_buf, LBUFSIZ, stdin) == NULL )
288          throw(stringException("no locator line"));
289
290       if ( fscanf(stdin, "%d", &remain_chars) != 1 )
291          throw(stringException("sgml_data_getchar(): fscanf() failed"));
292
293       if ( getc(stdin) != '\t' ) {
294          debug(cerr, remain_chars);
295          throw(stringException("'\\t' expected"));
296       }
297
298    }
299
300    chars_to_read = MIN(max_sz, remain_chars);
301
302    if ( (int)fread((char*)buf, 1, chars_to_read, stdin) != chars_to_read ) {
303       throw(stringException("sgml_data_getchar(): fread() failed"));
304    }
305
306    remain_chars -= chars_to_read;
307
308    if ( remain_chars == 0 ) {
309       if ( getc(stdin) != '\n' )
310          throw(stringException("'\\n' expected"));
311    }
312
313 /*
314 for ( int i=0; i<chars_to_read; i++ ) {
315    cerr << buf[i];
316 }
317 MESSAGE(cerr, "\n");
318 debug(cerr, max_sz);
319 debug(cerr, chars_to_read);
320 */
321
322    return chars_to_read;
323 }
324
325 int stdin_ps_data_getchar(unsigned char* buf, int max_sz)
326 {
327    return fread((char*)buf, 1, max_sz, stdin);
328 }
329
330 extern int sgmllex();
331 extern int pslex();
332
333 void info_base::build_dict(char* agent_name)
334 {
335    compress_agent_handler* x = (compress_agent_handler*)
336                   (f_obj_dict -> get_handler(agent_name));
337
338    if ( x == 0 )
339       throw(stringException("info_base::build_dict(): unknown compress agent"));
340
341    if ( strstr(agent_name, ".sgml.") != NULL ) {
342       (*x) -> build_dict(sgmllex, stdin_sgml_data_getchar);
343    } else
344    if ( strstr(agent_name, ".ps.") != NULL ) {
345       (*x) -> build_dict(pslex, stdin_ps_data_getchar);
346    } else
347       throw(stringException("info_base::build_dict(): unknown compress target"));
348 }
349