]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_integer.cc
bump library version, since CLN doesn't export global object ctors any more.
[cln.git] / src / real / format-output / cl_fmt_integer.cc
1 // format_integer().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_format.h"
8
9
10 // Implementation.
11
12 #include "cln/integer_io.h"
13 #include <cstring>
14 #include "cl_I.h"
15
16 namespace cln {
17
18 void format_integer (std::ostream& stream, const cl_I& arg,
19         unsigned int base, sintL mincol, char padchar,
20         char commachar, uintL commainterval, bool commaflag,
21         bool positive_sign_flag)
22 {
23         if ((mincol == 0) && !commaflag && !positive_sign_flag) {
24                 // Normale Ausgabe tut's.
25                 print_integer(stream,base,arg);
26                 return;
27         }
28         var char* oldstring = print_integer_to_string(base,arg);
29         var uintL oldstring_length = ::strlen(oldstring);
30         var uintL number_of_digits = (minusp(arg) ? oldstring_length-1 : oldstring_length);
31         var uintL number_of_commas = (commaflag ? floor(number_of_digits-1,commainterval) : 0);
32         var bool positive_sign = positive_sign_flag && (arg > 0);
33         var uintL newstring_length = (positive_sign ? 1 : 0) + oldstring_length + number_of_commas;
34         var char* newstring = (char *) malloc_hook(newstring_length+1);
35         newstring[newstring_length] = '\0'; // newstring termination
36         // newstring füllen:
37         {
38                 // Erst Vorzeichen +:
39                 if (positive_sign)
40                         newstring[0] = '+';
41                 // Dann oldstring in newstring übertragen, dabei Kommata überspringen:
42                 var uintL oldpos = oldstring_length;
43                 var uintL oldpos_mod = 0; // = (oldstring_length - oldpos) % commainterval
44                 var uintL newpos = newstring_length;
45                 until (oldpos == 0) {
46                         newstring[--newpos] = oldstring[--oldpos];
47                         if (number_of_commas > 0) {
48                                 // Check whether ((oldstring_length - oldpos) % commainterval) == 0
49                                 if (++oldpos_mod == commainterval) {
50                                         oldpos_mod = 0;
51                                         // noch ein Komma einzufügen
52                                         newstring[--newpos] = commachar;
53                                         number_of_commas--;
54                                 }
55                         }
56                 }
57         }
58 #if 0
59         format_padded_string(stream,mincol,1,0,padchar,true,newstring);
60 #else // expand this special case of format_padded_string inline:
61         if ((sintL)newstring_length < mincol)
62                 format_padding(stream,mincol-newstring_length,padchar);
63         fprint(stream,newstring);
64 #endif
65         free_hook(newstring);
66         free_hook(oldstring);
67 }
68
69 }  // namespace cln