info_base.C 8.5 KB

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