OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / c_api_book.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_book.cc /main/4 1996/06/11 17:26:37 cde-hal $
24
25 #include "oliasdb/c_api_common.h"
26
27 static doc_smart_ptr*
28 getDocUsingPrOrSecOrSeq(DtMmdbInfoRequest* request)
29 {
30    mtry {
31       info_base* x = getBookCase(request -> bookcase_descriptor);
32       if ( x == 0 ) return 0;
33
34       oid_t * id = getPrimiaryOid(request);
35
36       if ( id ) {
37          return new doc_smart_ptr(x, *id);
38       }
39
40       id = getSecondaryOid(request); // book id
41
42       if ( id ) {
43          return new doc_smart_ptr(*id, x);
44       }
45
46       int seq = getSeqNum(request); // seq number
47
48       if ( seq == -1 ) return 0;
49
50 // bookcase is treated 1 based internally. But 0-based externally.
51       return new doc_smart_ptr(x, seq+1);
52    }
53
54    mcatch (mmdbException &,e)
55    {
56      return 0;
57    } end_try;
58      return 0;
59 }
60
61 DtMmdbHandle*
62 DtMmdbBookGetTocObjectId(DtMmdbInfoRequest* request)
63 {
64    mtry {
65       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
66       if ( x == 0 ) return 0;
67
68       DtMmdbHandle *z = newDtMmdbHandle(x -> locator_id());
69
70       delete x;
71       return z;
72    }
73
74    mcatch (mmdbException &,e)
75    {
76      return 0;
77    } end_try;
78      return 0;
79 }
80
81 const char*
82 DtMmdbBookGetShortTitle(DtMmdbInfoRequest* request, unsigned int* length)
83 {
84    mtry {
85       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
86       if ( x == 0 ) return 0;
87
88       const char* z = x -> short_title();
89
90       if ( length ) *length = strlen(z);
91
92       delete x;
93       return z;
94    }
95
96    mcatch (mmdbException &,e)
97    {
98      return 0;
99    } end_try;
100      return 0;
101 }
102
103 const char*
104 DtMmdbBookGetLongTitle(DtMmdbInfoRequest* request, unsigned int* length)
105 {
106    mtry {
107       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
108       if ( x == 0 ) return 0;
109
110       const char* z = x -> long_title();
111
112       if ( length ) *length = strlen(z);
113
114       delete x;
115       return z;
116    }
117
118    mcatch (mmdbException &,e)
119    {
120      return 0;
121    } end_try;
122      return 0;
123 }
124
125 int DtMmdbBookGetSeqNum(DtMmdbInfoRequest* request)
126 {
127    mtry {
128       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
129       if ( x == 0 ) return 0;
130
131       int z = x -> seq_num();
132
133       delete x;
134       return z;
135    }
136
137    mcatch (mmdbException &,e)
138    {
139      return 0;
140    } end_try;
141      return 0;
142 }
143
144 const char*
145 DtMmdbBookGetSeqLIcense(DtMmdbInfoRequest* request, unsigned int* length)
146 {
147     mtry {
148       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
149       if ( x == 0 ) return 0;
150
151       const char* z = x -> license_terms();
152       if ( length ) 
153         *length = x -> license_terms_size();
154
155       delete x;
156       return z;
157    }
158
159    mcatch (mmdbException &,e)
160    {
161      return 0;
162    } end_try;
163      return 0;
164 }
165
166 DtMmdbHandle** DtMmdbBookGetTabList(DtMmdbInfoRequest* request, unsigned int* length)
167 {
168    mtry {
169       doc_smart_ptr* x = getDocUsingPrOrSecOrSeq(request);
170       if ( x == 0 ) return 0;
171
172       short_list_handler* z = x -> tab_list();
173
174       if ( z == 0 ) return 0;
175
176       int count = (*z) -> count();
177
178       DtMmdbHandle** u = (DtMmdbHandle**)malloc(sizeof(DtMmdbHandle*)* (count+1));
179
180       if ( u == 0 ) return 0;
181          
182       const char* desc = 0;
183       pstring_handler *p = 0;
184
185       int i;
186       for (i=0; i<count; i++) {
187          p = (pstring_handler *)((*z)->get_component (i+1));
188
189 // The format is the title, a tab char, then the section oid.
190          desc = (*p)->get();
191
192          while (*desc != '\0' && *desc != '\t') desc++;
193
194          if (*desc != '\t')
195            return 0;
196
197          desc++;
198
199          oid_t w((char*)desc, true, false);
200          u[i] = newDtMmdbHandle(w);
201
202          delete p;
203
204       }
205
206       u[i] = 0;
207
208       if ( length ) *length = count;
209
210       delete z;
211       delete x;
212       return u;
213    }
214
215    mcatch (mmdbException &,e)
216    {
217      return 0;
218    } end_try;
219      return 0;
220 }