pstring.C 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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: pstring.C /main/6 1996/09/16 14:26:44 mgreess $
  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. #include "object/pstring.h"
  50. #ifdef C_API
  51. buffer* pstring::v_io_buf_ptr;
  52. #define v_io_buf (*v_io_buf_ptr)
  53. #else
  54. buffer pstring::v_io_buf(LBUFSIZ);
  55. #endif
  56. void pstring::init_persistent_info(persistent_info* x)
  57. {
  58. root::init_persistent_info(x);
  59. if ( get_mode(OLD_OBJECT) == false ) {
  60. char* y = 0;
  61. if ( storage_ptr )
  62. storage_ptr -> insertString(v_str_ptr.loc, y, 0);
  63. }
  64. }
  65. pstring::pstring(c_code_t c_id) : primitive(c_id),
  66. v_sz(0)
  67. {
  68. //
  69. // CDExc21900
  70. // Make sure the entire union is initialized for 64bit architectures.
  71. //
  72. memset((char*) &v_str_ptr, 0, sizeof(v_str_ptr));
  73. }
  74. pstring::pstring(const char* x, int leng, c_code_t c_id) :
  75. primitive(c_id)
  76. {
  77. _init(x, leng);
  78. }
  79. pstring::pstring(pstring& x) : primitive(x)
  80. {
  81. _init((const char*)x.get(), x.v_sz);
  82. }
  83. void pstring::_init(const char* x, int leng)
  84. {
  85. v_sz = leng;
  86. if ( v_sz == 0 ) {
  87. v_str_ptr.loc = 0;
  88. return;
  89. }
  90. v_str_ptr.p = new char[leng+1];
  91. memcpy(v_str_ptr.p, x, leng);
  92. v_str_ptr.p[leng] = 0;
  93. }
  94. pstring::~pstring()
  95. {
  96. if ( get_mode(PERSISTENT) == false && NULL != v_str_ptr.p )
  97. delete v_str_ptr.p;
  98. }
  99. // /////////////////////////////////////
  100. // should not delete what get() returns!
  101. // /////////////////////////////////////
  102. char* pstring::get(buffer& string_buffer)
  103. {
  104. //MESSAGE(cerr, "get()");
  105. if ( get_mode(PERSISTENT) == true ) {
  106. /*
  107. MESSAGE(cerr, "persistent");
  108. debug(cerr, v_sz);
  109. debug(cerr, v_str_ptr.loc);
  110. debug(cerr, f_oid);
  111. */
  112. string_buffer.reset();
  113. string_buffer.expand_chunk(v_sz+1);
  114. char* this_v_str_ptr = string_buffer.get_base();
  115. storage_ptr ->
  116. readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
  117. /*
  118. for (int i=0; i<v_sz; i++)
  119. {
  120. cerr << int(this_v_str_ptr[i]) << " " ;
  121. }
  122. cerr << "\n";
  123. */
  124. this_v_str_ptr[v_sz] = 0;
  125. string_buffer.set_content_sz(v_sz);
  126. return this_v_str_ptr;
  127. } else {
  128. return v_str_ptr.p;
  129. }
  130. }
  131. /*
  132. Boolean pstring::value_LS(root& x, Boolean safe) const
  133. {
  134. if ( safe == true &&
  135. ( f_oid.ccode() != STRING_CODE ||
  136. x.my_oid().ccode() != STRING_CODE
  137. )
  138. )
  139. return false;
  140. Boolean ok;
  141. char* this_v_str_ptr;
  142. char* x_v_str_ptr;
  143. if ( get_mode(PERSISTENT) == true ) {
  144. this_v_str_ptr = new char[v_sz+1];
  145. storage_ptr -> readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
  146. } else
  147. this_v_str_ptr = v_str_ptr.p;
  148. pstring &casted_x = *(pstring*)&x;
  149. if ( casted_x.get_mode(PERSISTENT) == true ) {
  150. x_v_str_ptr = new char[v_sz+1];
  151. storage_ptr ->
  152. readString(casted_x.v_str_ptr.loc, x_v_str_ptr, casted_x.v_sz);
  153. } else
  154. x_v_str_ptr = casted_x.v_str_ptr.p;
  155. int min_len = MIN(v_sz, casted_x.v_sz);
  156. for ( int i=0; i<min_len; i++ ) {
  157. if ( this_v_str_ptr[i] < x_v_str_ptr[i] ) {
  158. ok = true;
  159. break;
  160. }
  161. if ( this_v_str_ptr[i] > x_v_str_ptr[i] ) {
  162. ok = false;
  163. break;
  164. }
  165. }
  166. if ( i == min_len && v_sz < casted_x.v_sz )
  167. ok = true;
  168. else
  169. ok = false;
  170. if ( get_mode(PERSISTENT) == true )
  171. delete this_v_str_ptr;
  172. if ( casted_x.get_mode(PERSISTENT) == true )
  173. delete x_v_str_ptr;
  174. return ok;
  175. }
  176. Boolean pstring::value_EQ(root& x, Boolean safe) const
  177. {
  178. if ( safe == true &&
  179. ( f_oid.ccode() != STRING_CODE ||
  180. x.my_oid().ccode() != STRING_CODE
  181. )
  182. )
  183. return false;
  184. pstring &casted_x = *(pstring*)&x;
  185. if ( v_sz != casted_x.v_sz )
  186. return false;
  187. Boolean ok;
  188. char* this_v_str_ptr;
  189. char* x_v_str_ptr;
  190. if ( get_mode(PERSISTENT) == true ) {
  191. this_v_str_ptr = new char[v_sz+1];
  192. storage_ptr -> readString(v_str_ptr.loc, this_v_str_ptr, v_sz);
  193. } else
  194. this_v_str_ptr = v_str_ptr.p;
  195. if ( casted_x.get_mode(PERSISTENT) == true ) {
  196. x_v_str_ptr = new char[v_sz+1];
  197. storage_ptr ->
  198. readString(casted_x.v_str_ptr.loc, x_v_str_ptr, casted_x.v_sz);
  199. } else
  200. x_v_str_ptr = casted_x.v_str_ptr.p;
  201. //ok = ( bcmp(this_v_str_ptr, x_v_str_ptr, v_sz) == 0 ) ? true : false;
  202. ok = ( memcmp(this_v_str_ptr, x_v_str_ptr, v_sz) == 0 ) ? true : false;
  203. if ( get_mode(PERSISTENT) == true )
  204. delete this_v_str_ptr;
  205. if ( casted_x.get_mode(PERSISTENT) == true )
  206. delete x_v_str_ptr;
  207. return ok;
  208. }
  209. */
  210. io_status pstring::asciiOut(ostream& out)
  211. {
  212. /*
  213. MESSAGE(cerr, "pstring::asciiOut():");
  214. debug(cerr, v_sz);
  215. debug(cerr, (void*)this);
  216. my_oid().asciiOut(cerr);
  217. cerr << "\n";
  218. */
  219. const char* x = get();
  220. out << v_sz << '\t';
  221. for ( unsigned int i=0; i<v_sz; i++ )
  222. out << x[i];
  223. return done;
  224. }
  225. io_status pstring::asciiIn(istream& in)
  226. {
  227. /*
  228. MESSAGE(cerr, "pstring::asciiIn():");
  229. debug(cerr, (void*)this);
  230. my_oid().asciiOut(cerr);
  231. cerr << "\n";
  232. */
  233. _asciiIn(in);
  234. pstring::update(v_io_buf.get_base(), v_io_buf.content_sz());
  235. return done;
  236. }
  237. io_status pstring::asciiIn(const char* buf, int size)
  238. {
  239. v_io_buf.reset();
  240. v_io_buf.expand_chunk(size);
  241. memcpy(v_io_buf.get_base(), buf, size);
  242. v_io_buf.set_content_sz(size);
  243. return done;
  244. }
  245. void pstring::_asciiIn(istream& in)
  246. {
  247. if ( ! cc_is_digit(in) )
  248. throw (stringException("a digit expected"));
  249. int len;
  250. in >> len;
  251. int tab = in.get(); // expect a '\t'
  252. if ( tab != '\t' ) {
  253. debug(cerr, len);
  254. debug(cerr, tab);
  255. throw(stringException("'\\t' expected"));
  256. }
  257. v_io_buf.reset();
  258. v_io_buf.expand_chunk(len);
  259. if ( len > 0 &&
  260. ( !in.read(v_io_buf.get_base(), len) || in.gcount() != len )
  261. ) {
  262. debug(cerr, len);
  263. debug(cerr, v_io_buf.get_base());
  264. throw(stringException("pstring::asciiIn(): read failed"));
  265. }
  266. int ret = in.get(); // expect a '\n'
  267. if ( ret != '\n' ) {
  268. debug(cerr, ret);
  269. throw(stringException(form("'\\n' expected. %c seen. (count=%d)", ret, len)));
  270. }
  271. v_io_buf.set_content_sz(len);
  272. }
  273. Boolean pstring::update(pstring& new_value)
  274. {
  275. return update((const char*)new_value.get(), new_value.v_sz);
  276. }
  277. Boolean pstring::update(const char* new_value, int new_value_sz)
  278. {
  279. /*
  280. MESSAGE(cerr, "pstring:: update value:");
  281. debug(cerr, v_str_ptr.loc);
  282. debug(cerr, f_oid);
  283. debug(cerr, v_sz);
  284. debug(cerr, new_value_sz);
  285. for (int i=0; i<new_value_sz; i++)
  286. {
  287. cerr << int(new_value[i]) << " " ;
  288. }
  289. cerr << "\n";
  290. */
  291. int old_sz = v_sz;
  292. v_sz = new_value_sz;
  293. if ( get_mode(PERSISTENT) == true ) {
  294. //MESSAGE(cerr, "persist case");
  295. if ( v_str_ptr.loc == 0 ) {
  296. storage_ptr -> insertString(v_str_ptr.loc, new_value, v_sz);
  297. } else {
  298. storage_ptr -> updateString(v_str_ptr.loc, new_value, v_sz);
  299. }
  300. } else {
  301. if ( old_sz < new_value_sz ) {
  302. delete v_str_ptr.p;
  303. v_str_ptr.p = new char[v_sz+1];
  304. }
  305. memcpy(v_str_ptr.p, new_value, v_sz);
  306. *(v_str_ptr.p + v_sz) = '\0';
  307. }
  308. set_mode(UPDATE, true);
  309. //debug(cerr, this -> get());
  310. return true;
  311. }
  312. int pstring::cdr_sizeof()
  313. {
  314. return primitive::cdr_sizeof() +
  315. sizeof(v_str_ptr.loc) +
  316. sizeof(v_sz);
  317. }
  318. io_status pstring::cdrOut(buffer& buf)
  319. {
  320. primitive::cdrOut(buf);
  321. buf.put(v_str_ptr.loc);
  322. buf.put(v_sz);
  323. return done;
  324. }
  325. io_status pstring::cdrIn(buffer& buf)
  326. {
  327. primitive::cdrIn(buf);
  328. buf.get(v_str_ptr.loc);
  329. buf.get(v_sz);
  330. return done;
  331. }
  332. MMDB_BODIES(pstring)
  333. pstring_handler::pstring_handler(const oid_t& v_oid, storagePtr _store) :
  334. handler(v_oid, _store)
  335. {
  336. }
  337. pstring_handler::pstring_handler(const char* str, int sz, storagePtr _store) :
  338. handler(STRING_CODE, _store)
  339. {
  340. (this -> operator->()) -> update(str, sz);
  341. }
  342. pstring_handler::~pstring_handler()
  343. {
  344. }
  345. pstring* pstring_handler::operator ->()
  346. {
  347. return (pstring*)handler::operator->();
  348. }