osfuns.cc revision 1.1.1.2 1 1.1 mrg /* Support for operator<< routines.
2 1.1 mrg
3 1.1 mrg THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 1.1 mrg CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 1.1 mrg FUTURE GNU MP RELEASES.
6 1.1 mrg
7 1.1 mrg Copyright 2001 Free Software Foundation, Inc.
8 1.1 mrg
9 1.1 mrg This file is part of the GNU MP Library.
10 1.1 mrg
11 1.1 mrg The GNU MP Library is free software; you can redistribute it and/or modify
12 1.1.1.2 mrg it under the terms of either:
13 1.1.1.2 mrg
14 1.1.1.2 mrg * the GNU Lesser General Public License as published by the Free
15 1.1.1.2 mrg Software Foundation; either version 3 of the License, or (at your
16 1.1.1.2 mrg option) any later version.
17 1.1.1.2 mrg
18 1.1.1.2 mrg or
19 1.1.1.2 mrg
20 1.1.1.2 mrg * the GNU General Public License as published by the Free Software
21 1.1.1.2 mrg Foundation; either version 2 of the License, or (at your option) any
22 1.1.1.2 mrg later version.
23 1.1.1.2 mrg
24 1.1.1.2 mrg or both in parallel, as here.
25 1.1 mrg
26 1.1 mrg The GNU MP Library is distributed in the hope that it will be useful, but
27 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 1.1.1.2 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 1.1.1.2 mrg for more details.
30 1.1 mrg
31 1.1.1.2 mrg You should have received copies of the GNU General Public License and the
32 1.1.1.2 mrg GNU Lesser General Public License along with the GNU MP Library. If not,
33 1.1.1.2 mrg see https://www.gnu.org/licenses/. */
34 1.1 mrg
35 1.1 mrg #include <iostream>
36 1.1 mrg #include <stdarg.h> /* for va_list and hence doprnt_funs_t */
37 1.1 mrg #include <string.h>
38 1.1 mrg
39 1.1 mrg #include "gmp.h"
40 1.1 mrg #include "gmp-impl.h"
41 1.1 mrg
42 1.1 mrg using namespace std;
43 1.1 mrg
44 1.1 mrg
45 1.1 mrg /* Don't need "format" for operator<< routines, just "memory" and "reps".
46 1.1 mrg Omitting gmp_asprintf_format lets us avoid dragging vsnprintf into the
47 1.1 mrg link. __gmp_asprintf_final will be called directly and doesn't need to
48 1.1 mrg be in the struct. */
49 1.1 mrg
50 1.1 mrg const struct doprnt_funs_t __gmp_asprintf_funs_noformat = {
51 1.1 mrg NULL,
52 1.1 mrg (doprnt_memory_t) __gmp_asprintf_memory,
53 1.1 mrg (doprnt_reps_t) __gmp_asprintf_reps,
54 1.1 mrg NULL
55 1.1 mrg };
56 1.1 mrg
57 1.1 mrg
58 1.1 mrg void
59 1.1 mrg __gmp_doprnt_params_from_ios (struct doprnt_params_t *p, ios &o)
60 1.1 mrg {
61 1.1 mrg if ((o.flags() & ios::basefield) == ios::hex)
62 1.1 mrg {
63 1.1 mrg p->expfmt = "@%c%02d";
64 1.1 mrg p->base = (o.flags() & ios::uppercase ? -16 : 16);
65 1.1 mrg }
66 1.1 mrg else
67 1.1 mrg {
68 1.1 mrg p->expfmt = (o.flags() & ios::uppercase ? "E%c%02d" : "e%c%02d");
69 1.1 mrg if ((o.flags() & ios::basefield) == ios::oct)
70 1.1 mrg p->base = 8;
71 1.1 mrg else
72 1.1 mrg p->base = 10;
73 1.1 mrg }
74 1.1 mrg
75 1.1 mrg /* "general" if none or more than one bit set */
76 1.1 mrg if ((o.flags() & ios::floatfield) == ios::fixed)
77 1.1 mrg p->conv = DOPRNT_CONV_FIXED;
78 1.1 mrg else if ((o.flags() & ios::floatfield) == ios::scientific)
79 1.1 mrg p->conv = DOPRNT_CONV_SCIENTIFIC;
80 1.1 mrg else
81 1.1 mrg p->conv = DOPRNT_CONV_GENERAL;
82 1.1 mrg
83 1.1 mrg p->exptimes4 = 0;
84 1.1 mrg
85 1.1 mrg p->fill = o.fill();
86 1.1 mrg
87 1.1 mrg /* "right" if more than one bit set */
88 1.1 mrg if ((o.flags() & ios::adjustfield) == ios::left)
89 1.1 mrg p->justify = DOPRNT_JUSTIFY_LEFT;
90 1.1 mrg else if ((o.flags() & ios::adjustfield) == ios::internal)
91 1.1 mrg p->justify = DOPRNT_JUSTIFY_INTERNAL;
92 1.1 mrg else
93 1.1 mrg p->justify = DOPRNT_JUSTIFY_RIGHT;
94 1.1 mrg
95 1.1 mrg /* ios::fixed allows prec==0, others take 0 as the default 6.
96 1.1 mrg Don't allow negatives (they do bad things to __gmp_doprnt_float_cxx). */
97 1.1 mrg p->prec = MAX (0, o.precision());
98 1.1 mrg if (p->prec == 0 && p->conv != DOPRNT_CONV_FIXED)
99 1.1 mrg p->prec = 6;
100 1.1 mrg
101 1.1 mrg /* for hex showbase is always, for octal only non-zero */
102 1.1 mrg if (o.flags() & ios::showbase)
103 1.1 mrg p->showbase = ((o.flags() & ios::basefield) == ios::hex
104 1.1 mrg ? DOPRNT_SHOWBASE_YES : DOPRNT_SHOWBASE_NONZERO);
105 1.1 mrg else
106 1.1 mrg p->showbase = DOPRNT_SHOWBASE_NO;
107 1.1 mrg
108 1.1 mrg p->showpoint = ((o.flags() & ios::showpoint) != 0);
109 1.1 mrg
110 1.1 mrg /* in fixed and scientific always show trailing zeros, in general format
111 1.1 mrg show them if showpoint is set (or so it seems) */
112 1.1 mrg if ((o.flags() & ios::floatfield) == ios::fixed
113 1.1 mrg || (o.flags() & ios::floatfield) == ios::scientific)
114 1.1 mrg p->showtrailing = 1;
115 1.1 mrg else
116 1.1 mrg p->showtrailing = p->showpoint;
117 1.1 mrg
118 1.1 mrg p->sign = (o.flags() & ios::showpos ? '+' : '\0');
119 1.1 mrg
120 1.1 mrg p->width = o.width();
121 1.1 mrg
122 1.1 mrg /* reset on each output */
123 1.1 mrg o.width (0);
124 1.1 mrg }
125