OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / c_api_common.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: c_api_common.cc /main/5 1996/07/18 16:01:11 drk $ */
24
25 #include "dstr/void_ptr_array.h"
26 #include "storage/page_storage.h"
27 #include "api/info_lib.h"
28 #include <stdlib.h>
29 #include <limits.h>
30 #include "schema/store_desc.h"
31 #include "dynhash/data_t.h"
32 #include "index/index.h"
33 #include "object/compressed_pstring.h"
34 #include "object/pstring.h"
35
36 #include "oliasdb/DtMmdb.h"
37 #include "oliasdb/olias_funcs.h"
38 #include "oliasdb/c_api_common.h"
39 #include "oliasdb/graphic_hd.h"
40
41 #include "exception_init.h"
42
43 void_ptr_array* infolib_array = 0;
44 void_ptr_array* bookcase_array = 0;
45 OLIAS_DB *mmdb_ptr = 0;
46
47 DtMmdbHandle* DtMmdbGroundId;
48
49 void initialize_MMDB()
50 {
51    initialize_exception();
52
53    INIT_EXCEPTIONS();
54
55    cin_ptr  = new fstream(0);
56    cout_ptr = new fstream(1);
57    cerr_ptr = new fstream(2);
58
59    g_memory_pool_ptr = new memory_pool;
60
61    page_storage::f_global_pcache_ptr = new page_cache_global_part;
62
63    ostring::input_buf = new char[LBUFSIZ];
64
65    data_t::larson_convertor_ptr = new atoi_larson;
66    data_t::pearson_convertor_ptr = new atoi_pearson(SHRT_MAX, 256);
67
68    ground_ptr = new oid_t(c_code_t(0), i_code_t(0));
69
70    //root::io_info_buffer_ptr = new struct persis_t;
71
72    pstring::v_io_buf_ptr = new buffer(LBUFSIZ);
73    graphic_smart_ptr::local_graphic_buffer_ptr = new buffer(LBUFSIZ);
74
75    compressed_pstring::working_buffer_ptr = new buffer(LBUFSIZ);
76    compressed_pstring::v_cp_io_buf_ptr = new buffer(LBUFSIZ);
77
78    store_desc::v_unixf_pool_ptr = 
79         new lru(ACTIVE_UNIXF_SZ, INACTIVE_UNIXF_SZ, false);
80
81    c_index::v_static_key_ptr = new key_type(LBUFSIZ);
82
83    infolib_array = new void_ptr_array(10);
84    bookcase_array = new void_ptr_array(10);
85 }
86
87 void quit_MMDB()
88 {
89    delete c_index::v_static_key_ptr;
90
91    delete store_desc::v_unixf_pool_ptr;
92
93    delete compressed_pstring::working_buffer_ptr;
94    delete compressed_pstring::v_cp_io_buf_ptr;
95
96    delete pstring::v_io_buf_ptr;
97    delete graphic_smart_ptr::local_graphic_buffer_ptr;
98
99    //delete root::io_info_buffer_ptr;
100
101    delete ground_ptr;
102
103    delete data_t::larson_convertor_ptr;
104    delete data_t::pearson_convertor_ptr;
105
106    delete ostring::input_buf;
107
108    delete page_storage::f_global_pcache_ptr;
109
110    delete g_memory_pool_ptr;
111
112    delete cin_ptr  ;
113    delete cout_ptr ;
114    delete cerr_ptr ;
115
116    delete infolib_array;
117    delete bookcase_array;
118
119    quit_exception();
120
121 }
122
123 ///////////////////////////////////////////////////////////////
124
125
126 extern "C" {
127 void DtMmdbInit()
128 {
129    initialize_MMDB();
130 }
131
132 void DtMmdbQuit()
133 {
134    quit_MMDB();
135 }
136
137 }
138
139 void* operator new( size_t sz ) throw(std::bad_alloc)
140 {
141    void* p = (void*)malloc(sz);
142    //printf("a::operator new called(). sz= %d, allo=%x\n", sz, p);
143    return p;
144 }
145
146 void operator delete( void* p ) throw()
147 {
148    //printf("a::operator free called(). alloc= %x\n", p);
149    if ( p )
150       free((char*)p);
151 }
152
153 #ifdef DEBUG
154 extern "C" {
155 void* centerline_new(int n, char*, int)
156 {
157     return :: operator new(n);
158 }
159 }
160 #endif
161
162 /*
163 info_lib* getInfoLib(int infolib_descriptor)
164 {
165    if ( 0 <= infolib_descriptor && infolib_array &&
166         infolib_descriptor < infolib_array -> no_elmts()
167          )
168    {
169       return (info_lib*)(*infolib_array)[infolib_descriptor];
170    } else
171       return 0;
172 }
173 */
174
175 info_base* getBookCase(int bookcase_descriptor)
176 {
177    if ( 0 <= bookcase_descriptor && bookcase_array &&
178         bookcase_descriptor < bookcase_array -> no_elmts()
179       ) {
180       return (info_base*)(*bookcase_array)[bookcase_descriptor];
181    } else
182       return 0;
183 }
184
185 oid_t* getPrimiaryOid(DtMmdbInfoRequest* x)
186 {
187    if ( x == 0 ) return 0;
188
189    if ( x -> primary_oid == 0 ) return 0;
190
191    return (oid_t*)(x -> primary_oid -> oid_ptr);
192 }
193
194 oid_t* getSecondaryOid(DtMmdbInfoRequest* x)
195 {
196    if ( x == 0 ) return 0;
197
198    if ( x -> secondary_oid == 0 ) return 0;
199
200    return (oid_t*)(x -> secondary_oid -> oid_ptr);
201 }
202
203 int getSeqNum(DtMmdbInfoRequest* x)
204 {
205    if ( x == 0 ) return -1;
206    return x -> sequence_num;
207 }
208
209 const char* getLocator(DtMmdbInfoRequest* x)
210 {
211    if ( x == 0 ) return 0;
212
213    return x -> locator_ptr;
214 }
215
216 DtMmdbHandle* newDtMmdbHandle(const oid_t& x)
217 {
218    oid_t* y = new oid_t(x);
219    DtMmdbHandle *z = (DtMmdbHandle*)malloc(sizeof(DtMmdbHandle));
220    z -> oid_ptr = (void*)y;
221    return z;
222 }
223
224 DtMmdbGraphicInfo* newDtMmdbGraphicInfo()
225 {
226    DtMmdbGraphicInfo *z = 
227       (DtMmdbGraphicInfo*)malloc(sizeof(DtMmdbGraphicInfo));
228    return z;
229 }
230
231 void DtMmdbFreeHandle(DtMmdbHandle* x)
232 {
233    if ( x ) {
234 //    delete x -> oid_ptr;
235       free((void*)x);
236    }
237 }
238
239 void DtMmdbFreeHandleList(DtMmdbHandle** list)
240 {
241    if ( list ) {
242       int i = 0;
243       while ( list[i] ) {
244         DtMmdbFreeHandle(list[i]);
245         i++;
246       }
247       free((void*)list);
248    }
249 }
250
251 #ifdef REGRESSION_TEST
252 extern int auto_test(int argc, char** argv, OLIAS_DB& db);
253 #endif
254
255 extern "C" {
256 int auto_test_c_api(int argc, char** argv)
257 {
258    mtry
259    {
260 #ifdef REGRESSION_TEST
261       OLIAS_DB db;
262       return auto_test(argc, argv, db);
263 #else
264       return -1;
265 #endif
266    }
267
268    mcatch (mmdbException &,e)
269    {
270       cerr << "Exception msg: " << e << "\n";
271       return -1;
272    }
273    end_try;
274
275    return -1;
276 }
277
278
279 DtMmdbInfoRequest* newDtMmdbInfoRequestWithLoc(int bc_id, char* loc)
280 {
281    DtMmdbInfoRequest* x =
282         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
283
284   
285    x -> bookcase_descriptor = bc_id ;
286    x -> primary_oid = 0;
287    x -> secondary_oid = 0;
288    x -> locator_ptr = loc;
289    return x;
290 }
291
292 DtMmdbInfoRequest* newDtMmdbInfoRequestWithPrimaryOid(int bc_id, char* oid_str)
293 {
294    DtMmdbInfoRequest* x =
295         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
296
297   
298    x -> bookcase_descriptor = bc_id ;
299    x -> locator_ptr = 0;
300    x -> primary_oid = newDtMmdbHandle(oid_t(oid_str, true, false));
301    x -> secondary_oid = 0;
302    return x;
303 }
304
305 DtMmdbInfoRequest* newDtMmdbInfoRequestWithSecondaryOid(int bc_id, char* oid_str)
306 {
307    DtMmdbInfoRequest* x =
308         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
309
310   
311    x -> bookcase_descriptor = bc_id ;
312    x -> locator_ptr = 0;
313    x -> primary_oid = 0;
314    x -> secondary_oid = newDtMmdbHandle(oid_t(oid_str, true, false));
315    return x;
316 }
317
318 DtMmdbInfoRequest* newDtMmdbInfoRequestWithSeqnum(int bc_id, char* str)
319 {
320    DtMmdbInfoRequest* x =
321         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
322
323   
324    x -> bookcase_descriptor = bc_id ;
325    x -> locator_ptr = 0;
326    x -> primary_oid = 0;
327    x -> secondary_oid = 0;
328    x -> sequence_num = atoi(str);
329    return x;
330 }
331
332 char* DtMmdbHandleToString(DtMmdbHandle* x)
333 {
334    static char buf[100];
335    if ( x -> oid_ptr) {
336       oid_t *z = (oid_t*)(x -> oid_ptr);
337       snprintf(buf, sizeof(buf), "%d.%d", z -> ccode(), (int)z -> icode());
338    } else
339      buf[0] = 0;
340
341    return buf;
342 }
343
344 void DtMmdbFreeInfoRequest(DtMmdbInfoRequest* x)
345 {
346    if ( x ) {
347      DtMmdbFreeHandle(x-> primary_oid);
348      DtMmdbFreeHandle(x-> secondary_oid);
349      free(x);
350    }
351 }
352 }
353