Remove Unixware and openserver support
[oweals/cde.git] / cde / programs / nsgmls / StringOf.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 /* $XConsortium: StringOf.h /main/1 1996/07/29 17:05:21 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef StringOf_INCLUDED
28 #define StringOf_INCLUDED 1
29
30 // The file is called StringOf to distinguish it from string.h on
31 // case-insensitive file systems.
32
33 // This offers a subset of the interface offered by the standard C++
34 // basic_string class as defined in the Jan 96 WP.
35 // Code in SP currently assumes that size_type is size_t.
36
37 #include <stddef.h>
38 #include <string.h>
39 #include "Boolean.h"
40
41 #ifdef SP_NAMESPACE
42 namespace SP_NAMESPACE {
43 #endif
44
45 template<class T>
46 class String {
47 public:
48   typedef size_t size_type;
49   typedef T *iterator;
50   typedef const T *const_iterator;
51   String();
52   ~String() { if (ptr_) delete [] ptr_; }
53   String(const T *, size_t);
54   String(const String<T> &);
55   String<T> &operator=(const String<T> &);
56   size_t size() const { return length_; }
57   String<T> &assign(const T *, size_t);
58   String<T> &insert(size_t i, const String<T> &s);
59   void swap(String<T> &str);
60   T operator[](size_t i) const { return ptr_[i]; }
61   T &operator[](size_t i) { return ptr_[i]; }
62   iterator begin() { return ptr_; }
63   const_iterator begin() const { return ptr_; }
64   const T *data() const { return ptr_; }
65   String<T> &operator+=(T c) {
66     if (length_ >= alloc_)
67       grow(1);
68     ptr_[length_++] = c;
69     return *this;
70   }
71   String<T> &operator+=(const String<T> &s) {
72     append(s.ptr_, s.length_);
73     return *this;
74   }
75   String<T> &append(const T *, size_t);
76   Boolean operator==(const String<T> &s) const {
77     return (length_ == s.length_
78             && (length_ == 0
79                 || (*ptr_ == *s.ptr_
80                     && (memcmp(ptr_ + 1, s.ptr_ + 1, (length_ - 1)*sizeof(T))
81                         == 0))));
82   }
83   Boolean operator!=(const String<T> &str) const {
84     return !(*this == str);
85   }
86   void resize(size_t n);
87 private:
88   void grow(size_t);
89   T *ptr_;
90   size_t length_;
91   size_t alloc_;
92 };
93
94 #ifdef SP_NAMESPACE
95 }
96 #endif
97
98 #endif /* not StringOf_INCLUDED */
99
100 #ifdef SP_DEFINE_TEMPLATES
101 #include "StringOf.C"
102 #endif