This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

num_put<>::do_put



Perhaps we could use the sprintf function to convert
a number to a string?

Below two functions (for double and long), that find out the
format for the sprintf function.

Usage:
sprintf(__charbuf, get_double_fmt(__flags), __precision, __a_double);
sprintf(__charbuf, get_long_fmt(__flags), __a_long);   



const char*
get_double_fmt(ios_base::fmtflags __flags)
{
  const char* __fmt[][4] = {{"%.*f", "%+.*f", "%#.*f", "+#.*f"},
                            {"%.*e", "%+.*e", "%#.*e", "+#.*e"},
                            {"%.*g", "%+.*g", "%#.*g", "+#.*g"}};

  int __line = 0;
  ios_base::fmtflags __flf = __flags & ios_base::floatfield;
  if (__flf == ios_base::fixed)
    ;
  else if (__flf == ios_base::scientific)
    __line = 1;
  else
    __line = 2;

  int __column = 0;
  if (__flags & ios_base::showpos)
    __column += 1;
  if (__flags & ios_base::showpoint)
    __column += 2;

  return __fmt[__line][__column];
}


const char*
get_long_fmt(ios_base::fmtflags __flags)
{
  const char* __fmt[][4] = {{"%ld", "%+ld", "%#ld", "+#ld"},
                            {"%lo", "%+lo", "%#lo", "+#lo"},
                            {"%lx", "%+lx", "%#lx", "+#lx"},
                            {"%lX", "%+lX", "%#lX", "+#lX"}};

  int __line = 0;
  if (__flags & ios_base::oct)
    __line += 1;
  else if (__flags & ios_base::hex) {
    __line += 2;
    if (__flags & ios_base::uppercase) 
      __line += 1;
  }
  int __column = 0;
  if (__flags & ios_base::showpos)
    __column += 1;
  if (__flags & ios_base::showbase)
    __column += 2;

  return __fmt[__line][__column];
}


Ryszard Kabatek
Martin-Luther University Halle-Wittenberg, Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]