aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/cxx/osfuns.cc
blob: 81590a8b168ac25b750a153cf05deb8495ca0a6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Support for operator<< routines.

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include <iostream>
#include <stdarg.h>    /* for va_list and hence doprnt_funs_t */
#include <string.h>

#include "gmp-impl.h"

using namespace std;


/* Don't need "format" for operator<< routines, just "memory" and "reps".
   Omitting gmp_asprintf_format lets us avoid dragging vsnprintf into the
   link.  __gmp_asprintf_final will be called directly and doesn't need to
   be in the struct.  */

const struct doprnt_funs_t  __gmp_asprintf_funs_noformat = {
  NULL,
  (doprnt_memory_t) __gmp_asprintf_memory,
  (doprnt_reps_t)   __gmp_asprintf_reps,
  NULL
};


void
__gmp_doprnt_params_from_ios (struct doprnt_params_t *p, ios &o)
{
  if ((o.flags() & ios::basefield) == ios::hex)
    {
      p->expfmt = "@%c%02d";
      p->base = (o.flags() & ios::uppercase ? -16 : 16);
    }
  else
    {
      p->expfmt = (o.flags() & ios::uppercase ? "E%c%02d" : "e%c%02d");
      if ((o.flags() & ios::basefield) == ios::oct)
        p->base = 8;
      else
        p->base = 10;
    }

  /* "general" if none or more than one bit set */
  if ((o.flags() & ios::floatfield) == ios::fixed)
    p->conv = DOPRNT_CONV_FIXED;
  else if ((o.flags() & ios::floatfield) == ios::scientific)
    p->conv = DOPRNT_CONV_SCIENTIFIC;
  else
    p->conv = DOPRNT_CONV_GENERAL;

  p->exptimes4 = 0;

  p->fill = o.fill();

  /* "right" if more than one bit set */
  if ((o.flags() & ios::adjustfield) == ios::left)
    p->justify = DOPRNT_JUSTIFY_LEFT;
  else if ((o.flags() & ios::adjustfield) == ios::internal)
    p->justify = DOPRNT_JUSTIFY_INTERNAL;
  else
    p->justify = DOPRNT_JUSTIFY_RIGHT;

  /* ios::fixed allows prec==0, others take 0 as the default 6.
     Don't allow negatives (they do bad things to __gmp_doprnt_float_cxx).  */
  p->prec = MAX (0, o.precision());
  if (p->prec == 0 && p->conv != DOPRNT_CONV_FIXED)
    p->prec = 6;

  /* for hex showbase is always, for octal only non-zero */
  if (o.flags() & ios::showbase)
    p->showbase = ((o.flags() & ios::basefield) == ios::hex
                   ? DOPRNT_SHOWBASE_YES : DOPRNT_SHOWBASE_NONZERO);
  else
    p->showbase = DOPRNT_SHOWBASE_NO;

  p->showpoint = ((o.flags() & ios::showpoint) != 0);

  /* in fixed and scientific always show trailing zeros, in general format
     show them if showpoint is set (or so it seems) */
  if ((o.flags() & ios::floatfield) == ios::fixed
      || (o.flags() & ios::floatfield) == ios::scientific)
    p->showtrailing = 1;
  else
    p->showtrailing = p->showpoint;

  p->sign = (o.flags() & ios::showpos ? '+' : '\0');

  p->width = o.width();

  /* reset on each output */
  o.width (0);
}