Fix typo in license headers
[oweals/cde.git] / cde / programs / nsgmls / StringOf.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 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.C /main/1 1996/07/29 17:05:16 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef StringOf_DEF_INCLUDED
28 #define StringOf_DEF_INCLUDED 1
29
30 #include <string.h>
31 #include <stddef.h>
32
33 #ifdef SP_NAMESPACE
34 namespace SP_NAMESPACE {
35 #endif
36
37 template<class T>
38 String<T>::String(const T *ptr, size_t length)
39 : length_(length), alloc_(length)
40 {
41   if (length) {
42     ptr_ = new T[length];
43     memcpy(ptr_, ptr, length*sizeof(T));
44   }
45   else
46     ptr_ = 0;
47 }
48
49 template<class T>
50 String<T>::String()
51 : ptr_(0), length_(0), alloc_(0)
52 {
53 }
54
55 template<class T>
56 String<T>::String(const String<T> &s)
57 : length_(s.length_), alloc_(s.length_)
58 {
59   if (length_) {
60     ptr_ = new T[length_];
61     memcpy(ptr_, s.ptr_, length_*sizeof(T));
62   }
63   else
64     ptr_ = 0;
65 }
66
67 template<class T>
68 String<T> &String<T>::operator=(const String<T> &s)
69 {
70   if (&s != this) {
71     if (s.length_ > alloc_) {
72       if (ptr_)
73         delete [] ptr_;
74       ptr_ = new T[alloc_ = s.length_];
75     }
76     memcpy(ptr_, s.ptr_, s.length_*sizeof(T));
77     length_ = s.length_;
78   }
79   return *this;
80 }
81
82 template<class T>
83 String<T> &String<T>::insert(size_t i, const String<T> &s)
84 {
85   if (length_ + s.length_ > alloc_)
86     grow(s.length_);
87   for (size_t n = length_ - i; n > 0; n--)
88     ptr_[i + n - 1 + s.length_] = ptr_[i + n - 1];
89   length_ += s.length_;
90   memcpy(ptr_ + i, s.ptr_, s.length_*sizeof(T));
91   return *this;
92 }
93
94 template<class T>
95 String<T> &String<T>::append(const T *p, size_t length)
96 {
97   if (length_ + length > alloc_)
98     grow(length);
99   memcpy(ptr_ + length_, p, length*sizeof(T));
100   length_ += length;
101   return *this;
102 }
103
104 template<class T>
105 void String<T>::grow(size_t n)
106 {
107   if (alloc_ < n)
108     alloc_ += n + 16;
109   else
110     alloc_ += alloc_;
111   T *s = new T[alloc_];
112   memcpy(s, ptr_, length_*sizeof(T));
113   delete [] ptr_;
114   ptr_ = s;
115 }
116
117 template<class T>
118 void String<T>::swap(String<T> &to)
119 {
120   {
121     T *tem = to.ptr_;
122     to.ptr_ = ptr_;
123     ptr_ = tem;
124   }
125   {
126     size_t tem = to.length_;
127     to.length_ = length_;
128     length_ = tem;
129   }
130   {
131     size_t tem = to.alloc_;
132     to.alloc_ = alloc_;
133     alloc_ = tem;
134   }
135 }
136
137 template<class T>
138 String<T> &String<T>::assign(const T *p, size_t n)
139 {
140   if (alloc_ < n) {
141     if (ptr_)
142       delete [] ptr_;
143     ptr_ = new T[alloc_ = n];
144   }
145   length_ = n;
146   for(T *to = ptr_; n > 0; n--, to++, p++)
147     *to = *p;
148   return *this;
149 }
150
151 template<class T>
152 void String<T>::resize(size_t n)
153 {
154   if (alloc_ < n) {
155     T *oldPtr_ = ptr_;
156     ptr_ = new T[alloc_ = n];
157     if (length_ > 0) {
158       memcpy(ptr_, oldPtr_, length_*sizeof(T));
159       delete [] oldPtr_;
160     }
161   }
162   length_ = n;
163 }
164
165 #ifdef SP_NAMESPACE
166 }
167 #endif
168
169 #endif /* not StringOf_DEF_INCLUDED */