stdutil.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "stdafx.h"
  2. int atoi( const string &s ){
  3. return atoi( s.c_str() );
  4. }
  5. double atof( const string &s ){
  6. return atof( s.c_str() );
  7. }
  8. string itoa( int n ){
  9. char buff[32];itoa( n,buff,10 );
  10. return string( buff );
  11. }
  12. static int _finite( double n ){ // definition: exponent anything but 2047.
  13. int e; // 11 bit exponent
  14. const int eMax = 2047; // 0x7ff, all bits = 1
  15. int *pn = (int *) &n;
  16. e = *++pn; // Intel order!
  17. e = ( e >> 20 ) & eMax;
  18. return e != eMax;
  19. }
  20. static int _isnan( double n ){ // definition: exponent 2047, nonzero fraction.
  21. int e; // 11 bit exponent
  22. const int eMax = 2047; // 0x7ff, all bits = 1
  23. int *pn = (int *) &n;
  24. e = *++pn; // Intel order!
  25. e = ( e >> 20 ) & eMax;
  26. if ( e != 2047 ) return 0; // almost always return here
  27. int fHi, fLo; // 52 bit fraction
  28. fHi = ( *pn ) & 0xfffff; // first 20 bits
  29. fLo = *--pn; // last 32 bits
  30. return ( fHi | fLo ) != 0; // returns 0,1 not just 0,nonzero
  31. }
  32. /////////////
  33. //By FLOYD!//
  34. /////////////
  35. string ftoa( float n ){
  36. static const int digits=6;
  37. int eNeg = -4, ePos = 8; // limits for e notation.
  38. char buffer[50]; // from MSDN example, 25 would probably suffice
  39. string t;
  40. int dec, sign;
  41. if ( _finite( n ) ){
  42. // if ( digits < 1 ) digits = 1; // less than one digit is nonsense
  43. // if ( digits > 8 ) digits = 8; // practical maximum for float
  44. t = _ecvt( n, digits, &dec, &sign );
  45. if ( dec <= eNeg + 1 || dec > ePos ){
  46. _gcvt( n, digits, buffer );
  47. t = buffer;
  48. return t;
  49. }
  50. // Here is the tricky case. We want a nicely formatted
  51. // number with no e-notation or multiple trailing zeroes.
  52. if ( dec <= 0 ){
  53. t = "0." + string( -dec, '0' ) + t;
  54. dec = 1; // new location for decimal point
  55. }
  56. else if( dec < digits ){
  57. t = t.substr( 0, dec ) + "." + t.substr( dec );
  58. }
  59. else{
  60. t = t + string( dec - digits, '0' ) + ".0";
  61. dec += dec - digits;
  62. }
  63. // Finally, trim off excess zeroes.
  64. int dp1 = dec + 1, p = t.length();
  65. while( --p > dp1 && t[p] == '0' );
  66. t = string( t, 0, ++p );
  67. return sign ? "-" + t : t;
  68. } // end of finite case
  69. if ( _isnan( n ) ) return "NaN";
  70. if ( n > 0.0 ) return "Infinity";
  71. if ( n < 0.0 ) return "-Infinity";
  72. abort();
  73. }
  74. /*
  75. string ftoa( float n ){
  76. static const float min=.000001f,max=9999999.0f;
  77. int i=*(int*)&n;
  78. int e=(i>>23)&0xff;
  79. int f=i&0x007fffff;
  80. if( e==0xff && f ) return "NAN";
  81. string t;
  82. int s=(i>>31)&0x01;
  83. if( e==0xff ){
  84. t="INFINITY";
  85. }else if( !e && !f ){
  86. t="0.000000";
  87. }else if( n>=min && n<=max ){
  88. int dec,sgn;
  89. t=_fcvt( fabs(n),6,&dec,&sgn );
  90. if( dec<=0 ){
  91. t="0."+string( -dec,'0' )+t;
  92. }else if( dec<t.size() ){
  93. t=t.substr( 0,dec )+"."+t.substr( dec );
  94. }else{
  95. t=t+string( '0',dec-t.size() )+".000000";
  96. }
  97. }else{
  98. char buff[32];
  99. _gcvt( fabs(n),7,buff );
  100. t=buff;
  101. }
  102. return s ? "-"+t : t;
  103. }
  104. */
  105. string tolower( const string &s ){
  106. string t=s;
  107. for( int k=0;k<t.size();++k ) t[k]=tolower(t[k]);
  108. return t;
  109. }
  110. string toupper( const string &s ){
  111. string t=s;
  112. for( int k=0;k<t.size();++k ) t[k]=toupper(t[k]);
  113. return t;
  114. }
  115. string fullfilename( const string &t ){
  116. char buff[MAX_PATH+1],*p;
  117. GetFullPathName( t.c_str(),MAX_PATH,buff,&p );
  118. return string(buff);
  119. }
  120. string filenamepath( const string &t ){
  121. char buff[MAX_PATH+1],*p;
  122. GetFullPathName( t.c_str(),MAX_PATH,buff,&p );
  123. if( !p ) return "";
  124. *p=0;return string(buff);
  125. }
  126. string filenamefile( const string &t ){
  127. char buff[MAX_PATH+1],*p;
  128. GetFullPathName( t.c_str(),MAX_PATH,buff,&p );
  129. if( !p ) return "";
  130. return string( p );
  131. }