Blame cxx/osfuns.cc

Packit 5c3484
/* Support for operator<< routines.
Packit 5c3484
Packit 5c3484
   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
Packit 5c3484
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
Packit 5c3484
   FUTURE GNU MP RELEASES.
Packit 5c3484
Packit 5c3484
Copyright 2001 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <iostream>
Packit 5c3484
#include <stdarg.h>    /* for va_list and hence doprnt_funs_t */
Packit 5c3484
#include <string.h>
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
using namespace std;
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* Don't need "format" for operator<< routines, just "memory" and "reps".
Packit 5c3484
   Omitting gmp_asprintf_format lets us avoid dragging vsnprintf into the
Packit 5c3484
   link.  __gmp_asprintf_final will be called directly and doesn't need to
Packit 5c3484
   be in the struct.  */
Packit 5c3484
Packit 5c3484
const struct doprnt_funs_t  __gmp_asprintf_funs_noformat = {
Packit 5c3484
  NULL,
Packit 5c3484
  (doprnt_memory_t) __gmp_asprintf_memory,
Packit 5c3484
  (doprnt_reps_t)   __gmp_asprintf_reps,
Packit 5c3484
  NULL
Packit 5c3484
};
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
__gmp_doprnt_params_from_ios (struct doprnt_params_t *p, ios &o)
Packit 5c3484
{
Packit 5c3484
  if ((o.flags() & ios::basefield) == ios::hex)
Packit 5c3484
    {
Packit 5c3484
      p->expfmt = "@%c%02d";
Packit 5c3484
      p->base = (o.flags() & ios::uppercase ? -16 : 16);
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      p->expfmt = (o.flags() & ios::uppercase ? "E%c%02d" : "e%c%02d");
Packit 5c3484
      if ((o.flags() & ios::basefield) == ios::oct)
Packit 5c3484
        p->base = 8;
Packit 5c3484
      else
Packit 5c3484
        p->base = 10;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  /* "general" if none or more than one bit set */
Packit 5c3484
  if ((o.flags() & ios::floatfield) == ios::fixed)
Packit 5c3484
    p->conv = DOPRNT_CONV_FIXED;
Packit 5c3484
  else if ((o.flags() & ios::floatfield) == ios::scientific)
Packit 5c3484
    p->conv = DOPRNT_CONV_SCIENTIFIC;
Packit 5c3484
  else
Packit 5c3484
    p->conv = DOPRNT_CONV_GENERAL;
Packit 5c3484
Packit 5c3484
  p->exptimes4 = 0;
Packit 5c3484
Packit 5c3484
  p->fill = o.fill();
Packit 5c3484
Packit 5c3484
  /* "right" if more than one bit set */
Packit 5c3484
  if ((o.flags() & ios::adjustfield) == ios::left)
Packit 5c3484
    p->justify = DOPRNT_JUSTIFY_LEFT;
Packit 5c3484
  else if ((o.flags() & ios::adjustfield) == ios::internal)
Packit 5c3484
    p->justify = DOPRNT_JUSTIFY_INTERNAL;
Packit 5c3484
  else
Packit 5c3484
    p->justify = DOPRNT_JUSTIFY_RIGHT;
Packit 5c3484
Packit 5c3484
  /* ios::fixed allows prec==0, others take 0 as the default 6.
Packit 5c3484
     Don't allow negatives (they do bad things to __gmp_doprnt_float_cxx).  */
Packit 5c3484
  p->prec = MAX (0, o.precision());
Packit 5c3484
  if (p->prec == 0 && p->conv != DOPRNT_CONV_FIXED)
Packit 5c3484
    p->prec = 6;
Packit 5c3484
Packit 5c3484
  /* for hex showbase is always, for octal only non-zero */
Packit 5c3484
  if (o.flags() & ios::showbase)
Packit 5c3484
    p->showbase = ((o.flags() & ios::basefield) == ios::hex
Packit 5c3484
                   ? DOPRNT_SHOWBASE_YES : DOPRNT_SHOWBASE_NONZERO);
Packit 5c3484
  else
Packit 5c3484
    p->showbase = DOPRNT_SHOWBASE_NO;
Packit 5c3484
Packit 5c3484
  p->showpoint = ((o.flags() & ios::showpoint) != 0);
Packit 5c3484
Packit 5c3484
  /* in fixed and scientific always show trailing zeros, in general format
Packit 5c3484
     show them if showpoint is set (or so it seems) */
Packit 5c3484
  if ((o.flags() & ios::floatfield) == ios::fixed
Packit 5c3484
      || (o.flags() & ios::floatfield) == ios::scientific)
Packit 5c3484
    p->showtrailing = 1;
Packit 5c3484
  else
Packit 5c3484
    p->showtrailing = p->showpoint;
Packit 5c3484
Packit 5c3484
  p->sign = (o.flags() & ios::showpos ? '+' : '\0');
Packit 5c3484
Packit 5c3484
  p->width = o.width();
Packit 5c3484
Packit 5c3484
  /* reset on each output */
Packit 5c3484
  o.width (0);
Packit 5c3484
}