Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / c_api_test.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_test.cc /main/4 1996/06/11 17:28:13 cde-hal $
24
25 #include "oliasdb/DtMmdb.h"
26 #include "oliasdb/mmdb.h"
27 #include "oliasdb/c_api_common.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30
31
32 void showBookCaseInfo(int i);
33
34 int showInfoLibInfo(int i)
35 {
36    int n = 0;
37    int j;
38    DtMmdbInfoLibInfo* info = DtMmdbInfoLibGetInfo(i);
39    if ( info ) {
40       if ( info -> path )
41         fprintf(stderr, "infolibInfo: path= %s\n", info -> path);
42       if ( info -> name )
43         fprintf(stderr, "infolibInfo: name= %s\n", info -> name);
44       fprintf(stderr, "infolibInfo: num_bookcases= %d\n", info -> num_bookcases);
45
46       n = info -> num_bookcases;
47
48       DtMmdbInfoLibFreeInfo(info);
49    } else
50       fprintf(stderr, "no infolibInfo\n");
51
52    return n;
53 }
54
55 void showBookCaseInfo(int i)
56 {
57    if ( i <= -1 ) {
58       fprintf(stderr, "bad bookcase descriptor %d\n", i);
59    } else {
60       DtMmdbBookCaseInfo* info = DtMmdbBookCaseGetInfo(i);
61       if ( info ) {
62          if ( info -> name )
63            fprintf(stderr, "    bookcaseInfo: name= %s\n", info -> name);
64          fprintf(stderr, "      bookcaseInfo: num_bookcases= %d\n", info -> num_books);
65          DtMmdbBookCaseFreeInfo(info);
66       } else
67          fprintf(stderr, "no bookcase info\n");
68    }
69 }
70
71 // automatica test cases 
72
73 extern int auto_test(int argc, char** argv, OLIAS_DB& db);
74
75 extern "C"
76 int auto_test_c_api(int argc, char** argv)
77 {
78    OLIAS_DB db;
79    return auto_test(argc, argv, db);
80 }
81
82 // test cases that require manual inspection
83 extern "C"
84 int test0(int argc, char** argv)
85 {
86    int j = 0;
87    int n = 0;
88    int i = -1;
89    switch ( argc ) {
90      case 4:
91        i = DtMmdbOpenInfoLib(argv[2], argv[3], DtMmdbFalse);
92        n = showInfoLibInfo(i);
93        for ( j=0; j<n; j++ )
94           showBookCaseInfo(DtMmdbGetBookCaseByIndex(i, j));
95        break;
96      case 3:
97        i = DtMmdbOpenInfoLib(argv[2], 0, DtMmdbFalse);
98        showInfoLibInfo(i);
99        n = showInfoLibInfo(i);
100        for ( j=0; j<n; j++ )
101           showBookCaseInfo(DtMmdbGetBookCaseByIndex(i, j));
102        break;
103      default:
104        fprintf(stderr, "bad argment list\n");
105    }
106
107    fprintf(stderr, "infolib desc=%d\n", i);
108    return 0;
109 }
110
111 extern "C"
112 int test1(int argc, char** argv)
113 {
114    int j = 0;
115    int n = 0;
116    int i = -1;
117    switch ( argc ) {
118      case 5:
119        i = DtMmdbOpenInfoLib(argv[2], argv[3], DtMmdbFalse);
120        n = showInfoLibInfo(i);
121        for ( j=0; j<n; j++ )
122           showBookCaseInfo(DtMmdbGetBookCaseByLoc(i, argv[4]));
123        break;
124      default:
125        fprintf(stderr, "bad argment list\n");
126    }
127
128    fprintf(stderr, "infolib desc=%d\n", i);
129    return 0;
130 }
131
132 DtMmdbInfoRequest* newDtMmdbInfoRequestWithLoc(int bc_id, char* loc)
133 {
134    DtMmdbInfoRequest* x = 
135         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
136
137    
138    x -> bookcase_descriptor = bc_id ;
139    x -> locator_ptr = loc;
140    return x;
141 }
142
143 DtMmdbInfoRequest* newDtMmdbInfoRequestWithPrimaryOid(int bc_id, char* oid_str)
144 {
145    DtMmdbInfoRequest* x = 
146         (DtMmdbInfoRequest*) malloc(sizeof(DtMmdbInfoRequest));
147
148    
149    x -> bookcase_descriptor = bc_id ;
150    x -> primary_oid = newDtMmdbHandle(oid_t(oid_str, true, false));
151    return x;
152 }
153
154 char* DtMmdbHandleToString(DtMmdbHandle* x)
155 {
156    static char buf[100];
157    if ( x -> oid_ptr) {
158       oid_t *z = (oid_t*)(x -> oid_ptr);
159       sprintf(buf, "%d.%d", z -> ccode(), z -> icode());
160    } else
161      buf[0] = 0;
162
163    return buf;
164 }
165
166 void showSection(DtMmdbInfoRequest* req)
167 {
168    if ( req == 0 ) return ;
169
170     const char* str = DtMmdbSectionGetLongTitle(req, 0);
171     if (str) {
172     fprintf(stderr, "        SectionInfo: LongTitle=%s\n", str);
173     }
174
175     str = DtMmdbSectionGetShortTitle(req, 0);
176     if (str) {
177     fprintf(stderr, "        SectionInfo: ShortTitle=%s\n", str);
178     }
179
180     str = DtMmdbSectionGetData(req, 0);
181     if (str) {
182     fprintf(stderr, "        SectionInfo: data=%s\n", str);
183     }
184
185     int size = DtMmdbSectionGetDataSize(req);
186     fprintf(stderr, "        SectionInfo: dataSize=%d\n", size);
187
188
189     str = DtMmdbSectionGetTocLoc(req);
190     if (str) {
191     fprintf(stderr, "        SectionInfo: TocLoc=%s\n", str);
192     }
193
194     DtMmdbHandle* id = DtMmdbSectionGetBookId(req);
195     if (id) {
196     fprintf(stderr, "        SectionInfo: BookId=%s\n", DtMmdbHandleToString(id));
197     }
198
199     id = DtMmdbSectionGetStyleSheetId(req);
200     if (id) {
201     fprintf(stderr, "        SectionInfo: StyleSheetId=%s\n", DtMmdbHandleToString(id));
202     }
203 }
204
205 void showTOC(DtMmdbInfoRequest* req)
206 {
207    if ( req == 0 ) return ;
208
209    DtMmdbHandle* id = DtMmdbTocGetParentId(req);
210    if (id) 
211       fprintf(stderr, "        TOC: parentId=%s\n", DtMmdbHandleToString(id));
212    
213
214    unsigned int ct = 0;
215    DtMmdbHandle** ids = DtMmdbTocGetChildIds(req, &ct);
216    if ( ids ) {
217      fprintf(stderr, "ct=%d\n", ct);
218      for ( int i=0; i<ct; i++ )
219         fprintf(stderr, "        TOC: childid[%d]=%s\n", i, DtMmdbHandleToString(ids[i]));
220    }
221
222    int i = DtMmdbTocGetNumOfChildren(req);
223    fprintf(stderr, "        TOC: NumOfChildren=%d\n", i);
224 }
225
226 extern "C"
227 int test2(int argc, char** argv)
228 {
229    int j = 0;
230    int n = 0;
231    int i = -1;
232    const char* str;
233    switch ( argc ) {
234      case 5:
235       {
236        i = DtMmdbOpenInfoLib(argv[2], argv[3], DtMmdbFalse);
237        j = DtMmdbGetBookCaseByLoc(i, argv[4]);
238
239        DtMmdbInfoRequest* req = newDtMmdbInfoRequestWithLoc(j, argv[4]);
240        showSection(req);
241        break;
242       }
243      default:
244        fprintf(stderr, "invalid arguments\n");
245    }
246
247    fprintf(stderr, "infolib desc=%d\n", i);
248    return 0;
249 }
250
251 extern "C"
252 int test3(int argc, char** argv)
253 {
254    int j = 0;
255    int n = 0;
256    int i = -1;
257    const char* str;
258    switch ( argc ) {
259      case 5:
260       {
261        i = DtMmdbOpenInfoLib(argv[2], argv[3], DtMmdbFalse);
262        j = DtMmdbGetBookCaseByName(i, argv[3]);
263
264        DtMmdbInfoRequest* req = newDtMmdbInfoRequestWithPrimaryOid(j, argv[4]);
265        showSection(req);
266        break;
267       }
268       default:
269        fprintf(stderr, "invalid arguments\n");
270    }
271
272    fprintf(stderr, "infolib desc=%d\n", i);
273    return 0;
274 }
275
276 extern "C"
277 int test4(int argc, char** argv)
278 {
279    int j = 0;
280    int i = 0;
281    const char* str;
282    switch ( argc ) {
283      case 5:
284       {
285        i = DtMmdbOpenInfoLib(argv[2], argv[3], DtMmdbFalse);
286        j = DtMmdbGetBookCaseByName(i, argv[3]);
287
288        DtMmdbInfoRequest* req = newDtMmdbInfoRequestWithPrimaryOid(j, argv[4]);
289        showTOC(req);
290        break;
291       }
292       default:
293        fprintf(stderr, "invalid arguments\n");
294    }
295
296    fprintf(stderr, "infolib desc=%d\n", i);
297    return 0;
298 }
299
300
301
302