]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string-data.icc
* The grand 2005-2006 replace.
[lilypond.git] / flower / include / string-data.icc
1 /* -*-C++-*-
2    String_data.inl -- implement String_data
3
4    source file of Flower lib
5
6    (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #ifndef STRINGDATA_INL
10 #define STRINGDATA_INL
11
12 #include "string-data.hh"
13
14 #include <algorithm>
15 #include <cassert>
16 #include <cstring>
17 #include <memory>
18 using namespace std;
19
20 const int INITIALMAX = 8;
21
22 #include <sys/types.h>
23
24 INLINE void
25 String_data::OKW ()
26 {
27   assert (ref_count_ == 1);
28 }
29
30 INLINE void
31 String_data::OK ()
32 {
33   assert (maxlen >= length_);
34   assert (bool (data_byte_));
35   assert (ref_count_ >= 1);
36 }
37
38 INLINE
39 String_data::String_data ()
40 {
41   ref_count_ = 0;
42   maxlen = INITIALMAX;
43   data_byte_ = new Byte[maxlen + 1];
44   data_byte_[0] = 0;
45   length_ = 0;
46 }
47
48 INLINE
49 String_data::String_data (String_data const &src)
50 {
51   ref_count_ = 0;
52   maxlen = length_ = src.length_;
53   data_byte_ = new Byte[maxlen + 1]; // should calc GNU 8byte overhead.         
54   memcpy (data_byte_, src.data_byte_, length_ + 1);
55 }
56
57 INLINE
58 String_data::~String_data ()
59 {
60   assert (ref_count_ == 0);
61   delete[] data_byte_;
62 }
63
64 INLINE void
65 String_data::setmax (int j)
66 {
67   OKW ();
68   if (j > maxlen)
69     {
70       delete[] data_byte_;
71       maxlen = j;
72       data_byte_ = new Byte[maxlen + 1];
73
74       data_byte_[0] = 0;
75       length_ = 0;
76     }
77 }
78
79 /* this is all quite hairy:
80    update of length_
81    update of maxlen
82    alloc of buffer
83    copying of buffer
84    needs blondification:
85    split tasks
86    define change authority
87 */
88 INLINE void
89 String_data::remax (int j)
90 {
91   OKW ();
92   if (j > maxlen)
93     {
94       Byte *p = new Byte[j + 1];
95       memcpy (p, data_byte_, min (maxlen, length_) + 1);
96       maxlen = j;
97       delete[] data_byte_;
98       data_byte_ = p;
99     }
100 }
101
102 INLINE void
103 String_data::tighten ()
104 { // should be dec'd const
105   maxlen = length_;
106   Byte *p = new Byte[maxlen + 1];
107   memcpy (p, data_byte_, length_ + 1);
108   delete[] data_byte_;
109   data_byte_ = p;
110 }
111 // assignment.
112 INLINE void
113 String_data::set (Byte const *byte, int length_i)
114 {
115   OKW ();
116
117   assert (byte && byte != data_byte_);
118
119   length_ = length_i;
120   remax (length_); // copies too
121   memcpy (data_byte_, byte, length_);
122   data_byte_[ length_ ] = 0;
123 }
124
125 INLINE
126 void
127 String_data::set (char const *str0)
128 {
129   set ((Byte const *)str0, strlen (str0));
130 }
131
132 /// concatenation.
133 INLINE void
134 String_data::append (Byte const *byte, int length_i)
135 {
136   OK ();
137   OKW ();
138   int old_i = length_;
139
140   length_ += length_i;
141   remax (length_);
142   memcpy (data_byte_ + old_i, byte, length_i);
143   data_byte_[ length_ ] = 0;
144 }
145
146 INLINE
147 void
148 String_data::operator += (char const *str0)
149 {
150   append ((Byte const *)str0, strlen (str0));
151 }
152
153 INLINE
154 char const *
155 String_data::to_str0 () const
156 {
157   return (char const *)data_byte_;
158 }
159 INLINE char *
160 String_data::get_str0 ()
161 {
162   return (char *)data_byte_;
163 }
164
165 INLINE Byte const *
166 String_data::to_bytes () const
167 {
168   return data_byte_;
169 }
170
171 INLINE Byte *
172 String_data::get_bytes ()
173 {
174   OKW ();
175   return data_byte_;
176 }
177
178 INLINE
179 void
180 String_data::trunc (int j)
181 {
182   OKW ();
183   assert (j >= 0 && j <= length_);
184   data_byte_[j] = 0;
185   length_ = j;
186 }
187
188 INLINE bool
189 String_data::is_binary_bo () const
190 {
191   //    return !memchr (data_byte_, length_, 0);
192   return ((int)strlen ((char const *)data_byte_) != length_);
193 }
194
195 INLINE Byte &
196 String_data::operator [] (int j)
197 {
198   assert (j >= 0 && j <= length_);
199   return data_byte_[j];
200 }
201
202 INLINE Byte
203 String_data::operator [] (int j) const
204 {
205   assert (j >= 0 && j <= length_);
206   return data_byte_[j];
207 }
208
209 #endif // __STRING_UTIL_CC //