]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string-data.icc
00691cc57b16eb73fa395656efb7aa0df3c7bbd1
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #ifndef STRINGDATA_INL
10 #define STRINGDATA_INL
11
12 #include <assert.h>
13 #include <memory.h>
14 #include <string.h>
15
16 #include "string-data.hh"
17 const int INITIALMAX=8;
18
19 #include <sys/types.h>
20
21 INLINE void 
22 String_data::OKW () 
23 {
24   assert (ref_count_ == 1);
25 }
26
27 INLINE void 
28 String_data::OK () 
29 {
30   assert (maxlen >= length_);
31   assert (bool (data_byte_));
32   assert (ref_count_ >= 1);
33 }
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
131 /// concatenation.
132 INLINE void 
133 String_data::append (Byte const* byte, int length_i) 
134 {
135   OK ();
136   OKW ();
137   int old_i = length_;
138   
139   length_ += length_i;
140   remax (length_);
141   memcpy (data_byte_ + old_i, byte, length_i);  
142   data_byte_[ length_ ] = 0;
143 }
144
145 INLINE
146 void 
147 String_data::operator += (char const* str0) 
148 {
149   append ((Byte const*)str0, strlen (str0) );
150 }
151
152
153
154 INLINE
155 char const*
156 String_data::to_str0 () const
157 {
158   return (char const*)data_byte_; 
159 }
160 INLINE char* 
161 String_data::get_str0 () 
162
163   return (char*)data_byte_; 
164 }
165
166 INLINE Byte const*
167 String_data::to_bytes () const 
168
169   return data_byte_; 
170 }
171
172 INLINE Byte* 
173 String_data::get_bytes () 
174 {
175   OKW ();
176   return data_byte_;
177 }
178
179 INLINE
180 void 
181 String_data::trunc (int j) 
182 {
183   OKW (); 
184   assert (j >= 0 && j <= length_);
185   data_byte_[j] = 0;
186   length_ = j;
187 }
188
189 INLINE bool
190 String_data::is_binary_bo () const
191 {
192   //    return !memchr (data_byte_, length_, 0);
193   return ((int)strlen ((char const*)data_byte_) != length_ );
194 }
195
196 INLINE Byte&
197 String_data::operator [] (int j) 
198 {
199   assert (j >= 0 && j <= length_);
200   return data_byte_[j] ; 
201 }
202
203 INLINE Byte 
204 String_data::operator [] (int j) const 
205 {
206   assert (j >= 0 && j <= length_);
207   return data_byte_[j]; 
208 }
209
210
211
212
213 #endif // __STRING_UTIL_CC //