c_api_test.C 7.3 KB

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