]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_integer.cc
Initial revision
[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 "cl_integer_io.h"
13 #include <string.h>
14 #include "cl_I.h"
15
16 void format_integer (cl_ostream stream, const cl_I& arg,
17         unsigned int base, sintL mincol, char padchar,
18         char commachar, uintL commainterval, cl_boolean commaflag,
19         cl_boolean positive_sign_flag)
20 {
21         if ((mincol == 0) && !commaflag && !positive_sign_flag) {
22                 // Normale Ausgabe tut's.
23                 print_integer(stream,base,arg);
24                 return;
25         }
26         var char* oldstring = print_integer_to_string(base,arg);
27         var uintL oldstring_length = strlen(oldstring);
28         var uintL number_of_digits = (minusp(arg) ? oldstring_length-1 : oldstring_length);
29         var uintL number_of_commas = (commaflag ? floor(number_of_digits-1,commainterval) : 0);
30         var cl_boolean positive_sign = (cl_boolean) (positive_sign_flag && (arg > 0));
31         var uintL newstring_length = (positive_sign ? 1 : 0) + oldstring_length + number_of_commas;
32         var char* newstring = (char *) cl_malloc_hook(newstring_length+1);
33         newstring[newstring_length] = '\0'; // newstring termination
34         // newstring füllen:
35         {
36                 // Erst Vorzeichen +:
37                 if (positive_sign)
38                         newstring[0] = '+';
39                 // Dann oldstring in newstring übertragen, dabei Kommata überspringen:
40                 var uintL oldpos = oldstring_length;
41                 var uintL oldpos_mod = 0; // = (oldstring_length - oldpos) % commainterval
42                 var uintL newpos = newstring_length;
43                 until (oldpos == 0) {
44                         newstring[--newpos] = oldstring[--oldpos];
45                         if (number_of_commas > 0) {
46                                 // Check whether ((oldstring_length - oldpos) % commainterval) == 0
47                                 if (++oldpos_mod == commainterval) {
48                                         oldpos_mod = 0;
49                                         // noch ein Komma einzufügen
50                                         newstring[--newpos] = commachar;
51                                         number_of_commas--;
52                                 }
53                         }
54                 }
55         }
56 #if 0
57         format_padded_string(stream,mincol,1,0,padchar,cl_true,newstring);
58 #else // expand this special case of format_padded_string inline:
59         if ((sintL)newstring_length < mincol)
60                 format_padding(stream,mincol-newstring_length,padchar);
61         fprint(stream,newstring);
62 #endif
63         cl_free_hook(newstring);
64         cl_free_hook(oldstring);
65 }