lilypond-0.1.12
[lilypond.git] / flower / string-convert.cc
blob5acdb4c88f55003a16702b66771773275df37b36
1 /*
2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
5 --*/
8 #include <assert.h>
9 #include <limits.h>
10 #include "libc-extension.hh"
11 #include "string.hh"
12 #include "string-convert.hh"
14 /**
15 a safe length for stringconversion buffers
17 worst case would be %f printing HUGE (or 1/HUGE), which is approx
18 2e318, this number would have approx 318 zero's in its string.
20 Should enlarge buff dynamically.
22 @see
23 man 3 snprintf
25 static const int STRING_BUFFER_LEN=1024;
27 String
28 String_convert::bin2hex_str (String bin_str)
30 String str;
31 Byte const* byte_C = bin_str.byte_C();
32 for ( int i = 0; i < bin_str.length_i(); i++)
34 str += (char)nibble2hex_byte (*byte_C >> 4);
35 str += (char)nibble2hex_byte (*byte_C++);
37 return str;
40 int
41 String_convert::bin2_i (String bin_str)
43 return bin2_u (bin_str);
46 unsigned
47 String_convert::bin2_u (String bin_str)
49 assert (bin_str.length_i() <= (int)sizeof(unsigned));
51 unsigned result_u = 0;
52 for ( int i = 0; i < bin_str.length_i(); i++)
54 result_u <<= 8;
55 result_u += (Byte)bin_str[ i ];
57 return result_u;
60 // breendet imp from String
61 int
62 String_convert::dec2_i (String dec_str)
64 if ( !dec_str.length_i())
65 return 0;
67 long l = 0;
68 int conv = sscanf (dec_str.ch_C(), "%ld", &l);
69 assert (conv);
71 return (int)l;
74 String
75 String_convert::i64_str (I64 i64, char const* fmt)
77 char buffer[STRING_BUFFER_LEN];
78 snprintf (buffer, STRING_BUFFER_LEN,
79 (fmt ? fmt : "%Ld"), i64); // assume radix 10
80 return String (buffer);
83 // breendet imp from String
84 double
85 String_convert::dec2_f (String dec_str)
87 if ( !dec_str.length_i())
88 return 0;
89 double d = 0;
90 int conv = sscanf (dec_str.ch_C(), "%lf", &d);
91 assert (conv);
92 return d;
95 int
96 String_convert::hex2bin_i (String hex_str, String& bin_str_r)
98 if ( hex_str.length_i() % 2)
99 hex_str = "0" + hex_str;
101 bin_str_r = "";
102 Byte const* byte_C= hex_str.byte_C();
103 int i = 0;
104 while ( i < hex_str.length_i())
106 int high_i = hex2nibble_i (*byte_C++);
107 int low_i = hex2nibble_i (*byte_C++);
108 if ( high_i < 0 || low_i < 0)
109 return 1; // illegal char
110 bin_str_r += String ((char)( high_i << 4 | low_i), 1 );
111 i += 2;
113 return 0;
116 String
117 String_convert::hex2bin_str (String hex_str)
119 String str;
120 // silly, asserts should alway be "on"!
121 // assert (!hex2bin_i (hex_str, str) );
122 int error_i = hex2bin_i (hex_str, str);
123 assert (!error_i);
124 return str;
127 int
128 String_convert::hex2nibble_i (Byte byte)
130 if ( byte >= '0' && byte <= '9')
131 return byte - '0';
132 if ( byte >= 'A' && byte <= 'F')
133 return byte - 'A' + 10;
134 if ( byte >= 'a' && byte <= 'f')
135 return byte - 'a' + 10;
136 return -1;
139 // stupido. Should use int_str()
140 String
141 String_convert::i2dec_str (int i, int length_i, char ch)
143 char fill_ch = ch;
144 if ( fill_ch)
145 fill_ch = '0';
147 // ugh
148 String dec_str (i);
150 // ugh
151 return String (fill_ch, length_i - dec_str.length_i()) + dec_str;
155 // stupido. Should use int_str()
156 String
157 String_convert::u2hex_str (unsigned u, int length_i, char fill_ch)
159 String str;
160 if ( !u)
161 str = "0";
163 #if 1 // both go...
164 while ( u)
166 str = String ((char)( ( u % 16)["0123456789abcdef"] ) ) + str;
167 u /= 16;
169 #else
170 str += int_str (u, "%x"); // hmm. %lx vs. %x -> portability?
171 #endif
173 str = String (fill_ch, length_i - str.length_i()) + str;
174 while ( ( str.length_i() > length_i) && ( str[ 0 ] == 'f' ) )
175 str = str.mid_str (2, INT_MAX);
177 return str;
180 String
181 String_convert::i2hex_str (int i, int length_i, char fill_ch)
183 return u2hex_str ((unsigned)i, length_i, fill_ch);
186 Byte
187 String_convert::nibble2hex_byte (Byte byte)
189 if ( ( byte & 0x0f) <= 9 )
190 return ( byte & 0x0f) + '0';
191 else
192 return ( byte & 0x0f) - 10 + 'a';
195 Convert an integer to a string
197 @param
198 #fmt# is a printf style format, default assumes "%d" as format.
200 String
201 String_convert::int_str (int i, char const* fmt)
203 char buffer[STRING_BUFFER_LEN];
204 snprintf (buffer, STRING_BUFFER_LEN,
205 (fmt ? fmt : "%d"), i); // assume radix 10
206 return String (buffer);
210 Convert a double to a string.
212 @param #fmt# is a printf style format, default assumes "%lf" as format
214 String
215 String_convert::double_str (double f, char const* fmt)
217 char buf[STRING_BUFFER_LEN];
219 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
220 return String (buf);
224 Make a string from a single character.
226 @param
227 #n# is a repetition count, default value is 1
229 String
230 String_convert::char_str (char c, int n)
232 n = n >= 0 ? n : 0;
233 char* ch_p = new char[ n ];
234 memset (ch_p, c, n);
235 String s ((Byte*)ch_p, n);
236 delete ch_p;
237 return s;
240 String
241 String_convert::rational_str (Rational r)
243 char * n = Itoa (r.numerator()); // LEAK????
245 String s = n;
246 if (r.denominator() != 1)
248 char * d = Itoa (r.denominator());
249 s += String ('/') + String (d);
250 //delete d;
252 /* delete n;
254 return s;
257 String
258 String_convert::pointer_str (void const *l)
260 char buffer[STRING_BUFFER_LEN];
261 snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10
262 return String (buffer);