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