]> 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--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
37 INLINE
38 String_data::String_data () 
39 {
40   ref_count_=0;
41   maxlen = INITIALMAX;
42   data_byte_ = new Byte[maxlen + 1];
43   data_byte_[0] = 0;
44   length_ = 0;
45 }
46
47 INLINE
48 String_data::String_data (String_data const &src) 
49 {
50   ref_count_=0; 
51   maxlen = length_ = src.length_;               
52   data_byte_ = new Byte[maxlen+1]; // should calc GNU 8byte overhead.   
53   memcpy (data_byte_, src.data_byte_, length_ + 1);     
54 }
55
56 INLINE
57 String_data::~String_data () 
58 {
59   assert (ref_count_ == 0);
60   delete[] data_byte_;
61 }
62
63 INLINE void 
64 String_data::setmax (int j) 
65 {       
66   OKW ();
67   if (j > maxlen) 
68     {
69       delete[] data_byte_;
70       maxlen = j;
71       data_byte_ = new Byte[maxlen + 1];
72   
73       data_byte_[0] = 0;
74       length_ = 0;
75     }
76 }
77
78 /* this is all quite hairy:  
79          update of length_
80          update of maxlen
81          alloc of buffer
82          copying of buffer
83  needs blondification: 
84         split tasks
85         define change authority
86 */
87 INLINE void 
88 String_data::remax (int j) 
89 {
90   OKW ();
91   if (j > maxlen) 
92     {
93       Byte *p = new Byte[j + 1];        
94       memcpy (p, data_byte_, (maxlen <? length_) + 1 );     
95       maxlen = j;
96       delete[] data_byte_;
97       data_byte_ = p;
98     }
99 }
100
101 INLINE void 
102 String_data::tighten () 
103 { // should be dec'd const
104   maxlen = length_;
105   Byte *p = new Byte[maxlen + 1];           
106   memcpy (p, data_byte_, length_ + 1);      
107   delete[] data_byte_;
108   data_byte_ = p;               
109 }
110 // assignment.
111 INLINE void 
112 String_data::set (Byte const* byte, int length_i) 
113 {
114   OKW ();
115
116   assert (byte && byte != data_byte_);
117
118   length_ = length_i;
119   remax (length_);     // copies too
120   memcpy (data_byte_, byte, length_);
121   data_byte_[ length_ ] = 0;
122 }
123
124 INLINE
125 void 
126 String_data::set (char const* str0) 
127 {
128   set ((Byte const*)str0, strlen (str0) );
129 }
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
154
155 INLINE
156 char const*
157 String_data::to_str0 () const
158 {
159   return (char const*)data_byte_; 
160 }
161 INLINE char* 
162 String_data::get_str0 () 
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
214 #endif // __STRING_UTIL_CC //