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