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