*** empty log message ***
[lilypond/patrick.git] / flower / include / string-data.icc
bloba87354cac539cc56a79a5c5a0a7a59325fb57d65
1 /* -*-C++-*-
2    String_data.inl -- implement String_data
4    source file of Flower lib
6    (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #ifndef STRINGDATA_INL
10 #define STRINGDATA_INL
12 #include "string-data.hh"
14 #include <algorithm>
15 #include <cassert>
16 #include <cstring>
17 #include <memory.h>
19 using namespace std;
21 const int INITIALMAX = 8;
23 #include <sys/types.h>
25 INLINE void
26 String_data::OKW ()
28   assert (ref_count_ == 1);
31 INLINE void
32 String_data::OK ()
34   assert (maxlen >= length_);
35   assert (bool (data_byte_));
36   assert (ref_count_ >= 1);
39 INLINE
40 String_data::String_data ()
42   ref_count_ = 0;
43   maxlen = INITIALMAX;
44   data_byte_ = new Byte[maxlen + 1];
45   data_byte_[0] = 0;
46   length_ = 0;
49 INLINE
50 String_data::String_data (String_data const &src)
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);
58 INLINE
59 String_data::~String_data ()
61   assert (ref_count_ == 0);
62   delete[] data_byte_;
65 INLINE void
66 String_data::setmax (int j)
68   OKW ();
69   if (j > maxlen)
70     {
71       delete[] data_byte_;
72       maxlen = j;
73       data_byte_ = new Byte[maxlen + 1];
75       data_byte_[0] = 0;
76       length_ = 0;
77     }
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
89 INLINE void
90 String_data::remax (int j)
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     }
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;
112 // assignment.
113 INLINE void
114 String_data::set (Byte const *byte, int length_i)
116   OKW ();
118   assert (byte && byte != data_byte_);
120   length_ = length_i;
121   remax (length_); // copies too
122   memcpy (data_byte_, byte, length_);
123   data_byte_[ length_ ] = 0;
126 INLINE
127 void
128 String_data::set (char const *str0)
130   set ((Byte const *)str0, strlen (str0));
133 /// concatenation.
134 INLINE void
135 String_data::append (Byte const *byte, int length_i)
137   OK ();
138   OKW ();
139   int old_i = length_;
141   length_ += length_i;
142   remax (length_);
143   memcpy (data_byte_ + old_i, byte, length_i);
144   data_byte_[ length_ ] = 0;
147 INLINE
148 void
149 String_data::operator += (char const *str0)
151   append ((Byte const *)str0, strlen (str0));
154 INLINE
155 char const *
156 String_data::to_str0 () const
158   return (char const *)data_byte_;
160 INLINE char *
161 String_data::get_str0 ()
163   return (char *)data_byte_;
166 INLINE Byte const *
167 String_data::to_bytes () const
169   return data_byte_;
172 INLINE Byte *
173 String_data::get_bytes ()
175   OKW ();
176   return data_byte_;
179 INLINE
180 void
181 String_data::trunc (int j)
183   OKW ();
184   assert (j >= 0 && j <= length_);
185   data_byte_[j] = 0;
186   length_ = j;
189 INLINE bool
190 String_data::is_binary_bo () const
192   //    return !memchr (data_byte_, length_, 0);
193   return ((int)strlen ((char const *)data_byte_) != length_);
196 INLINE Byte &
197 String_data::operator [] (int j)
199   assert (j >= 0 && j <= length_);
200   return data_byte_[j];
203 INLINE Byte
204 String_data::operator [] (int j) const
206   assert (j >= 0 && j <= length_);
207   return data_byte_[j];
210 #endif // __STRING_UTIL_CC //