2
0

ostring.C 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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: ostring.cc /main/3 1996/06/11 17:38:07 cde-hal $
  25. *
  26. * Copyright (c) 1992 HaL Computer Systems, Inc. All rights reserved.
  27. * UNPUBLISHED -- rights reserved under the Copyright Laws of the United
  28. * States. Use of a copyright notice is precautionary only and does not
  29. * imply publication or disclosure.
  30. *
  31. * This software contains confidential information and trade secrets of HaL
  32. * Computer Systems, Inc. Use, disclosure, or reproduction is prohibited
  33. * without the prior express written permission of HaL Computer Systems, Inc.
  34. *
  35. * RESTRICTED RIGHTS LEGEND
  36. * Use, duplication, or disclosure by the Government is subject to
  37. * restrictions as set forth in subparagraph (c)(l)(ii) of the Rights in
  38. * Technical Data and Computer Software clause at DFARS 252.227-7013.
  39. * HaL Computer Systems, Inc.
  40. * 1315 Dell Avenue, Campbell, CA 95008
  41. *
  42. */
  43. #include "utility/ostring.h"
  44. #ifdef C_API
  45. char* ostring::input_buf = 0;
  46. #else
  47. char ostring::input_buf[LBUFSIZ];
  48. #endif
  49. ostring::ostring() : v_sz(0), v_allo_sz(1)
  50. {
  51. v_p = new char[1];
  52. }
  53. ostring::ostring(const int i) : v_sz(0), v_allo_sz(i+1)
  54. {
  55. v_p = new char[i+1];
  56. }
  57. ostring::ostring(char* x, const int i)
  58. {
  59. int w = i;
  60. if ( w == -1 ) {
  61. w = strlen(x);
  62. }
  63. v_sz = w;
  64. v_allo_sz = w+1;
  65. v_p = new char[w+1];
  66. memcpy(v_p, x, w);
  67. v_p[w] = 0;
  68. }
  69. ostring::ostring(const ostring& s) :
  70. v_sz(s.v_sz), v_allo_sz(s.v_allo_sz)
  71. {
  72. v_p = new char[v_allo_sz];
  73. memcpy(v_p, s.v_p, v_sz);
  74. v_p[v_sz] = 0;
  75. }
  76. /*
  77. ostring::~ostring()
  78. {
  79. delete v_p;
  80. }
  81. */
  82. Boolean ostring::set(const char* x, int l)
  83. {
  84. expand(l+1);
  85. memcpy(v_p, x, l);
  86. v_p[l] = 0;
  87. v_sz = l;
  88. return true;
  89. }
  90. /*
  91. Boolean ostring::set(const char* x)
  92. {
  93. return set(x, strlen(x));
  94. }
  95. void ostring::reset()
  96. {
  97. v_sz = 0;
  98. }
  99. void ostring::set_size(const int s)
  100. {
  101. v_sz = s;
  102. v_p[v_sz] = 0;
  103. }
  104. */
  105. /********************************************/
  106. // set alloc_sz to at least new_alloc_sz bytes
  107. /********************************************/
  108. Boolean ostring::expand(const int new_alloc_sz, Boolean pre_zero)
  109. {
  110. if ( new_alloc_sz > v_allo_sz ) {
  111. v_allo_sz = new_alloc_sz+1;
  112. char* new_p = new char[v_allo_sz];
  113. if ( pre_zero == true )
  114. memset(new_p, char(0), v_allo_sz);
  115. if ( v_p ) {
  116. memcpy(new_p, v_p, v_sz);
  117. delete [] v_p;
  118. }
  119. v_p = new_p;
  120. }
  121. return true;
  122. }
  123. char* ostring::acquire()
  124. {
  125. v_allo_sz = v_sz = 0;
  126. char *x = v_p;
  127. v_p = 0;
  128. return x;
  129. }
  130. Boolean ostring::append(const char* x, int l)
  131. {
  132. expand(v_sz+l+1);
  133. memcpy(v_p+v_sz, x, l);
  134. v_sz += l;
  135. v_p[v_sz] = 0;
  136. return true;
  137. }
  138. Boolean ostring::update(const char* x, int l, int offset)
  139. {
  140. if ( offset + l > v_sz ) {
  141. MESSAGE(cerr, "update(): char chunk too small");
  142. throw(boundaryException(0, v_sz, offset+l));
  143. }
  144. memcpy(v_p+offset, x, l);
  145. return true;
  146. }
  147. int ostring::substr(ostring& s)
  148. {
  149. if ( v_p == 0 || s.v_p == 0 )
  150. return -1;
  151. char* sub_p = strstr(v_p, s.v_p);
  152. if ( sub_p == 0 )
  153. return -1;
  154. else
  155. return (int)(sub_p - v_p);
  156. }
  157. /*
  158. int ostring::size() const
  159. {
  160. return v_sz;
  161. }
  162. int ostring::alloc_size() const
  163. {
  164. return v_allo_sz;
  165. }
  166. */
  167. ostring& ostring::operator +(ostring& s)
  168. {
  169. int l1 = v_sz;
  170. int l2 = s.v_sz;
  171. ostring *new_ostring = new ostring(l1+l2);
  172. int i;
  173. for ( i = 0; i<l1; (*new_ostring).v_p[i] = s.v_p[i] ) i++;
  174. for ( i = 0; i<l2; (*new_ostring).v_p[l1 + i] = s.v_p[i] ) i++;
  175. return *new_ostring;
  176. }
  177. Boolean ostring::string_LS(ostring& y) const
  178. {
  179. char* x_str = this -> get() ;
  180. char* y_str = y.get() ;
  181. int x_sz = this -> size() ;
  182. int y_sz = y.size() ;
  183. if ( x_sz == y_sz ) {
  184. if ( memcmp(x_str, y_str, x_sz ) < 0 )
  185. return true;
  186. else
  187. return false;
  188. } else {
  189. int min = MIN(x_sz, y_sz);
  190. for ( int i=0; i<min; i++ ) {
  191. if ( x_str[i] < y_str[i] )
  192. return true;
  193. else
  194. if ( x_str[i] > y_str[i] )
  195. return false;
  196. }
  197. if ( x_sz < y_sz )
  198. return true;
  199. else
  200. return false;
  201. }
  202. }
  203. Boolean ostring::string_EQ(ostring& y) const
  204. {
  205. if ( this -> size() == y.size() &&
  206. memcmp(this -> get(), y.get(), this -> size() ) == 0
  207. )
  208. return true;
  209. else
  210. return false;
  211. }
  212. ostream& operator <<(ostream& s, const ostring& o)
  213. {
  214. if ( o.v_p ) {
  215. //s << o.v_sz << ":";
  216. for ( int i=0; i<o.v_sz; i++ )
  217. // if ( isprint(o.v_p[i]) )
  218. s << o.v_p[i];
  219. // else
  220. // s << int(o.v_p[i]);
  221. }
  222. return s;
  223. }
  224. istream& operator >>(istream& s, ostring& o)
  225. {
  226. s.getline( o.input_buf, LBUFSIZ );
  227. o.set(o.input_buf);
  228. return s;
  229. }
  230. ostring* ostring::operator+= (ostring* )
  231. {
  232. return 0;
  233. }
  234. ostring* ostring::concate_with(...)
  235. {
  236. return 0 ;
  237. }