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