2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: ostring.cc /main/3 1996/06/11 17:38:07 cde-hal $
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.
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.
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
45 #include "utility/ostring.h"
48 char* ostring::input_buf = 0;
50 char ostring::input_buf[LBUFSIZ];
53 ostring::ostring() : v_sz(0), v_allo_sz(1)
58 ostring::ostring(const int i) : v_sz(0), v_allo_sz(i+1)
63 ostring::ostring(char* x, const int i)
79 ostring::ostring(const ostring& s) :
80 v_sz(s.v_sz), v_allo_sz(s.v_allo_sz)
82 v_p = new char[v_allo_sz];
83 memcpy(v_p, s.v_p, v_sz);
94 Boolean ostring::set(const char* x, int l)
106 Boolean ostring::set(const char* x)
108 return set(x, strlen(x));
111 void ostring::reset()
116 void ostring::set_size(const int s)
123 /********************************************/
124 // set alloc_sz to at least new_alloc_sz bytes
125 /********************************************/
126 Boolean ostring::expand(const int new_alloc_sz, Boolean pre_zero)
128 if ( new_alloc_sz > v_allo_sz ) {
129 v_allo_sz = new_alloc_sz+1;
130 char* new_p = new char[v_allo_sz];
132 if ( pre_zero == true )
133 memset(new_p, char(0), v_allo_sz);
136 memcpy(new_p, v_p, v_sz);
145 char* ostring::acquire()
147 v_allo_sz = v_sz = 0;
153 Boolean ostring::append(const char* x, int l)
157 memcpy(v_p+v_sz, x, l);
163 Boolean ostring::update(const char* x, int l, int offset)
165 if ( offset + l > v_sz ) {
166 MESSAGE(cerr, "update(): char chunk too small");
167 throw(boundaryException(0, v_sz, offset+l));
170 memcpy(v_p+offset, x, l);
174 int ostring::substr(ostring& s)
176 if ( v_p == 0 || s.v_p == 0 )
179 char* sub_p = strstr(v_p, s.v_p);
184 return (int)(sub_p - v_p);
188 int ostring::size() const
193 int ostring::alloc_size() const
199 ostring& ostring::operator +(ostring& s)
204 ostring *new_ostring = new ostring(l1+l2);
207 for ( i = 0; i<l1; (*new_ostring).v_p[i] = s.v_p[i] ) i++;
208 for ( i = 0; i<l2; (*new_ostring).v_p[l1 + i] = s.v_p[i] ) i++;
213 Boolean ostring::string_LS(ostring& y) const
215 char* x_str = this -> get() ;
216 char* y_str = y.get() ;
218 int x_sz = this -> size() ;
219 int y_sz = y.size() ;
221 if ( x_sz == y_sz ) {
222 if ( memcmp(x_str, y_str, x_sz ) < 0 )
228 int min = MIN(x_sz, y_sz);
230 for ( int i=0; i<min; i++ ) {
231 if ( x_str[i] < y_str[i] )
234 if ( x_str[i] > y_str[i] )
244 Boolean ostring::string_EQ(ostring& y) const
246 if ( this -> size() == y.size() &&
247 memcmp(this -> get(), y.get(), this -> size() ) == 0
254 ostream& operator <<(ostream& s, const ostring& o)
257 //s << o.v_sz << ":";
258 for ( int i=0; i<o.v_sz; i++ )
259 // if ( isprint(o.v_p[i]) )
262 // s << int(o.v_p[i]);
267 istream& operator >>(istream& s, ostring& o)
269 s.getline( o.input_buf, LBUFSIZ );
275 ostring* ostring::operator+= (ostring* )
280 ostring* ostring::concate_with(...)