Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / ostring.C
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 librararies 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
44
45 #include "utility/ostring.h"
46
47 #ifdef C_API
48 char* ostring::input_buf = 0;
49 #else
50 char ostring::input_buf[LBUFSIZ];
51 #endif
52
53 ostring::ostring() : v_sz(0), v_allo_sz(1)
54 {
55    v_p = new char[1];
56 }
57
58 ostring::ostring(const int i) : v_sz(0), v_allo_sz(i+1)
59 {
60    v_p = new char[i+1];
61 }
62
63 ostring::ostring(char* x, const int i) 
64 {
65    int w = i;
66
67    if ( w == -1 ) {
68       w = strlen(x);
69    }
70
71    v_sz = w;
72    v_allo_sz = w+1;
73
74    v_p = new char[w+1];
75    memcpy(v_p, x, w);
76    v_p[w] = 0;
77 }
78
79 ostring::ostring(const ostring& s) : 
80 v_sz(s.v_sz), v_allo_sz(s.v_allo_sz)
81 {
82    v_p = new char[v_allo_sz];
83    memcpy(v_p, s.v_p, v_sz);
84    v_p[v_sz] = 0;
85 }
86
87 /*
88 ostring::~ostring()
89 {
90    delete v_p;
91 }
92 */
93
94 Boolean ostring::set(const char* x, int l)
95 {
96    expand(l+1);
97
98    memcpy(v_p, x, l);
99    v_p[l] = 0;
100    v_sz = l;
101
102    return true;
103 }
104
105 /*
106 Boolean ostring::set(const char* x)
107 {
108    return set(x, strlen(x));
109 }
110
111 void ostring::reset()
112 {
113    v_sz = 0;
114 }
115
116 void ostring::set_size(const int s)
117 {
118    v_sz = s;
119    v_p[v_sz] = 0;
120 }
121 */
122
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)
127 {
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];
131
132       if ( pre_zero == true )
133          memset(new_p, char(0), v_allo_sz);
134   
135       if ( v_p ) {
136          memcpy(new_p, v_p, v_sz);
137          delete v_p;
138       }
139
140       v_p = new_p;
141    }
142    return true;
143 }
144
145 char* ostring::acquire() 
146 {          
147    v_allo_sz = v_sz = 0;
148    char *x = v_p;
149    v_p = 0;
150    return x; 
151 }
152
153 Boolean ostring::append(const char* x, int l)
154 {
155    expand(v_sz+l+1);
156
157    memcpy(v_p+v_sz, x, l);
158    v_sz += l;
159    v_p[v_sz] = 0;
160    return true;
161 }
162
163 Boolean ostring::update(const char* x, int l, int offset)
164 {
165    if ( offset + l > v_sz ) {
166       MESSAGE(cerr, "update(): char chunk too small");
167       throw(boundaryException(0, v_sz, offset+l));
168    }
169
170    memcpy(v_p+offset, x, l);
171    return true;
172 }
173
174 int ostring::substr(ostring& s)
175 {
176    if ( v_p == 0 || s.v_p == 0 )
177       return -1;
178
179    char* sub_p = strstr(v_p, s.v_p);
180
181    if ( sub_p == 0 )
182       return -1;
183    else
184       return (int)(sub_p - v_p);
185 }
186
187 /*
188 int ostring::size() const
189 {
190    return v_sz;
191 }
192
193 int ostring::alloc_size() const
194 {
195    return v_allo_sz;
196 }
197 */
198
199 ostring& ostring::operator +(ostring& s)
200 {
201    int l1 = v_sz;
202    int l2 = s.v_sz;
203
204    ostring *new_ostring = new ostring(l1+l2);
205
206    int i;
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++;
209
210    return *new_ostring;
211 }
212
213 Boolean ostring::string_LS(ostring& y) const
214 {
215    char* x_str = this -> get() ;
216    char* y_str = y.get() ;
217
218    int x_sz = this -> size() ;
219    int y_sz = y.size() ;
220
221    if ( x_sz == y_sz ) {
222       if ( memcmp(x_str, y_str, x_sz ) < 0 )
223          return true;
224       else
225          return false;
226    } else {
227
228       int min = MIN(x_sz, y_sz);
229
230       for ( int i=0; i<min; i++ ) {
231          if ( x_str[i] < y_str[i] ) 
232             return true;
233          else
234          if ( x_str[i] > y_str[i] ) 
235             return false;
236       }
237       if ( x_sz < y_sz )
238          return true;
239       else
240          return false;
241    }
242 }
243
244 Boolean ostring::string_EQ(ostring& y) const
245 {
246   if ( this -> size() == y.size() &&
247        memcmp(this -> get(), y.get(), this -> size() ) == 0 
248      )
249      return true;
250   else
251      return false;
252 }
253
254 ostream& operator <<(ostream& s, const ostring& o)
255 {
256    if ( o.v_p ) {
257       //s << o.v_sz << ":";
258       for ( int i=0; i<o.v_sz; i++ )
259      //    if ( isprint(o.v_p[i]) )
260             s << o.v_p[i];
261      //    else
262      //       s << int(o.v_p[i]);
263    }
264    return s;
265 }
266
267 istream& operator >>(istream& s, ostring& o)
268 {
269    s.getline( o.input_buf, LBUFSIZ );
270    o.set(o.input_buf);
271    return s;
272 }
273
274    
275 ostring* ostring::operator+= (ostring* )
276 {
277   return 0;
278 }
279    
280 ostring* ostring::concate_with(...)
281 {
282   return 0 ;
283 }
284