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