]> git.donarmstrong.com Git - lilypond.git/blob - flower/string-convert.cc
c105169cee55d179e8963dd9dbb95febefb08741
[lilypond.git] / flower / string-convert.cc
1 /*
2   PROJECT: FlowerSoft C++ library
3   FILE   : string-convert.cc
4
5 --*/
6
7
8 #include <assert.h>
9 #include <limits.h>
10 #include "libc-extension.hh"
11 #include "string.hh"
12 #include "string-convert.hh"
13
14 /**
15    a safe length for stringconversion buffers
16
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.
19
20    Should enlarge buff dynamically.
21    
22    @see
23    man 3 snprintf
24    */
25 static const int STRING_BUFFER_LEN=1024;
26
27 String
28 String_convert::bin2hex_str (String bin_str)
29 {
30   String str;
31   Byte const* byte_C = bin_str.byte_C();
32   for ( int i = 0; i < bin_str.length_i(); i++) 
33     {
34         str += (char)nibble2hex_byte (*byte_C >> 4);
35         str += (char)nibble2hex_byte (*byte_C++);
36     }
37   return str;
38 }
39
40 int
41 String_convert::bin2_i (String bin_str)
42 {
43   assert (bin_str.length_i() <= 4);
44
45   int result_i = 0;
46   for ( int i = 0; i < bin_str.length_i(); i++) 
47     {
48         result_i <<= 8;
49         result_i += (Byte)bin_str[ i ];
50     }
51   return result_i;
52 }
53
54 // breendet imp from String
55 int
56 String_convert::dec2_i (String dec_str)
57 {
58   if ( !dec_str.length_i())
59         return 0;
60
61   long l = 0;
62   int conv = sscanf (dec_str.ch_C(), "%ld", &l);
63   assert (conv);
64
65   return (int)l;
66 }
67
68 String
69 String_convert::i64_str (I64 i64, char const* fmt)
70 {
71   char buffer[STRING_BUFFER_LEN];
72   snprintf (buffer, STRING_BUFFER_LEN,
73              (fmt ? fmt : "%Ld"), i64);     // assume radix 10
74   return String (buffer);
75
76 }
77 // breendet imp from String
78 double
79 String_convert::dec2_f (String dec_str)
80 {
81   if ( !dec_str.length_i())
82         return 0;
83   double d = 0;
84   int conv = sscanf (dec_str.ch_C(), "%lf", &d);
85   assert (conv);
86   return d;
87 }
88
89 int
90 String_convert::hex2bin_i (String hex_str, String& bin_str_r)
91 {
92   if ( hex_str.length_i() % 2)
93       hex_str = "0" + hex_str;
94
95   bin_str_r = "";
96   Byte const* byte_C= hex_str.byte_C();
97   int i = 0;
98   while ( i < hex_str.length_i()) 
99     {
100       int high_i = hex2nibble_i (*byte_C++);
101       int low_i = hex2nibble_i (*byte_C++);
102       if ( high_i < 0 || low_i < 0)
103           return 1; // illegal char
104       bin_str_r += String ((char)( high_i << 4 | low_i), 1 );
105       i += 2;
106     }
107   return 0;
108 }
109
110 String 
111 String_convert::hex2bin_str (String hex_str)
112 {
113   String str;
114 //  silly, asserts should alway be "on"!
115 //    assert (!hex2bin_i (hex_str, str) );
116   int error_i = hex2bin_i (hex_str, str);
117   assert (!error_i);
118   return str;
119 }
120
121 int 
122 String_convert::hex2nibble_i (Byte byte)
123 {
124   if ( byte >= '0' && byte <= '9')
125       return byte - '0';
126   if ( byte >= 'A' && byte <= 'F')
127       return byte - 'A' + 10;
128   if ( byte >= 'a' && byte <= 'f')
129       return byte - 'a' + 10;
130   return -1;
131 }
132
133 // stupido.  Should use int_str()
134 String 
135 String_convert::i2dec_str (int i, int length_i, char ch)
136 {
137   char fill_ch = ch;
138   if ( fill_ch)
139       fill_ch = '0';
140
141   // ugh
142   String dec_str (i);
143   
144   // ugh
145   return String (fill_ch, length_i - dec_str.length_i()) + dec_str;
146 }
147
148
149 // stupido.  Should use int_str()
150 String 
151 String_convert::u2hex_str (unsigned u, int length_i, char fill_ch)
152 {
153   String str;
154   if ( !u)
155         str = "0";
156
157 #if 1 // both go...
158   while ( u) 
159     {
160         str = String ((char)( ( u % 16)["0123456789abcdef"] ) ) + str;
161         u /= 16;
162     }
163 #else
164   str += int_str (u, "%x");     // hmm. %lx vs. %x -> portability?
165 #endif
166
167   str = String (fill_ch, length_i - str.length_i()) + str;
168   while ( ( str.length_i() > length_i) &&  ( str[ 0 ] == 'f' ) )
169         str = str.mid_str (2, INT_MAX);
170
171   return str;
172 }
173
174 String 
175 String_convert::i2hex_str (int i, int length_i, char fill_ch)
176 {
177   return u2hex_str ((unsigned)i, length_i, fill_ch);
178 }
179
180 Byte
181 String_convert::nibble2hex_byte (Byte byte)
182 {
183   if ( ( byte & 0x0f) <= 9 )
184         return ( byte & 0x0f) + '0';
185   else
186         return ( byte & 0x0f) - 10 + 'a';
187 }
188 /**
189   Convert an integer to a string
190
191   @param
192   #fmt# is a printf style format, default assumes "%d" as format. 
193   */
194 String
195 String_convert::int_str (int i, char const* fmt)
196 {
197   char buffer[STRING_BUFFER_LEN];
198   snprintf (buffer, STRING_BUFFER_LEN,
199              (fmt ? fmt : "%d"), i);     // assume radix 10
200   return String (buffer);
201 }
202
203 /**
204   Convert a double to a string.
205
206   @param #fmt# is a printf style format, default assumes "%lf" as format
207  */
208 String
209 String_convert::double_str (double f, char const* fmt)
210 {
211   char buf[STRING_BUFFER_LEN]; 
212
213   snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
214   return String (buf);
215 }
216
217 /**
218   Make a string from a single character.
219
220   @param
221   #n# is a repetition count, default value is 1
222  */
223 String
224 String_convert::char_str (char c, int n)
225 {
226   n = n >= 0 ? n : 0;
227   char* ch_p = new char[ n ];
228   memset (ch_p, c, n);
229   String s ((Byte*)ch_p, n);
230   delete ch_p;
231   return s;
232 }
233
234 String
235 String_convert::rational_str (Rational r)
236 {
237   char * n = Itoa (r.numerator()); // LEAK????
238   
239   String s = n;
240   if (r.denominator() != 1) 
241     {
242         char * d = Itoa (r.denominator());
243         s +=  String ('/') + String (d);
244         //delete d;
245     }
246 /*    delete n;
247   */
248   return s;
249 }
250
251 String
252 String_convert::pointer_str (void const *l)
253 {
254   char buffer[STRING_BUFFER_LEN];
255   snprintf (buffer, STRING_BUFFER_LEN, "%p", l);     // assume radix 10
256   return String (buffer);
257 }