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