(print): new file. Set limits to
[lilypond.git] / flower / include / string-data.icc
blob00691cc57b16eb73fa395656efb7aa0df3c7bbd1
1 /* -*-C++-*-
2   String_data.inl -- implement String_data
4   source file of Flower lib
6   (c)  1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #ifndef STRINGDATA_INL
10 #define STRINGDATA_INL
12 #include <assert.h>
13 #include <memory.h>
14 #include <string.h>
16 #include "string-data.hh"
17 const int INITIALMAX=8;
19 #include <sys/types.h>
21 INLINE void 
22 String_data::OKW () 
24   assert (ref_count_ == 1);
27 INLINE void 
28 String_data::OK () 
30   assert (maxlen >= length_);
31   assert (bool (data_byte_));
32   assert (ref_count_ >= 1);
36 INLINE
37 String_data::String_data () 
39   ref_count_=0;
40   maxlen = INITIALMAX;
41   data_byte_ = new Byte[maxlen + 1];
42   data_byte_[0] = 0;
43   length_ = 0;
46 INLINE
47 String_data::String_data (String_data const &src) 
49   ref_count_=0; 
50   maxlen = length_ = src.length_;               
51   data_byte_ = new Byte[maxlen+1]; // should calc GNU 8byte overhead.   
52   memcpy (data_byte_, src.data_byte_, length_ + 1);     
55 INLINE
56 String_data::~String_data () 
58   assert (ref_count_ == 0);
59   delete[] data_byte_;
62 INLINE void 
63 String_data::setmax (int j) 
64 {       
65   OKW ();
66   if (j > maxlen) 
67     {
68       delete[] data_byte_;
69       maxlen = j;
70       data_byte_ = new Byte[maxlen + 1];
71   
72       data_byte_[0] = 0;
73       length_ = 0;
74     }
77 /* this is all quite hairy:  
78          update of length_
79          update of maxlen
80          alloc of buffer
81          copying of buffer
82  needs blondification: 
83         split tasks
84         define change authority
86 INLINE void 
87 String_data::remax (int j) 
89   OKW ();
90   if (j > maxlen) 
91     {
92       Byte *p = new Byte[j + 1];        
93       memcpy (p, data_byte_, (maxlen <? length_) + 1 );     
94       maxlen = j;
95       delete[] data_byte_;
96       data_byte_ = p;
97     }
100 INLINE void 
101 String_data::tighten () 
102 { // should be dec'd const
103   maxlen = length_;
104   Byte *p = new Byte[maxlen + 1];           
105   memcpy (p, data_byte_, length_ + 1);      
106   delete[] data_byte_;
107   data_byte_ = p;               
109 // assignment.
110 INLINE void 
111 String_data::set (Byte const* byte, int length_i) 
113   OKW ();
115   assert (byte && byte != data_byte_);
117   length_ = length_i;
118   remax (length_);     // copies too
119   memcpy (data_byte_, byte, length_);
120   data_byte_[ length_ ] = 0;
123 INLINE
124 void 
125 String_data::set (char const* str0) 
127   set ((Byte const*)str0, strlen (str0) );
131 /// concatenation.
132 INLINE void 
133 String_data::append (Byte const* byte, int length_i) 
135   OK ();
136   OKW ();
137   int old_i = length_;
138   
139   length_ += length_i;
140   remax (length_);
141   memcpy (data_byte_ + old_i, byte, length_i);  
142   data_byte_[ length_ ] = 0;
145 INLINE
146 void 
147 String_data::operator += (char const* str0) 
149   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]; 
213 #endif // __STRING_UTIL_CC //