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