/home/ntakagi/work/STLport-5.1.5/src/num_put_float.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 
00019 #include "stlport_prefix.h"
00020 
00021 #include <cmath>
00022 #include <ios>
00023 #include <locale>
00024 
00025 #if defined (__DECCXX)
00026 #  define NDIG 400
00027 #else
00028 #  define NDIG 82
00029 #endif
00030 
00031 #if defined (_STLP_NO_LONG_DOUBLE)
00032 #  define MAXECVT 17
00033 #  define MAXFCVT 18
00034 typedef double max_double_type;
00035 #else
00036 #  define MAXECVT 35
00037 #  define MAXFCVT 36
00038 typedef long double max_double_type;
00039 #endif
00040 
00041 #define MAXFSIG MAXECVT
00042 #define MAXESIZ 5
00043 
00044 #define todigit(x) ((x)+'0')
00045 
00046 #if defined (_STLP_UNIX)
00047 
00048 #  if defined (__sun)
00049 #    include <floatingpoint.h>
00050 #  endif
00051 
00052 #  if defined (__sun) || defined (__digital__) || defined (__sgi) || defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
00053 // DEC, SGI & Solaris need this
00054 #    include <values.h>
00055 #    include <nan.h>
00056 #  endif
00057 
00058 #  if defined (__QNXNTO__) || ( defined(__GNUC__) && defined(__APPLE__) ) || defined(_STLP_USE_UCLIBC) /* 0.9.26 */ || \
00059       defined(__FreeBSD__)
00060 #    define USE_SPRINTF_INSTEAD
00061 #  endif
00062 
00063 #  if defined( _AIX ) // JFA 3-Aug-2000
00064 #    include <math.h>
00065 #    include <float.h>
00066 #  endif
00067 
00068 #endif
00069 
00070 #include <cstdio>
00071 #include <cstdlib>
00072 
00073 //#if defined(_CRAY)
00074 //# include <stdlib.h>
00075 //#endif
00076 
00077 #if defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__) || defined (__DJGPP) || \
00078     defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
00079 #  include <float.h>
00080 #endif
00081 
00082 #if defined(__MRC__) || defined(__SC__)  || defined(_CRAY)  //*TY 02/24/2000 - added support for MPW
00083 #  include <fp.h>
00084 #endif
00085 
00086 #if defined (__CYGWIN__)
00087 #  include <ieeefp.h>
00088 #endif
00089 
00090 #if defined (__MSL__)
00091 #  include <cstdlib>  // for atoi
00092 #  include <cstdio>  // for snprintf
00093 #  include <algorithm>
00094 #  include <cassert>
00095 #endif
00096 
00097 #if defined (__ISCPP__)
00098 #  include <cfloat>
00099 #endif
00100 
00101 #include <algorithm>
00102 
00103 #if defined (__DMC__)
00104 #  define snprintf _snprintf
00105 #endif
00106 
00107 #if defined(__hpux) && (!defined(_INCLUDE_HPUX_SOURCE) || defined(__GNUC__))
00108 extern "C" double erf(double);
00109 extern "C" double erfc(double);
00110 extern "C" double gamma(double);                             /* obsolescent */
00111 extern "C" double hypot(double, double);
00112 extern "C" int    isnan(double);
00113 extern "C" double j0(double);
00114 extern "C" double j1(double);
00115 extern "C" double jn(int, double);
00116 extern "C" double lgamma(double);
00117 extern "C" double y0(double);
00118 extern "C" double y1(double);
00119 extern "C" double yn(int, double);
00120 
00121 #  define HUGE_VALF     _SINFINITY
00122 #  define INFINITY      _SINFINITY
00123 #  define NAN           _SQNAN
00124 
00125 #  define isnan(x)              _ISNAN(x)
00126 #  define isinf(x)              _ISINF(x)
00127 #  define signbit(x)            _SIGNBIT(x)
00128 #  define isfinite(x)           _ISFINITE(x)
00129 #  define isnormal(x)           _ISNORMAL(x)
00130 #  define fpclassify(x)         _FPCLASSIFY(x)
00131 #  define isunordered(x,y)      _ISUNORDERED(x,y)
00132 #  define isgreater(x,y)        _ISGREATER(x,y)
00133 #  define isgreaterequal(x,y)   _ISGREATEREQUAL(x,y)
00134 #  define isless(x,y)           _ISLESS(x,y)
00135 #  define islessequal(x,y)      _ISLESSEQUAL(x,y)
00136 #  define islessgreater(x,y)    _ISLESSGREATER(x,y)
00137 
00138 #  define FP_NORMAL     0
00139 #  define FP_ZERO       1
00140 #  define FP_INFINITE   2
00141 #  define FP_SUBNORMAL  3
00142 #  define FP_NAN        4
00143 
00144 #  define DECIMAL_DIG   17
00145 
00146 #  define _IS64(x) (sizeof(x) == sizeof(double))
00147 #  define _IS32(x) (sizeof(x) == sizeof(float))
00148 
00149 extern "C" {
00150   extern double copysign(double, double);
00151   extern const float _SINFINITY;
00152   extern const float _SQNAN;
00153 //#  if defined (_PA_RISC)
00154 #    define _ISNAN(x)          (_IS32(x)?_Isnanf(x):(isnan)(x))
00155 #    define _ISINF(x)          (_IS32(x)?_Isinff(x):_Isinf(x))
00156 #    define _SIGNBIT(x)        (_IS32(x)?_Signbitf(x):_Signbit(x))
00157 #    define _ISFINITE(x)       (_IS32(x)?_Isfinitef(x):_Isfinite(x))
00158 #    define _ISNORMAL(x)       (_IS32(x)?_Isnormalf(x):_Isnormal(x))
00159 #    define _FPCLASSIFY(x)     (_IS32(x)?_Fpclassifyf(x)>>1:_Fpclassify(x)>>1)
00160 #    define _ISUNORDERED(x,y)  (_IS32(x)&&_IS32(y)?_Isunorderedf(x,y):_Isunordered(x,y))
00161   extern int _Signbit(double);
00162   extern int _Signbitf(float);
00163   extern int _Isnanf(float);
00164   extern int _Isfinite(double);
00165   extern int _Isfinitef(float);
00166   extern int _Isinf(double);
00167   extern int _Isinff(float);
00168   extern int _Isnormal(double);
00169   extern int _Isnormalf(float);
00170   extern int _Isunordered(double, double);
00171   extern int _Isunorderedf(float, float);
00172   extern int _Fpclassify(double);
00173   extern int _Fpclassifyf(float);
00174 //#  else
00175 //#    include "math_ia64_internal.h"
00176 //#    define _FPCLASSIFY(x)     (_IS32(x)?_Fpclassf(x):_Fpclass(x))
00177 //  extern int _Fpclass(double);
00178 //  extern int _Fpclassf(float);
00179 //#  endif
00180 }
00181 
00182 #  if !defined (_INCLUDE_XOPEN_SOURCE_EXTENDED)
00183 extern "C" char *fcvt(double, int, int *, int *);
00184 extern "C" char *ecvt(double, int, int *, int *);
00185 #  endif
00186 #  if !defined (_INCLUDE_HPUX_SOURCE)
00187 #    if !defined (_LONG_DOUBLE)
00188 #      define _LONG_DOUBLE
00189 typedef struct {
00190   uint32_t word1, word2, word3, word4;
00191 } long_double;
00192 #    endif /* _LONG_DOUBLE */
00193 extern "C" char *_ldecvt(long_double, int, int *, int *);
00194 extern "C" char *_ldfcvt(long_double, int, int *, int *);
00195 
00196 #  endif
00197 #endif /* __hpux */
00198 
00199 _STLP_BEGIN_NAMESPACE
00200 _STLP_MOVE_TO_PRIV_NAMESPACE
00201 
00202 #if defined (__MWERKS__) || defined(__BEOS__)
00203 #  define USE_SPRINTF_INSTEAD
00204 #endif
00205 
00206 #if defined (_AIX) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
00207 // Some OS'es only provide non-reentrant primitives, so we have to use additional synchronization here
00208 
00209 #  if !defined(_REENTRANT) && !defined(_THREAD_SAFE) && !(defined(_POSIX_THREADS) && defined(__OpenBSD__))
00210 #    define LOCK_CVT
00211 #    define RETURN_CVT(ecvt, x, n, pt, sign, buf) return ecvt(x, n, pt, sign);
00212 #  else
00213 static _STLP_STATIC_MUTEX __put_float_mutex _STLP_MUTEX_INITIALIZER;
00214 #    define LOCK_CVT _STLP_auto_lock lock(__put_float_mutex);
00215 #    define RETURN_CVT(ecvt, x, n, pt, sign, buf) strcpy(buf, ecvt(x, n, pt, sign)); return buf;
00216 #  endif // !_REENTRANT
00217 #endif // _AIX || __FreeBSD__ || __NetBSD__ || __OpenBSD__
00218 
00219 // Tests for infinity and NaN differ on different OSs.  We encapsulate
00220 // these differences here.
00221 
00222 #if !defined (USE_SPRINTF_INSTEAD)
00223 #  if defined (__hpux) || defined (__DJGPP) || (defined (_STLP_USE_GLIBC) && ! defined (__MSL__)) || \
00224       defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
00225 static inline bool _Stl_is_nan_or_inf(double x)
00226 #    if defined (isfinite)
00227 { return !isfinite(x); }
00228 #    else
00229 { return !finite(x); }
00230 #    endif
00231 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 ); }
00232 static inline bool _Stl_is_inf(double x)        { return isinf(x); }
00233 // inline bool _Stl_is_neg_inf(double x)    { return isinf(x) < 0; }
00234 static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && x < 0; }
00235 #  elif (defined (__unix) || defined (__unix__)) && \
00236          !defined (__APPLE__) && !defined (__DJGPP) && !defined(__osf__) && \
00237          !defined (_CRAY)
00238 static inline bool _Stl_is_nan_or_inf(double x) { return IsNANorINF(x); }
00239 static inline bool _Stl_is_inf(double x)        { return IsNANorINF(x) && IsINF(x); }
00240 static inline bool _Stl_is_neg_inf(double x)    { return (IsINF(x)) && (x < 0.0); }
00241 static inline bool _Stl_is_neg_nan(double x)    { return IsNegNAN(x); }
00242 #  elif defined (__BORLANDC__) && ( __BORLANDC__ < 0x540 )
00243 static inline bool _Stl_is_nan_or_inf(double x) {  return !_finite(x); }
00244 static inline bool _Stl_is_inf(double x)        {  return _Stl_is_nan_or_inf(x) && ! _isnan(x);}
00245 static inline bool _Stl_is_neg_inf(double x)    {  return _Stl_is_inf(x) && x < 0 ; }
00246 static inline bool _Stl_is_neg_nan(double x)    { return _isnan(x) && x < 0 ; }
00247 #  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
00248 static inline bool _Stl_is_nan_or_inf(double x) { return !_finite(x); }
00249 static inline bool _Stl_is_inf(double x)        {
00250   int fclass = _fpclass(x);
00251   return fclass == _FPCLASS_NINF || fclass == _FPCLASS_PINF;
00252 }
00253 static inline bool _Stl_is_neg_inf(double x)    { return _fpclass(x) == _FPCLASS_NINF; }
00254 static inline bool _Stl_is_neg_nan(double x)    { return _isnan(x) && _copysign(1., x) < 0 ; }
00255 #  elif defined (__MRC__) || defined (__SC__)    //*TY 02/24/2000 - added support for MPW
00256 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !isfinite(x); }
00257 static bool _Stl_is_inf(double x)        { return !isfinite(x); }
00258 static bool _Stl_is_neg_inf(double x)    { return !isfinite(x) && signbit(x); }
00259 static bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
00260 #  elif /* defined(__FreeBSD__) || defined(__OpenBSD__) || */ (defined(__GNUC__) && defined(__APPLE__))
00261 static inline bool _Stl_is_nan_or_inf(double x) { return !finite(x); }
00262 static inline bool _Stl_is_inf(double x)        {   return _Stl_is_nan_or_inf(x) && ! isnan(x); }
00263 static inline bool _Stl_is_neg_inf(double x)    {   return _Stl_is_inf(x) && x < 0 ; }
00264 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && copysign(1., x) < 0 ; }
00265 #  elif defined( _AIX ) // JFA 11-Aug-2000
00266 static bool _Stl_is_nan_or_inf(double x) { return isnan(x) || !finite(x); }
00267 static bool _Stl_is_inf(double x)        { return !finite(x); }
00268 // bool _Stl_is_neg_inf(double x)    { return _class(x) == FP_MINUS_INF; }
00269 static bool _Stl_is_neg_inf(double x)    { return _Stl_is_inf(x) && ( copysign(1., x) < 0 );  }
00270 static bool _Stl_is_neg_nan(double x)    { return isnan(x) && ( copysign(1., x) < 0 );  }
00271 #  elif defined (__ISCPP__)
00272 static inline bool _Stl_is_nan_or_inf  (double x) { return _fp_isINF(x) || _fp_isNAN(x); }
00273 static inline bool _Stl_is_inf         (double x) { return _fp_isINF(x); }
00274 static inline bool _Stl_is_neg_inf     (double x) { return _fp_isINF(x) && x < 0; }
00275 static inline bool _Stl_is_neg_nan     (double x) { return _fp_isNAN(x) && x < 0; }
00276 #  elif defined (_CRAY)
00277 #    if defined (_CRAYIEEE)
00278 static inline bool _Stl_is_nan_or_inf(double x) { return isnan(x) || isinf(x); }
00279 static inline bool _Stl_is_inf(double x)        { return isinf(x); }
00280 static inline bool _Stl_is_neg_inf(double x)    { return isinf(x) && signbit(x); }
00281 static inline bool _Stl_is_neg_nan(double x)    { return isnan(x) && signbit(x); }
00282 #    else
00283 static inline bool _Stl_is_nan_or_inf(double x) { return false; }
00284 static inline bool _Stl_is_inf(double x)        { return false; }
00285 static inline bool _Stl_is_neg_inf(double x)    { return false; }
00286 static inline bool _Stl_is_neg_nan(double x)    { return false; }
00287 #    endif
00288 #  else // nothing from above
00289 #    define USE_SPRINTF_INSTEAD
00290 #  endif
00291 #endif // !USE_SPRINTF_INSTEAD
00292 
00293 #if !defined (USE_SPRINTF_INSTEAD)
00294 // Reentrant versions of floating-point conversion functions.  The argument
00295 // lists look slightly different on different operating systems, so we're
00296 // encapsulating the differences here.
00297 
00298 #  if defined (__CYGWIN__) || defined(__DJGPP)
00299 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00300 { return ecvtbuf(x, n, pt, sign, buf); }
00301 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00302 { return fcvtbuf(x, n, pt, sign, buf); }
00303 #    if !defined (_STLP_NO_LONG_DOUBLE)
00304 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00305 { return ecvtbuf(x, n, pt, sign, buf); }
00306 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00307 { return fcvtbuf(x, n, pt, sign, buf); }
00308 #    endif
00309 #  elif defined (_STLP_USE_GLIBC)
00310 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00311 { return buf + ecvt_r(x, n, pt, sign, buf, NDIG+2); }
00312 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00313 { return buf + fcvt_r(x, n, pt, sign, buf, NDIG+2); }
00314 #    if !defined (_STLP_NO_LONG_DOUBLE)
00315 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00316 { return buf + qecvt_r(x, n, pt, sign, buf, NDIG+2); }
00317 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00318 { return buf + qfcvt_r(x, n, pt, sign, buf, NDIG+2); }
00319 #    endif
00320 #  elif defined (_STLP_SCO_OPENSERVER) || defined (__NCR_SVR)
00321 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00322 { return ecvt(x, n, pt, sign); }
00323 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00324 { return fcvt(x, n, pt, sign); }
00325 #    if !defined (_STLP_NO_LONG_DOUBLE)
00326 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00327 { return ecvtl(x, n, pt, sign); }
00328 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00329 { return fcvtl(x, n, pt, sign); }
00330 #    endif
00331 #  elif defined (__sun)
00332 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00333 { return econvert(x, n, pt, sign, buf); }
00334 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00335 { return fconvert(x, n, pt, sign, buf); }
00336 #    if !defined (_STLP_NO_LONG_DOUBLE)
00337 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00338 { return qeconvert(&x, n, pt, sign, buf); }
00339 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00340 { return qfconvert(&x, n, pt, sign, buf); }
00341 #    endif
00342 #  elif defined (__DECCXX)
00343 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00344 { return (ecvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
00345 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00346 { return (fcvt_r(x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
00347 #    if !defined (_STLP_NO_LONG_DOUBLE)
00348 // fbp : no "long double" conversions !
00349 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00350 { return (ecvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0) ; }
00351 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00352 { return (fcvt_r((double)x, n, pt, sign, buf, NDIG)==0 ? buf : 0); }
00353 #    endif
00354 #  elif defined (__hpux)
00355 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00356 { return ecvt(x, n, pt, sign); }
00357 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00358 { return fcvt(x, n, pt, sign); }
00359 #    if !defined (_STLP_NO_LONG_DOUBLE)
00360 
00361 #      if defined( _REENTRANT ) && (defined(_PTHREADS_DRAFT4) || defined(PTHREAD_THREADS_MAX))
00362 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00363 { return (_ldecvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
00364 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00365 { return (_ldfcvt_r(*(long_double*)&x, n, pt, sign, buf, NDIG+2)==0 ? buf : 0); }
00366 #      else
00367 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00368 { return _ldecvt(*(long_double*)&x, n, pt, sign); }
00369 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00370 { return _ldfcvt(*(long_double*)&x, n, pt, sign); }
00371 #      endif
00372 #    endif
00373 #  elif defined (_AIX) || defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__)
00374 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00375 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
00376 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00377 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
00378 #    if !defined (_STLP_NO_LONG_DOUBLE)
00379 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00380 { LOCK_CVT RETURN_CVT(ecvt, x, n, pt, sign, buf) }
00381 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00382 { LOCK_CVT RETURN_CVT(fcvt, x, n, pt, sign, buf) }
00383 #    endif
00384 #  elif defined (__unix) && !defined (__APPLE__) && !defined (_CRAY)
00385 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00386 { return ecvt_r(x, n, pt, sign, buf); }
00387 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00388 { return fcvt_r(x, n, pt, sign, buf); }
00389 #    if !defined (_STLP_NO_LONG_DOUBLE)
00390 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00391 { return qecvt_r(x, n, pt, sign, buf); }
00392 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00393 { return qfcvt_r(x, n, pt, sign, buf); }
00394 #    endif
00395 #  elif defined (_STLP_MSVC_LIB) || defined (__MINGW32__) || defined (__BORLANDC__)
00396 // those guys claim _cvt functions being reentrant.
00397 #    if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
00398 #      define _STLP_APPEND(a, b) a##b
00399 #      define _STLP_BUF_PARAMS , char* buf, size_t bsize
00400 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) _STLP_APPEND(F, _s)(buf, bsize, X, N, PT, SIGN); return buf
00401 #    else
00402 #      define _STLP_CVT_DONT_NEED_BUF
00403 #      define _STLP_BUF_PARAMS
00404 #      define _STLP_SECURE_FUN(F, X, N, PT, SIGN) return F(X, N, PT, SIGN)
00405 #    endif
00406 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
00407 { _STLP_SECURE_FUN(_ecvt, x, n, pt, sign); }
00408 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
00409 { _STLP_SECURE_FUN(_fcvt, x, n, pt, sign); }
00410 #    if !defined (_STLP_NO_LONG_DOUBLE)
00411 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
00412 { _STLP_SECURE_FUN(_ecvt, (double)x, n, pt, sign); }
00413 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign _STLP_BUF_PARAMS)
00414 { _STLP_SECURE_FUN(_fcvt, (double)x, n, pt, sign); }
00415 #    endif
00416 #    undef _STLP_SECURE_FUN
00417 #    undef _STLP_BUF_PARAMS
00418 #    undef _STLP_APPEND
00419 #  elif defined (__ISCPP__)
00420 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* buf)
00421 { return _fp_ecvt( x, n, pt, sign, buf); }
00422 
00423 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* buf)
00424 { return _fp_fcvt(x, n, pt, sign, buf); }
00425 
00426 #    if !defined (_STLP_NO_LONG_DOUBLE)
00427 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* buf)
00428 { return _fp_ecvt( x, n, pt, sign, buf); }
00429 
00430 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf)
00431 { return _fp_fcvt(x, n, pt, sign, buf); }
00432 #    endif
00433 #  elif defined (__MRC__) || defined (__SC__) || defined (_CRAY)
00434 static inline char* _Stl_ecvtR(double x, int n, int* pt, int* sign, char* )
00435 { return ecvt( x, n, pt, sign ); }
00436 static inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char* )
00437 { return fcvt(x, n, pt, sign); }
00438 #    if !defined (_STLP_NO_LONG_DOUBLE)
00439 static inline char* _Stl_qecvtR(long double x, int n, int* pt, int* sign, char* )
00440 { return ecvt( x, n, pt, sign ); }
00441 static inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* )
00442 { return fcvt(x, n, pt, sign); }
00443 #    endif
00444 #  endif
00445 
00446 #  if defined (_STLP_CVT_DONT_NEED_BUF)
00447 #    define _STLP_CVT_BUFFER(B)
00448 #  elif !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
00449 #    define _STLP_CVT_BUFFER(B) , B
00450 #  else
00451 #    define _STLP_CVT_BUFFER(B) , _STLP_ARRAY_AND_SIZE(B)
00452 #  endif
00453 
00454 #  if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
00455 #    define _STLP_BUFFER(B) B
00456 #  else
00457 #    define _STLP_BUFFER(B) _STLP_ARRAY_AND_SIZE(B)
00458 #  endif
00459 
00460 //----------------------------------------------------------------------
00461 // num_put
00462 
00463 // __format_float formats a mantissa and exponent as returned by
00464 // one of the conversion functions (ecvt_r, fcvt_r, qecvt_r, qfcvt_r)
00465 // according to the specified precision and format flags.  This is
00466 // based on doprnt but is much simpler since it is concerned only
00467 // with floating point input and does not consider all formats.  It
00468 // also does not deal with blank padding, which is handled by
00469 // __copy_float_and_fill.
00470 
00471 static size_t __format_float_scientific( __iostring& buf, const char *bp,
00472                                          int decpt, int sign, bool is_zero,
00473                                          ios_base::fmtflags flags,
00474                                          int precision, bool /* islong */)
00475 {
00476   // sign if required
00477   if (sign)
00478     buf += '-';
00479   else if (flags & ios_base::showpos)
00480     buf += '+';
00481 
00482   // first digit of mantissa
00483   buf += *bp++;
00484 
00485   // start of grouping position, grouping won't occur in scientific notation
00486   // as it is impossible to have something like 1234.0e04 but we return a correct
00487   // group position for coherency with __format_float_fixed.
00488   size_t __group_pos = buf.size();
00489 
00490   // decimal point if required
00491   if (precision != 0 || flags & ios_base::showpoint) {
00492     buf += '.';
00493   }
00494 
00495   // rest of mantissa
00496   int rz = precision;
00497   while (rz-- > 0 && *bp != 0)
00498     buf += *bp++;
00499 
00500   // exponent
00501   char expbuf[MAXESIZ + 2];
00502   char *suffix = expbuf + MAXESIZ;
00503   *suffix = 0;
00504   if (!is_zero) {
00505     int nn = decpt - 1;
00506     if (nn < 0)
00507       nn = -nn;
00508     for (; nn > 9; nn /= 10)
00509       *--suffix = (char) todigit(nn % 10);
00510     *--suffix = (char) todigit(nn);
00511   }
00512 
00513   // prepend leading zeros to exponent
00514   while (suffix > &expbuf[MAXESIZ - 2])
00515     *--suffix = '0';
00516 
00517   // put in the exponent sign
00518   *--suffix = (char) ((decpt > 0 || is_zero ) ? '+' : '-');
00519 
00520   // put in the e
00521   *--suffix = flags & ios_base::uppercase ? 'E' : 'e';
00522 
00523   // copy the suffix
00524   buf += suffix;
00525   return __group_pos;
00526 }
00527 
00528 static size_t __format_float_fixed( __iostring &buf, const char *bp,
00529                                     int decpt, int sign, bool /* x */,
00530                                     ios_base::fmtflags flags,
00531                                     int precision, bool islong )
00532 {
00533   if ( sign && (decpt > -precision) && (*bp != 0) )
00534     buf += '-';
00535   else if ( flags & ios_base::showpos )
00536     buf += '+';
00537 
00538   int k       = 0;
00539   int maxfsig = islong ? 2*MAXFSIG : MAXFSIG;
00540 
00541   // digits before decimal point
00542   int nnn = decpt;
00543   do {
00544     buf += ((nnn <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++));
00545   } while ( --nnn > 0 );
00546 
00547   // start of grouping position
00548   size_t __group_pos = buf.size();
00549 
00550   // decimal point if needed
00551   if ( flags & ios_base::showpoint || precision > 0 ) {
00552     buf += '.';
00553   }
00554 
00555   // digits after decimal point if any
00556   nnn = (min) (precision, MAXFCVT);
00557 
00558   while ( --nnn >= 0 ) {
00559     buf += (++decpt <= 0 || *bp == 0 || k >= maxfsig) ? '0' : (++k, *bp++);
00560   }
00561 
00562   // trailing zeros if needed
00563   if ( precision > MAXFCVT ) {
00564     buf.append( precision - MAXFCVT, '0' );
00565   }
00566 
00567   return __group_pos;
00568 }
00569 
00570 static void __format_nan_or_inf(__iostring& buf, double x, ios_base::fmtflags flags)
00571 {
00572   static const char* inf[2] = { "inf", "Inf" };
00573   static const char* nan[2] = { "nan", "NaN" };
00574   const char** inf_or_nan;
00575   if (_Stl_is_inf(x)) {            // Infinity
00576     inf_or_nan = inf;
00577     if (_Stl_is_neg_inf(x))
00578       buf += '-';
00579     else if (flags & ios_base::showpos)
00580       buf += '+';
00581   } else {                      // NaN
00582     inf_or_nan = nan;
00583     if (_Stl_is_neg_nan(x))
00584       buf += '-';
00585     else if (flags & ios_base::showpos)
00586       buf += '+';
00587   }
00588   buf += inf_or_nan[flags & ios_base::uppercase ? 1 : 0];
00589 }
00590 
00591 template <class max_double_type>
00592 static inline size_t __format_float( __iostring &buf, const char * bp,
00593                                      int decpt, int sign, max_double_type x,
00594                                      ios_base::fmtflags flags,
00595                                      int precision, bool islong)
00596 {
00597   size_t __group_pos = 0;
00598   // Output of infinities and NANs does not depend on the format flags
00599   if (_Stl_is_nan_or_inf((double)x)) {       // Infinity or NaN
00600     __format_nan_or_inf(buf, (double)x, flags);
00601   } else {                        // representable number
00602     switch (flags & ios_base::floatfield) {
00603       case ios_base::scientific:
00604         __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0.0,
00605                                                  flags, precision, islong);
00606         break;
00607       case ios_base::fixed:
00608         __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
00609                                             flags, precision, islong);
00610         break;
00611       default: // g format
00612         // establish default precision
00613         if (flags & ios_base::showpoint || precision > 0) {
00614           if (precision == 0) precision = 1;
00615         } else
00616           precision = 6;
00617 
00618         // reset exponent if value is zero
00619         if (x == 0)
00620           decpt = 1;
00621 
00622         int kk = precision;
00623         if (!(flags & ios_base::showpoint)) {
00624           size_t n = strlen(bp);
00625           if (n < (size_t)kk)
00626             kk = (int)n;
00627           while (kk >= 1 && bp[kk-1] == '0')
00628             --kk;
00629         }
00630 
00631         if (decpt < -3 || decpt > precision) {
00632           precision = kk - 1;
00633           __group_pos = __format_float_scientific( buf, bp, decpt, sign, x == 0,
00634                                                    flags, precision, islong);
00635         } else {
00636           precision = kk - decpt;
00637           __group_pos = __format_float_fixed( buf, bp, decpt, sign, true,
00638                                               flags, precision, islong);
00639         }
00640         break;
00641     } /* switch */
00642   } /* else is_nan_or_inf */
00643   return __group_pos;
00644 }
00645 
00646 #else /* USE_SPRINTF_INSTEAD */
00647 
00648 struct GroupPos {
00649   bool operator () (char __c) const {
00650     return __c == '.' ||
00651            __c == 'e' || __c == 'E';
00652   }
00653 };
00654 
00655 // Creates a format string for sprintf()
00656 static int __fill_fmtbuf(char* fmtbuf, ios_base::fmtflags flags, char long_modifier) {
00657   fmtbuf[0] = '%';
00658   int i = 1;
00659 
00660   if (flags & ios_base::showpos)
00661     fmtbuf[i++] = '+';
00662 
00663   if (flags & ios_base::showpoint)
00664     fmtbuf[i++] = '#';
00665 
00666   fmtbuf[i++] = '.';
00667   fmtbuf[i++] = '*';
00668 
00669   if (long_modifier)
00670     fmtbuf[i++] = long_modifier;
00671 
00672   switch (flags & ios_base::floatfield)
00673     {
00674     case ios_base::scientific:
00675       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'E' : 'e';
00676       break;
00677     case ios_base::fixed:
00678 #  if defined (__FreeBSD__)
00679       fmtbuf[i++] = 'f';
00680 #  else
00681       fmtbuf[i++] = (flags & ios_base::uppercase) ? 'F' : 'f';
00682 #  endif
00683       break;
00684     default:
00685       fmtbuf[i++] = (flags & ios_base::uppercase) ?  'G' : 'g';
00686       break;
00687     }
00688 
00689   fmtbuf[i] = 0;
00690   return i;
00691 }
00692 
00693 #endif  /* USE_SPRINTF_INSTEAD */
00694 
00695 size_t  _STLP_CALL
00696 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
00697               double x) {
00698 #if defined (USE_SPRINTF_INSTEAD)
00699   /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
00700    * below. - ptr
00701    */
00702   char static_buf[128];
00703   // char *static_buf = new char [128+precision];
00704   char fmtbuf[32];
00705   __fill_fmtbuf(fmtbuf, flags, 0);
00706   // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
00707 #  if !defined (N_PLAT_NLM)
00708   snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
00709 #  else
00710   sprintf(static_buf, fmtbuf, precision, x);
00711 #  endif
00712   buf = static_buf;
00713   // delete [] static_buf;
00714   return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
00715 #else
00716 #  if !defined (_STLP_CVT_DONT_NEED_BUF)
00717   char cvtbuf[NDIG + 2];
00718 #  endif
00719   char * bp;
00720   int decpt, sign;
00721 
00722   switch (flags & ios_base::floatfield) {
00723   case ios_base::fixed:
00724     bp = _Stl_fcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00725     break;
00726   case ios_base::scientific :
00727     bp = _Stl_ecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00728     break;
00729   default :
00730     bp = _Stl_ecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00731     break;
00732   }
00733   return __format_float(buf, bp, decpt, sign, x, flags, precision, false);
00734 #endif
00735 }
00736 
00737 #if !defined (_STLP_NO_LONG_DOUBLE)
00738 size_t _STLP_CALL
00739 __write_float(__iostring &buf, ios_base::fmtflags flags, int precision,
00740               long double x) {
00741 #  if defined (USE_SPRINTF_INSTEAD)
00742   /* If we want 'abitrary' precision, we should use 'abitrary' buffer size
00743    * below. - ptr
00744    */
00745   char static_buf[128];
00746   // char *static_buf = new char [128+precision];
00747   char fmtbuf[64];
00748   int i = __fill_fmtbuf(fmtbuf, flags, 'L');
00749   // snprintf(static_buf, 128+precision, fmtbuf, precision, x);
00750 #    if !defined (N_PLAT_NLM)
00751   snprintf(_STLP_ARRAY_AND_SIZE(static_buf), fmtbuf, precision, x);
00752 #    else
00753   sprintf(static_buf, fmtbuf, precision, x);
00754 #    endif
00755   // we should be able to return buf + sprintf(), but we do not trust'em...
00756   buf = static_buf;
00757   // delete [] static_buf;
00758   return find_if(buf.begin(), buf.end(), GroupPos()) - buf.begin();
00759 #  else
00760 #    if !defined (_STLP_CVT_DONT_NEED_BUF)
00761   char cvtbuf[NDIG + 2];
00762 #    endif
00763   char * bp;
00764   int decpt, sign;
00765 
00766   switch (flags & ios_base::floatfield) {
00767   case ios_base::fixed:
00768     bp = _Stl_qfcvtR(x, (min) (precision, MAXFCVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00769     break;
00770   case ios_base::scientific:
00771     bp = _Stl_qecvtR(x, (min) (precision + 1, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00772     break;
00773   default :
00774     bp = _Stl_qecvtR(x, (min) (precision, MAXECVT), &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00775     break;
00776   }
00777   return __format_float(buf, bp, decpt, sign, x, flags, precision, true);
00778 #  endif /* USE_SPRINTF_INSTEAD */
00779 }
00780 #endif /* _STLP_NO_LONG_DOUBLE */
00781 
00782 void _STLP_CALL __get_floor_digits(__iostring &out, _STLP_LONGEST_FLOAT_TYPE __x) {
00783 #if defined (USE_SPRINTF_INSTEAD)
00784   char cvtbuf[128];
00785 #  if !defined (_STLP_NO_LONG_DOUBLE)
00786 #    if !defined (N_PLAT_NLM)
00787   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%Lf", __x); // check for 1234.56!
00788 #    else
00789   sprintf(cvtbuf, "%Lf", __x); // check for 1234.56!
00790 #    endif
00791 #  else
00792   snprintf(_STLP_ARRAY_AND_SIZE(cvtbuf), "%f", __x);  // check for 1234.56!
00793 #  endif
00794   char *p = strchr( cvtbuf, '.' );
00795   if ( p == 0 ) {
00796     out.append( cvtbuf );
00797   } else {
00798     out.append( cvtbuf, p );
00799   }
00800 #else
00801 #  if !defined (_STLP_CVT_DONT_NEED_BUF)
00802   char cvtbuf[NDIG + 2];
00803 #  endif
00804   char * bp;
00805   int decpt, sign;
00806 #  if !defined (_STLP_NO_LONG_DOUBLE)
00807   bp = _Stl_qfcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00808 #  else
00809   bp = _Stl_fcvtR(__x, 0, &decpt, &sign _STLP_CVT_BUFFER(cvtbuf));
00810 #  endif
00811 
00812   if (sign) {
00813     out += '-';
00814   }
00815   out.append(bp, bp + decpt);
00816 #endif // USE_PRINTF_INSTEAD
00817 }
00818 
00819 
00820 #if !defined (_STLP_NO_WCHAR_T)
00821 void _STLP_CALL __convert_float_buffer( __iostring const& str, __iowstring &out,
00822                                         const ctype<wchar_t>& ct, wchar_t dot, bool __check_dot)
00823 {
00824   string::const_iterator str_ite(str.begin()), str_end(str.end());
00825 
00826   //First loop, check the dot char
00827   if (__check_dot) {
00828     while (str_ite != str_end) {
00829       if (*str_ite != '.') {
00830         out += ct.widen(*str_ite++);
00831       } else {
00832         out += dot;
00833         break;
00834       }
00835     }
00836   } else {
00837     if (str_ite != str_end) {
00838       out += ct.widen(*str_ite);
00839     }
00840   }
00841 
00842   if (str_ite != str_end) {
00843     //Second loop, dot has been found, no check anymore
00844     while (++str_ite != str_end) {
00845       out += ct.widen(*str_ite);
00846     }
00847   }
00848 }
00849 
00850 #endif
00851 
00852 void _STLP_CALL
00853 __adjust_float_buffer(__iostring &str, char dot) {
00854   if ('.' != dot) {
00855     size_t __dot_pos = str.find('.');
00856     if (__dot_pos != string::npos) {
00857       str[__dot_pos] = dot;
00858     }
00859   }
00860 }
00861 
00862 _STLP_MOVE_TO_STD_NAMESPACE
00863 _STLP_END_NAMESPACE
00864 
00865 // Local Variables:
00866 // mode:C++
00867 // End:



Generated on Mon Mar 10 15:32:17 2008 by  doxygen 1.5.1