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