* python/lilylib.py (setup_temp): temporary directories are mode 700.
[lilypond.git] / flower / string-convert.cc
blob74b6c23277a6aed9ff0a69c449974ded9678b237
1 /*
2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
5 --*/
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <limits.h>
11 #include "libc-extension.hh"
12 #include "string.hh"
13 #include "string-convert.hh"
14 #include "rational.hh"
15 #include "array.hh"
17 /**
18 A safe length for stringconversion buffers.
20 worst case would be %f printing HUGE (or 1/HUGE), which is approx
21 2e318, this number would have approx 318 zero's in its string.
23 Should enlarge buff dynamically.
25 @see
26 man 3 snprintf
28 static const int STRING_BUFFER_LEN=1024;
30 String
31 String_convert::bool_string (bool b)
33 return String (b ? "true" : "false");
36 String
37 String_convert::bin2hex (String bin_string)
39 String str;
40 Byte const* byte = bin_string.to_bytes ();
41 for (int i = 0; i < bin_string.length (); i++)
43 str += to_string ((char)nibble2hex_byte (*byte >> 4));
44 str += to_string ((char)nibble2hex_byte (*byte++));
46 return str;
49 int
50 String_convert::bin2int (String bin_string)
52 return bin2unsigned (bin_string);
55 unsigned
56 String_convert::bin2unsigned (String bin_string)
58 assert (bin_string.length () <= (int)sizeof (unsigned));
60 unsigned result_u = 0;
61 for (int i = 0; i < bin_string.length (); i++)
63 result_u <<= 8;
64 result_u += (Byte)bin_string[ i ];
66 return result_u;
69 // breendet imp from String
70 int
71 String_convert::dec2int (String dec_string)
73 if (!dec_string.length ())
74 return 0;
76 long l = 0;
77 int conv = sscanf (dec_string.to_str0 (), "%ld", &l);
78 assert (conv);
80 return (int)l;
83 String
84 String_convert::i64_string (I64 i64, char const* fmt)
86 char buffer[STRING_BUFFER_LEN];
87 snprintf (buffer, STRING_BUFFER_LEN,
88 (fmt ? fmt : "%Ld"), i64); // assume radix 10
89 return String (buffer);
92 // breendet imp from String
93 double
94 String_convert::dec2double (String dec_string)
96 if (!dec_string.length ())
97 return 0;
98 double d = 0;
99 int conv = sscanf (dec_string.to_str0 (), "%lf", &d);
100 assert (conv);
101 return d;
105 String_convert::hex2bin (String hex_string, String& bin_string_r)
107 if (hex_string.length () % 2)
108 hex_string = "0" + hex_string;
110 bin_string_r = "";
111 Byte const* byte= hex_string.to_bytes ();
112 int i = 0;
113 while (i < hex_string.length ())
115 int high_i = hex2nibble (*byte++);
116 int low_i = hex2nibble (*byte++);
117 if (high_i < 0 || low_i < 0)
118 return 1; // illegal char
119 bin_string_r += to_string ((char) (high_i << 4 | low_i), 1 );
120 i += 2;
122 return 0;
125 String
126 String_convert::hex2bin (String hex_string)
128 String str;
129 // silly, asserts should alway be "on"!
130 // assert (!hex2bin (hex_string, str) );
131 int error_i = hex2bin (hex_string, str);
132 assert (!error_i);
133 return str;
136 int
137 String_convert::hex2nibble (Byte byte)
139 if (byte >= '0' && byte <= '9')
140 return byte - '0';
141 if (byte >= 'A' && byte <= 'F')
142 return byte - 'A' + 10;
143 if (byte >= 'a' && byte <= 'f')
144 return byte - 'a' + 10;
145 return -1;
148 // stupido. Should use int_string ()
149 String
150 String_convert::int2dec (int i, int length_i, char ch)
152 char fill_char = ch;
153 if (fill_char)
154 fill_char = '0';
156 // ugh
157 String dec_string = to_string (i);
159 // ugh
160 return to_string (fill_char, length_i - dec_string.length ()) + dec_string;
164 // stupido. Should use int_string ()
165 String
166 String_convert::unsigned2hex (unsigned u, int length_i, char fill_char)
168 String str;
169 if (!u)
170 str = "0";
172 #if 1 // both go...
173 while (u)
175 str = to_string ((char) ((u % 16)["0123456789abcdef"] ) ) + str;
176 u /= 16;
178 #else
179 str += int_string (u, "%x"); // hmm. %lx vs. %x -> portability?
180 #endif
182 str = to_string (fill_char, length_i - str.length ()) + str;
183 while ((str.length () > length_i) && (str[ 0 ] == 'f' ) )
184 str = str.cut_string (2, INT_MAX);
186 return str;
189 String
190 String_convert::int2hex (int i, int length_i, char fill_char)
192 return unsigned2hex ((unsigned)i, length_i, fill_char);
195 Byte
196 String_convert::nibble2hex_byte (Byte byte)
198 if ((byte & 0x0f) <= 9 )
199 return (byte & 0x0f) + '0';
200 else
201 return (byte & 0x0f) - 10 + 'a';
204 Convert an integer to a string
206 @param
207 #fmt# is a printf style format, default assumes "%d" as format.
209 String
210 String_convert::int_string (int i, char const* fmt)
212 char buffer[STRING_BUFFER_LEN];
213 snprintf (buffer, STRING_BUFFER_LEN,
214 (fmt ? fmt : "%d"), i); // assume radix 10
215 return String (buffer);
218 String
219 String_convert::form_string (char const* format, ...)
221 va_list args;
222 va_start (args, format);
223 char buffer[STRING_BUFFER_LEN];
224 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
225 va_end (args);
226 return String (buffer);
229 String
230 String_convert::vform_string (char const* format, va_list args)
232 char buffer[STRING_BUFFER_LEN];
233 vsnprintf (buffer, STRING_BUFFER_LEN, format, args);
234 return String (buffer);
238 Convert a double to a string.
240 @param #fmt# is a printf style format, default assumes "%lf" as format
242 String
243 String_convert::double_string (double f, char const* fmt)
245 char buf[STRING_BUFFER_LEN];
247 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
248 return String (buf);
252 Make a string from a single character.
254 @param
255 #n# is a repetition count, default value is 1
257 String
258 String_convert::char_string (char c, int n)
260 n = n >= 0 ? n : 0;
261 char* ch = new char[ n ];
262 memset (ch, c, n);
263 String s ((Byte*)ch, n);
264 delete[] ch;
265 return s;
268 String
269 String_convert::rational_string (Rational r)
271 return r.to_string ();
274 String
275 String_convert::pointer_string (void const *l)
277 char buffer[STRING_BUFFER_LEN];
278 snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10
279 return String (buffer);
283 Convert a double to a string.
285 @param
286 #n# is the number of nonzero digits
288 String
289 String_convert::precision_string (double x, int n)
291 String format = "%." + to_string (0 >? n - 1) + "e";
292 String str = double_string (abs (x), format.to_str0 ());
294 int exp = str.right_string (3).to_int ();
295 str = str.left_string (str.length () - 4);
297 while (str[str.length () - 1] == '0')
298 str = str.left_string (str.length () - 1);
299 if (str[str.length () - 1] == '.')
300 str = str.left_string (str.length () - 1);
302 if (exp == 0)
303 return (sign (x) > 0 ? str : "-" + str);
305 str = str.left_string (1) + str.cut_string (2, INT_MAX);
306 int dot = 1 + exp;
307 if (dot <= 0)
308 str = "0." + to_string ('0', -dot) + str;
309 else if (dot >= str.length ())
310 str += to_string ('0', dot - str.length ());
311 else if (( dot > 0) && (dot < str.length ()))
312 str = str.left_string (dot) + "." + str.cut_string (dot, INT_MAX);
313 else
314 assert (0);
316 return (sign (x) > 0 ? str : "-" + str);
319 Array<String>
320 String_convert::split (String str, char c)
322 Array<String> a;
323 int i = str.index (c);
324 while (i >=0)
326 String s = str.left_string (i);
327 a.push (s);
328 while (str[++i] == c)
330 str = str.cut_string (i, INT_MAX);
331 i = str.index (c);
333 if (str.length ())
334 a.push (str);
335 return a;
339 String
340 String_convert::long_string (long l)
342 char s[STRING_BUFFER_LEN];
343 sprintf (s,"%ld", l);
344 return s;
347 String
348 String_convert::pad_to (String s, int n)
350 return s + to_string (' ' , (n - s.length ()) >? 0);