]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string-data.icc
599b9d82a632b01bd62a2e24d16f50eeeaa972de
[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--1998 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     {
67       delete data_byte_p_;
68       maxlen = j;
69       data_byte_p_ = new Byte[maxlen + 1];
70   
71       data_byte_p_[0] = 0;
72       length_i_ = 0;
73     }
74 }
75
76 /* this is all quite hairy:  
77          update of length_i_
78          update of maxlen
79          alloc of buffer
80          copying of buffer
81  needs blondification: 
82         split tasks
83         define change authority
84 */
85 INLINE void 
86 String_data::remax (int j) 
87 {
88   OKW();
89   if (j > maxlen) 
90     {
91       Byte *p = new Byte[j + 1];        
92       memcpy (p, data_byte_p_, (maxlen <? length_i_) + 1 );         
93       maxlen = j;
94       delete[] data_byte_p_;
95       data_byte_p_ = p;
96     }
97 }
98
99 INLINE void 
100 String_data::tighten() 
101 { // should be dec'd const
102   maxlen = length_i_;
103   Byte *p = new Byte[maxlen + 1];           
104   memcpy (p, data_byte_p_, length_i_ + 1);          
105   delete[] data_byte_p_;
106   data_byte_p_ = p;             
107 }
108 // assignment.
109 INLINE void 
110 String_data::set (Byte const* byte_C, int length_i) 
111 {
112   OKW();
113
114   assert (byte_C && byte_C != data_byte_p_);
115
116   length_i_ = length_i;
117   remax (length_i_);     // copies too
118   memcpy (data_byte_p_, byte_C, length_i_);
119   data_byte_p_[ length_i_ ] = 0;
120 }
121
122 INLINE
123 void 
124 String_data::set (char const* ch_C) 
125 {
126   set ((Byte const*)ch_C, strlen (ch_C) );
127 }
128
129
130 /// concatenation.
131 INLINE void 
132 String_data::append (Byte const* byte_C, int length_i) 
133 {
134   OK();
135   OKW();
136   int old_i = length_i_;
137   
138   length_i_ += length_i;
139   remax (length_i_);
140   memcpy (data_byte_p_ + old_i, byte_C, length_i);      
141   data_byte_p_[ length_i_ ] = 0;
142 }
143
144 INLINE
145 void 
146 String_data::operator += (char const* ch_C) 
147 {
148   append ((Byte const*)ch_C, strlen (ch_C) );
149 }
150
151
152
153 INLINE
154 char const*
155 String_data::ch_C() const
156 {
157   return (char const*)data_byte_p_; 
158 }
159 INLINE char* 
160 String_data::ch_l() 
161
162   return (char*)data_byte_p_; 
163 }
164
165 INLINE Byte const*
166 String_data::byte_C() const 
167
168   return data_byte_p_; 
169 }
170
171 INLINE Byte* 
172 String_data::byte_l() 
173 {
174   OKW();
175   return data_byte_p_;
176 }
177
178 INLINE
179 void 
180 String_data::trunc (int j) 
181 {
182   OKW(); 
183   assert (j >= 0 && j <= length_i_);
184   data_byte_p_[j] = 0;
185   length_i_ = j;
186 }
187
188 INLINE bool
189 String_data::is_binary_bo() const
190 {
191   //    return !memchr (data_byte_p_, length_i_, 0);
192   return ((int)strlen ((char const*)data_byte_p_) != length_i_ );
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 //