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