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