compressed_pstring.C 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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: compressed_pstring.cc /main/4 1996/06/11 17:23:44 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. #include "object/compressed_pstring.h"
  50. #ifdef C_API
  51. buffer* compressed_pstring::working_buffer_ptr;
  52. buffer* compressed_pstring::v_cp_io_buf_ptr;
  53. #define working_buffer (*working_buffer_ptr)
  54. #define v_cp_io_buf (*v_cp_io_buf_ptr)
  55. // for pstring's buffer
  56. #define v_io_buf (*v_io_buf_ptr)
  57. #else
  58. buffer compressed_pstring::working_buffer(LBUFSIZ);
  59. buffer compressed_pstring::v_cp_io_buf(LBUFSIZ);
  60. #endif
  61. compressed_pstring::compressed_pstring(c_code_t c_id) :
  62. pstring(c_id), agent(0), v_uncompressed_sz(0)
  63. {
  64. }
  65. compressed_pstring::~compressed_pstring()
  66. {
  67. }
  68. MMDB_BODIES(compressed_pstring)
  69. char* compressed_pstring::get(buffer& string_buffer)
  70. {
  71. //MESSAGE(cerr, "compressed_pstring::get()");
  72. //debug(cerr, v_uncompressed_sz);
  73. string_buffer.reset();
  74. string_buffer.expand_chunk(v_uncompressed_sz+1);
  75. pstring::get(working_buffer);
  76. // use cached version of compression agent
  77. //debug(cerr, compress_agent_id);
  78. if ( (storage_ptr -> my_oid()).ccode() == MEM_STORAGE_CODE )
  79. agent = (compress_agent_handler*)
  80. (new handler(DICT_AGENT_CODE, storage_ptr));
  81. else
  82. agent = new compress_agent_handler(compress_agent_id, storage_ptr);
  83. (*agent) -> decompress(working_buffer, string_buffer);
  84. delete agent;
  85. string_buffer.get_base()[v_uncompressed_sz] = 0;
  86. //MESSAGE(cerr, "compressed_pstring::get() done");
  87. return string_buffer.get_base();
  88. }
  89. io_status compressed_pstring::asciiIn(istream& in)
  90. {
  91. compress_agent_id.asciiIn(in); // compress agent oid part
  92. //debug(cerr, compress_agent_id);
  93. return this -> _asciiIn(in);
  94. }
  95. io_status compressed_pstring::asciiIn(const char* buf, int size, const oid_t& id)
  96. {
  97. compress_agent_id.become(id); // compress agent oid part
  98. return this -> _asciiIn(buf, size);
  99. }
  100. io_status compressed_pstring::asciiIn(const char* buf, int size)
  101. {
  102. return pstring::asciiIn(buf, size);
  103. }
  104. io_status compressed_pstring::_asciiIn(istream& in)
  105. {
  106. // use cached version of compression agent
  107. if ( (storage_ptr -> my_oid()).ccode() == MEM_STORAGE_CODE )
  108. agent = (compress_agent_handler*)
  109. (new handler(DICT_AGENT_CODE, storage_ptr));
  110. else
  111. agent = new compress_agent_handler(compress_agent_id, storage_ptr);
  112. pstring::_asciiIn(in);
  113. _compress();
  114. return done;
  115. }
  116. io_status compressed_pstring::_asciiIn(const char* buf, int size)
  117. {
  118. // use cached version of compression agent
  119. if ( (storage_ptr -> my_oid()).ccode() == MEM_STORAGE_CODE )
  120. agent = (compress_agent_handler*)
  121. (new handler(DICT_AGENT_CODE, storage_ptr));
  122. else
  123. agent = new compress_agent_handler(compress_agent_id, storage_ptr);
  124. pstring::asciiIn(buf, size); // uncompress data part
  125. _compress();
  126. return done;
  127. }
  128. void compressed_pstring::_compress()
  129. {
  130. v_uncompressed_sz = v_io_buf.content_sz();
  131. if ( v_uncompressed_sz > 0 ) {
  132. working_buffer.reset();
  133. (*agent) -> compress(v_io_buf, working_buffer);
  134. //debug(cerr, v_uncompressed_sz);
  135. //debug(cerr, working_buffer.content_sz());
  136. pstring::update(working_buffer.get_base(), working_buffer.content_sz());
  137. }
  138. set_mode(UPDATE, true);
  139. delete agent;
  140. }
  141. int compressed_pstring::size() const
  142. {
  143. return v_uncompressed_sz;
  144. }
  145. int compressed_pstring::cdr_sizeof()
  146. {
  147. return pstring::cdr_sizeof() + sizeof(v_uncompressed_sz) +
  148. compress_agent_id.cdr_sizeof();
  149. }
  150. io_status compressed_pstring::cdrOut(buffer& buf)
  151. {
  152. //MESSAGE(cerr, "compressed_pstring: cdrOut()");
  153. //debug(cerr, compress_agent_id);
  154. pstring::cdrOut(buf);
  155. buf.put(v_uncompressed_sz);
  156. return compress_agent_id.cdrOut(buf);
  157. }
  158. io_status compressed_pstring::cdrIn(buffer& buf)
  159. {
  160. pstring::cdrIn(buf);
  161. buf.get(v_uncompressed_sz);
  162. return compress_agent_id.cdrIn(buf);
  163. }
  164. HANDLER_BODIES(compressed_pstring)