e1af1efa9cc37dd3b7d2353b28cf76886edcb71a
[oweals/cde.git] / cde / lib / tt / lib / util / tt_string.h
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 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: tt_string.h /main/5 1996/09/24 10:06:16 drk $                                                        */
28 /*-*-C++-*-
29  * 
30  * @(#)tt_string.h      1.27 @(#)
31  *
32  * tt_string.h
33  *
34  * Copyright (c) 1990 by Sun Microsystems, Inc.
35  *
36  * Headers for tt string class.
37  */
38 #ifndef _TT_STRING_H
39 #define _TT_STRING_H
40
41 #include "tt_options.h"
42 #if defined(OPT_BUG_SUNOS_4) && defined(__GNUG__)
43 #       include <stdlib.h>
44 #endif
45 #include <limits.h>
46
47 #if defined(OPT_BUG_HPUX)
48 extern "C" {
49 #include <wchar.h>
50 };
51 #elif defined(OPT_BUG_SUNOS_5) || defined(OPT_BUG_USL) || defined(OPT_BUG_UXP)
52 #include <widec.h>
53 #include <wctype.h>
54 #elif defined(OPT_BUG_AIX)
55 #include <string.h>
56 #elif defined(__osf__)
57 #include <wchar.h>
58 #endif
59
60 #include <rpc/rpc.h>
61 #include "util/tt_list.h"
62 #include "util/tt_object.h"
63 #include <memory.h>
64 #if defined(ultrix)
65 #define bool_t int
66 #endif
67
68 const int _Tt_string_unlimited  = -1;
69 const int _Tt_string_user_width = -2;
70
71 declare_list_of(_Tt_string_buf)
72 class _Tt_string_buf : public _Tt_object {
73     friend class _Tt_string;
74       public:
75         _Tt_string_buf() 
76         {
77                 content = (char *)0;
78                 length = 0;
79         }
80         _Tt_string_buf(const _Tt_string_buf &b);
81         virtual ~_Tt_string_buf();
82         void    set(const unsigned char*s, int len);
83         bool_t  operator==(const _Tt_string_buf &b);
84         bool_t  xdr(XDR *xdrs);
85         bool_t  xdr_1(XDR *xdrs);
86         bool_t  xdr_2(XDR *xdrs);
87         // _Tt_string_unlimited:  no limit
88         // _Tt_string_user_width: use $TT_ARG_TRACE_WIDTH or $_SUN_TT_ARG_TRACE_WIDTH
89         void    print(const _Tt_ostream &os,
90                       int max_print_width = 80000,
91                       int quote_it = 0) const;
92       private:
93         char * content;
94         int length;             // includes null byte (length=strlen()+1)
95 };
96
97 class _Tt_string : public _Tt_string_buf_ptr {
98       public:
99         _Tt_string();
100         _Tt_string(const _Tt_string_buf_ptr &s) : _Tt_string_buf_ptr(s) {};
101         _Tt_string(const _Tt_string &s);
102         _Tt_string(const char *s);
103         _Tt_string(const unsigned char *s, int len);
104         _Tt_string(int len);    // Used by cat/substring to make result cells
105         ~_Tt_string();
106         _Tt_string& operator=(const char *s);
107         _Tt_string& operator=(const _Tt_string_buf_ptr &s)
108                         {*(_Tt_string_buf_ptr *)this = s; return *this;}
109         _Tt_string& operator=(const _Tt_string &s)
110                         {*(_Tt_string_buf_ptr *)this = s; return *this;}
111         _Tt_string& set(const unsigned char*s, int len);
112         char& operator[](int offset);
113         const char& operator[](int offset) const;
114         //
115         // XXX char *() should not be const, but so much of UNIX takes char *
116         // instead of const char *
117         //
118         operator char *() const;
119         operator const char *() const;
120         _Tt_string left(int len) const;
121         _Tt_string right(int len) const;
122         _Tt_string mid(int offset, int len) const;
123         _Tt_string split(int index, _Tt_string &prefix) const;
124         _Tt_string split(char c, _Tt_string &prefix) const;
125         _Tt_string split(const char *s, _Tt_string &prefix) const;
126         _Tt_string rsplit(char c, _Tt_string &prefix) const;
127         _Tt_string quote_nulls() const;
128         _Tt_string unquote_nulls() const;
129         _Tt_string cat(const _Tt_string &s) const;
130         _Tt_string cat(char c) const;
131         _Tt_string cat(int i) const;
132         _Tt_string cat(unsigned int i) const;
133         _Tt_string cat(long i) const;
134         _Tt_string cat(unsigned long i) const;
135         _Tt_string replace(const char *old, const _Tt_string &with) const;
136         int len() const {
137                 return (*this)->length;
138         }
139         int operator==(const _Tt_string& s) const {
140                 return((*this)->length==s->length &&
141                        0==memcmp((*this)->content, s->content,
142                                  (*this)->length));
143         }
144         int operator==(const char *s) const {return 0==cmp(s);}
145         int operator<(const _Tt_string& s) const
146         {return -1==cmp(s->content, s->length);}
147         int operator<(const char *s) const {return -1==cmp(s);}
148         int operator>(const _Tt_string& s) const
149         {return +1==cmp(s->content, s->length);}
150         int operator>(const char *s) const {return +1==cmp(s);}
151         int operator!=(const _Tt_string &s) const {return !(*this==s);}
152         int operator!=(const char *s) const {return !(*this==s);}
153         int operator<=(const _Tt_string &s) const {return !(*this>s);}
154         int operator<=(const char *s) const {return !(*this>s);}
155         int operator>=(const _Tt_string &s) const {return !(*this<s);}
156         int operator>=(const char *s) const {return !(*this<s);}
157         int sh_match(const _Tt_string &pattern) const;
158 #ifdef _DEBUG_MALLOC_INC
159         int DBindex(char *fname, int lnum, char c) const;
160         int DBrindex(char *fname, int lnum, char c) const;
161 #else
162         int index(char c) const;
163         int index(const char *s) const;
164         int rindex(char c) const;
165         int strchr(char c) const;
166         int strchr(const char *s) const;
167         int strrchr(char c) const;
168 #endif
169         int hash(int max_buckets) const;
170         bool_t xdr(XDR *xdrs);
171         void print(void) const;
172         void print(const _Tt_ostream &os, int max_print_width = 80000,
173                    int quote_it = 0) const;
174         int cmp(const _Tt_string &s) const {return cmp(s->content, s->length);}
175         int cmp(const char *q, int qlen = -1) const;
176 };
177
178 // Since _Tt_string is "just a _Tt_string_buf_ptr" with some extra operations,
179 // a _Tt_string_list and associated classes are just the _Tt_string_buf_list
180 // class and associated classes.
181
182 void    _tt_string_print(const _Tt_ostream &os, const _Tt_object *obj);
183 class _Tt_string_list : public _Tt_string_buf_list {
184       public:
185         _Tt_string_list();
186         _Tt_string_list(const _Tt_string_list &l);
187         _Tt_string_list(const _Tt_string_buf_list &l);
188         ~_Tt_string_list();
189 };
190
191 class _Tt_string_list_ptr : public _Tt_string_buf_list_ptr {
192       public:
193         _Tt_string_list_ptr();
194         _Tt_string_list_ptr(_Tt_string_list *s) :
195           _Tt_string_buf_list_ptr((_Tt_string_buf_list *)s) {};
196         _Tt_string_list_ptr(const _Tt_string_list_ptr &p) :
197           _Tt_string_buf_list_ptr(p) {};
198         ~_Tt_string_list_ptr();
199         _Tt_string_list_ptr &operator=(_Tt_string_list *s);
200         _Tt_string_list_ptr &operator=(const _Tt_string_list_ptr &s);
201 };
202
203 class _Tt_string_list_cursor: public _Tt_string_buf_list_cursor {
204       public:
205         _Tt_string_list_cursor();
206         _Tt_string_list_cursor(const _Tt_string_list_cursor &c);
207         _Tt_string_list_cursor(const _Tt_string_list_ptr &l);
208         ~_Tt_string_list_cursor();
209         _Tt_string operator *() const;
210         _Tt_string operator ->() const;
211 };      
212         
213 int _tt_xdr_encode(xdrproc_t xdr_fn, void *data, _Tt_string &opaque_string);
214 int _tt_xdr_decode(xdrproc_t xdr_fn, void *data, _Tt_string opaque_string);
215 bool_t tt_xdr_string(XDR *xdrs, _Tt_string *s);
216 bool_t _tt_xdr_string_list(XDR *xdrs, _Tt_string_list_ptr *strings_ptr);
217 void _tt_print_escaped_string(const _Tt_ostream &os,
218                               const char *s, int length,
219                               int max_print_width = 8000,
220                               int quote_it = 0);
221
222 #include "util/tt_iostream.h"
223
224 #endif