oid_list.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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: oid_list.cc /main/5 1996/07/18 14:43:28 drk $
  25. *
  26. * Copyright (c) 1993 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. #include "object/oid_list.h"
  50. void oid_list::init_persistent_info(persistent_info* x)
  51. {
  52. ostring* u = list_ptr.p;
  53. root::init_persistent_info(x);
  54. if ( get_mode(OLD_OBJECT) == false ) {
  55. //MESSAGE(cerr, "oid_list::init_persistent_info");
  56. //debug(cerr, (void*)this);
  57. //debug(cerr, my_oid());
  58. char* w = 0;
  59. int v= 0;
  60. //MESSAGE(cerr, "oid_list::init_persistent_info(), new object case");
  61. //debug(cerr, (void*)this);
  62. //debug(cerr, (void*)list_ptr.p);
  63. if (u) {
  64. w = u -> get();
  65. v = u -> size();
  66. }
  67. if ( storage_ptr )
  68. storage_ptr -> insertString(list_ptr.loc, w, v);
  69. //debug(cerr, list_ptr.loc);
  70. //debug(cerr, v_sz);
  71. }
  72. delete u;
  73. }
  74. oid_list::oid_list(c_code_t c_id) : composite(c_id),
  75. f_internal_index(false), chk_index(0)
  76. {
  77. list_ptr.loc = 0;
  78. init_data_member(0);
  79. }
  80. oid_list::oid_list(int num_oids, c_code_t c_id) : composite(c_id),
  81. f_internal_index(false), chk_index(0)
  82. {
  83. list_ptr.loc = 0;
  84. init_data_member(num_oids);
  85. }
  86. oid_list::oid_list(oid_list& x) :
  87. composite(x), chk_index(0)
  88. {
  89. list_ptr.loc = 0;
  90. init_data_member(x.v_sz);
  91. for ( unsigned int i=1; i<=v_sz; i++ )
  92. update_component(i, x(i));
  93. }
  94. oid_list::~oid_list()
  95. {
  96. if ( get_mode(PERSISTENT) == false ) {
  97. delete list_ptr.p;
  98. }
  99. delete chk_index;
  100. }
  101. void oid_list::init_data_member(int leng)
  102. {
  103. int char_leng = leng * OID_T_SZ ;
  104. char* ptr = new char[char_leng];
  105. oid_t x(ground);
  106. char* y = ptr;
  107. for ( int i=0; i<leng; i++ ) {
  108. x.to_char_string(y, false);
  109. y += OID_T_SZ;
  110. }
  111. list_ptr.loc = 0; // to please purify
  112. list_ptr.p = new ostring(ptr, char_leng);
  113. //MESSAGE(cerr, "oid_list::init_data_member(int leng)");
  114. //debug(cerr, (void*)this);
  115. //debug(cerr, (void*)list_ptr.p);
  116. delete [] ptr;
  117. v_sz = leng;
  118. set_mode(UPDATE, true);
  119. }
  120. void oid_list::build_internal_index()
  121. {
  122. if ( get_mode(OLD_OBJECT) == true &&
  123. get_mode(PERSISTENT) == true &&
  124. v_sz > 1000
  125. )
  126. chk_index = new chunks_index(storage_ptr, list_ptr.loc);
  127. else
  128. chk_index = 0;
  129. }
  130. void oid_list::reqest_build_internal_index()
  131. {
  132. f_internal_index = true;
  133. }
  134. Boolean oid_list::expand_space(int extra_oids)
  135. {
  136. /*
  137. MESSAGE(cerr, "in expand_space()");
  138. debug(cerr, my_oid());
  139. debug(cerr, extra_oids);
  140. debug(cerr, v_sz);
  141. debug(cerr, int(list_ptr.loc));
  142. debug(cerr, int(&list_ptr.loc));
  143. */
  144. Boolean ok;
  145. int extra_char_leng = extra_oids * OID_T_SZ;
  146. if ( get_mode(PERSISTENT) == true ) {
  147. int total_char_leng = extra_char_leng + v_sz * OID_T_SZ;
  148. char* ptr = new char[total_char_leng];
  149. memset(ptr, (char)0, total_char_leng);
  150. if ( list_ptr.loc == 0 ) {
  151. storage_ptr -> insertString(list_ptr.loc, ptr, total_char_leng);
  152. } else {
  153. storage_ptr -> appendString(list_ptr.loc, ptr, total_char_leng);
  154. }
  155. delete [] ptr;
  156. ok = true;
  157. } else {
  158. ok = list_ptr.p -> expand( extra_char_leng );
  159. }
  160. v_sz += extra_oids;
  161. set_mode(UPDATE, true);
  162. return ok;
  163. }
  164. io_status oid_list::asciiOut(ostream& out)
  165. {
  166. if ( !out )
  167. return fail;
  168. out << v_sz << '\n';
  169. int ind = first();
  170. while (ind ) {
  171. oid_t x = (*this)(ind);
  172. if ( x.eq(ground) == false ) {
  173. x.asciiOut(out);
  174. out << "\n";
  175. }
  176. next(ind);
  177. }
  178. return done;
  179. }
  180. oid_t oid_list::operator()(int ind)
  181. {
  182. /*
  183. MESSAGE(cerr, "oid_list::operator()");
  184. debug(cerr, my_oid());
  185. debug(cerr, ind);
  186. debug(cerr, v_sz);
  187. debug(cerr, int(this));
  188. debug(cerr, int(list_ptr.p));
  189. */
  190. if ( !INRANGE(ind, 1, (int) v_sz) ) {
  191. MESSAGE(cerr, "oid_list::opeartor(): out of range");
  192. throw(boundaryException(1, v_sz, ind));
  193. }
  194. char z[OID_T_SZ];
  195. int offset = (ind - 1) * OID_T_SZ;
  196. if ( get_mode(PERSISTENT) == true )
  197. {
  198. if ( list_ptr.loc == 0 )
  199. throw(stringException("zero loc value"));
  200. if ( f_internal_index == true && chk_index == 0 ) {
  201. build_internal_index();
  202. }
  203. if ( chk_index ) {
  204. str_index_record_t* anchor =
  205. chk_index -> chunk_location(offset);
  206. storage_ptr ->
  207. readString(anchor -> loc, z, OID_T_SZ,
  208. offset - anchor -> str_offset);
  209. } else
  210. storage_ptr ->
  211. readString(list_ptr.loc, z, OID_T_SZ, offset);
  212. } else {
  213. if ( list_ptr.p == 0 )
  214. throw(stringException("zero list_ptr.p value"));
  215. memcpy(z, list_ptr.p -> get() + offset, OID_T_SZ);
  216. }
  217. //MESSAGE(cerr, "oid_list::operator() done");
  218. #ifdef PORTABLE_DB
  219. return oid_t(z, false, swap_order());
  220. #else
  221. return oid_t(z, false, false);
  222. #endif
  223. }
  224. Boolean
  225. oid_list::insert_component(const oid_t& new_comp_oid)
  226. {
  227. /*
  228. MESSAGE(cerr, "oid_list::insert_component");
  229. debug(cerr, v_sz);
  230. debug(cerr, (void*)this);
  231. debug(cerr, int(list_ptr.p));
  232. new_comp_oid.asciiOut(cerr); cerr << "\n";
  233. */
  234. char z[OID_T_SZ];
  235. new_comp_oid.to_char_string(z, swap_order());
  236. if ( get_mode(PERSISTENT) == true ) {
  237. if ( list_ptr.loc == 0 ) {
  238. storage_ptr ->
  239. insertString(list_ptr.loc, z, OID_T_SZ);
  240. } else {
  241. storage_ptr ->
  242. appendString(list_ptr.loc, z, OID_T_SZ);
  243. }
  244. } else {
  245. list_ptr.p -> append(z, OID_T_SZ);
  246. }
  247. v_sz++;
  248. set_mode(UPDATE, true);
  249. return true;
  250. }
  251. Boolean
  252. oid_list::update_component(int index, const oid_t& new_oid)
  253. {
  254. //MESSAGE(cerr, "oid_list::update_component()");
  255. //debug(cerr, my_oid());
  256. if ( !INRANGE(index, 1, (int) v_sz) ) {
  257. MESSAGE(cerr, "oid_list update(): out of range");
  258. throw(boundaryException(1, v_sz, index));
  259. }
  260. char z[OID_T_SZ];
  261. Boolean swap;
  262. if ( get_mode(PERSISTENT) == true && swap_order() == true )
  263. swap = true;
  264. else
  265. swap = false;
  266. new_oid.to_char_string(z, swap);
  267. if ( get_mode(PERSISTENT) == true ) {
  268. if ( list_ptr.loc == 0 )
  269. throw(stringException("zero list_ptr.p value"));
  270. storage_ptr ->
  271. updateString(list_ptr.loc, z,
  272. OID_T_SZ, (index-1) * OID_T_SZ
  273. );
  274. } else {
  275. //MESSAGE(cerr, "oid_list::update_component(): vm update");
  276. if ( list_ptr.p == 0 )
  277. throw(stringException("zero list_ptr.p value"));
  278. list_ptr.p -> update(z, OID_T_SZ, (index-1)*OID_T_SZ);
  279. }
  280. //MESSAGE(cerr, "oid_list::update_component() done");
  281. return true;
  282. }
  283. io_status oid_list::asciiIn(istream& in)
  284. {
  285. //MESSAGE(cerr, "oid_list::asciiIn()");
  286. if ( in.get() != LIST_MARK_CHAR )
  287. throw(formatException("should be a LIST_MARK_CHAR"));
  288. if ( in.get() != '\n' )
  289. throw(formatException("should be a \n"));
  290. dlist temp_list;
  291. dlist_void_ptr_cell* y = 0;
  292. char c;
  293. while ( (c = in.get()) != LIST_MARK_CHAR ) {
  294. in.putback(c);
  295. oid_t *x = new oid_t(ground);
  296. x -> asciiIn(in);
  297. y = new dlist_void_ptr_cell(x);
  298. temp_list.insert_as_tail(y);
  299. v_sz++;
  300. }
  301. if ( in.get() != '\n' ) {
  302. throw(formatException("bad oid_list input stream"));
  303. }
  304. int oid_array_sz = v_sz * OID_T_SZ;
  305. char *oid_array = new char[oid_array_sz];
  306. long ind = temp_list.first();
  307. int i = 0;
  308. char z[OID_T_SZ];
  309. while ( ind ) {
  310. y = (dlist_void_ptr_cell*)(ind);
  311. ((oid_t*)(y -> void_ptr())) ->
  312. to_char_string(z, swap_order());
  313. delete ((oid_t*)(y -> void_ptr()));
  314. memcpy(oid_array + i * OID_T_SZ, z, OID_T_SZ);
  315. i++;
  316. temp_list.next(ind);
  317. }
  318. // save the list
  319. if ( get_mode(PERSISTENT) == true ) {
  320. if ( list_ptr.loc == 0 )
  321. storage_ptr -> insertString(list_ptr.loc, oid_array, oid_array_sz);
  322. else
  323. storage_ptr -> updateString(list_ptr.loc, oid_array, oid_array_sz);
  324. set_mode(UPDATE, true);
  325. } else {
  326. delete list_ptr.p;
  327. list_ptr.p = new ostring(0);
  328. list_ptr.p -> set(oid_array, v_sz);
  329. }
  330. delete [] oid_array;
  331. return done;
  332. }
  333. Boolean oid_list::remove_component(const oid_t& x)
  334. {
  335. int ind = first();
  336. while ( ind ) {
  337. if ( (*this)(ind).eq(x) == true ) {
  338. update_component(ind, ground);
  339. set_mode(UPDATE, true);
  340. return true;
  341. }
  342. next(ind);
  343. }
  344. return false;
  345. }
  346. int oid_list::cdr_sizeof()
  347. {
  348. return composite::cdr_sizeof() + sizeof(list_ptr.loc);
  349. }
  350. io_status oid_list::cdrOut(buffer& buf)
  351. {
  352. /*
  353. MESSAGE(cerr, "oid_list::cdrOut()");
  354. debug(cerr, (void*)this);
  355. debug(cerr, (void*)list_ptr.loc);
  356. debug(cerr, my_oid());
  357. */
  358. composite::cdrOut(buf);
  359. buf.put(list_ptr.loc);
  360. return done;
  361. }
  362. io_status oid_list::cdrIn(buffer& buf)
  363. {
  364. //MESSAGE(cerr, "oid_list::cdrIn()");
  365. //debug(cerr, (void*)this);
  366. composite::cdrIn(buf);
  367. buf.get(list_ptr.loc);
  368. //debug(cerr, (void*)list_ptr.loc);
  369. //debug(cerr, my_oid());
  370. return done;
  371. }
  372. MMDB_BODIES(oid_list)
  373. oid_list_handler::oid_list_handler(int num_oids, storagePtr store):
  374. handler(OID_LIST_CODE, store)
  375. {
  376. ((oid_list*)obj_ptr) -> init_data_member(num_oids);
  377. }
  378. oid_list_handler::oid_list_handler(const oid_t& v_oid, storagePtr store):
  379. handler(v_oid, store)
  380. {
  381. }
  382. oid_list_handler::~oid_list_handler()
  383. {
  384. }
  385. oid_list* oid_list_handler::operator ->()
  386. {
  387. return (oid_list*)handler::operator->();
  388. }