CStringEx CTypeUnitDouble::FormatDouble( const double dvalue ) const { int decimal, sign; char *buffer; if( dvalue == 0 ) return( CStringEx( "0.0" )); #ifdef NL_OS_WINDOWS buffer = _fcvt( dvalue, 5, &decimal, &sign ); #else buffer = fcvt( dvalue, 5, &decimal, &sign ); #endif // NL_OS_WINDOWS CStringEx sx( buffer ); if( decimal <= 0 ) { sx = (std::string)(CStringEx( "0." ) + CStringEx( '0', -decimal ) + sx); } else sx.insert(decimal,"."); while( sx[sx.length()-1] == '0' ) sx.left( (int)sx.length() -1 ); if( sx[sx.length()-1] == '.' ) sx += '0'; if( sign ) sx = CStringEx( "-" + sx ); return sx; }
static char * _pfloat(long double r, register char *s, int n, int flags) { register char *s1; int sign, dp; register int i; s1 = _fcvt(r, n, &dp, &sign); if (sign) *s++ = '-'; else if (flags & FL_SIGN) *s++ = '+'; else if (flags & FL_SPACE) *s++ = ' '; if (dp<=0) *s++ = '0'; for (i=dp; i>0; i--) if (*s1) *s++ = *s1++; else *s++ = '0'; if (((i=n) > 0) || (flags & FL_ALT)) *s++ = '.'; while (++dp <= 0) { if (--i<0) break; *s++ = '0'; } while (--i >= 0) if (*s1) *s++ = *s1++; else *s++ = '0'; return s; }
/* replacement of Unix rint() for Windows */ static int rint (double x) { char *buf; int i,dec,sig; buf = _fcvt(x, 0, &dec, &sig); i = atoi(buf); if(sig == 1) { i = i * -1; } return(i); }
void MyProgram::timeBoard(const float timeDelta) { /*m_RunTimerF = (float)this->m_timer.getMilliseconds()/1000; int decimal, sign; char *buffer; buffer = _fcvt(m_RunTimerF,7,&decimal,&sign); String temp(buffer); String subtemp = temp.substr(decimal,3); int t = atoi(subtemp.c_str()); int secondNum; secondNum = 30 - m_RunTimerF - 2;*/ int decimal, sign; time = time-timeDelta; //time = time -(float)this->m_timer.getMilliseconds()/1000; char *buffer; buffer = _fcvt(time,7,&decimal,&sign); String temp(buffer); String uptemp; String subtemp; if(time>=10) { uptemp = temp.substr(0,2); subtemp = temp.substr(decimal,2); } else if(time<10 && time>=1) { uptemp = temp.substr(0,1); subtemp = temp.substr(decimal,2); } else { uptemp = '0'; subtemp = temp.substr(0,2); } if(time<0.01) { gameOver = true; win = false; EleTime->setCaption("TimeLeft: 0:00\n Life: " + Ogre::StringConverter::toString(myPlayer->myHP)); } else EleTime->setCaption("TimeLeft: " + uptemp +":"+ subtemp +"\n Life: " + Ogre::StringConverter::toString(myPlayer->myHP)); //EleTime->setCaption("TimeLeft: " + Ogre::StringConverter::toString(secondNum) +":"+ Ogre::StringConverter::toString(t) +"\n Life: " + Ogre::StringConverter::toString(myPlayer->myHP)); }
char* SosUtil::floatToString(float val, int precision, char* displayString) { int pos1=0,pos2=0; char* buf = _fcvt(val,precision,&pos1,&pos2); char buf2[50] = {0}; int count = 0; if(pos1 < 0) { buf2[0] = '.'; count++; while(pos1 < 0) { pos1++; buf2[count] = '0'; count++; } strcpy(buf2+count,buf); } else if(pos1 > 0) { strncpy(buf2,buf,pos1); count = strlen(buf2); buf2[pos1]='.'; strcpy(buf2 + pos1 +1,buf + pos1); } else { buf2[0]='.'; strcpy(buf2+1,buf); } if(val < 0) { displayString[0] = '-'; strcpy(displayString + 1,buf2); } else { strcpy(displayString,buf2); } return displayString; }
CString InsertDecimal(double dNumber) { if(dNumber <= 0.00) return "0.00"; int decimal, sign; //char* buffer; CString strNumber( _fcvt( dNumber, 2, &decimal, &sign)); if(decimal == 0) strNumber.Insert(0, "0."); else if(decimal == -1) strNumber.Insert(0, "0.0"); else if(decimal < -1) return "0.00"; else strNumber.Insert(decimal, '.'); return strNumber; }
//================================================================================================ //-------------+++--> Format T-Clock's OutPut String From Current Date, Time, & System Information: unsigned MakeFormat(char buf[FORMAT_MAX_SIZE], const char* fmt, SYSTEMTIME* pt, int beat100) //------------------+++--> { const char* bufend = buf+FORMAT_MAX_SIZE; const char* pos; char* out = buf; ULONGLONG TickCount = 0; while(*fmt) { if(*fmt == '"') { for(++fmt; *fmt&&*fmt!='"'; ) *out++ = *fmt++; if(*fmt) ++fmt; continue; } if(*fmt=='\\' && fmt[1]=='n') { fmt+=2; *out++='\n'; } /// for testing else if(*fmt == 'S' && fmt[1] == 'S' && (fmt[2] == 'S' || fmt[2] == 's')) { fmt += 3; out += api.WriteFormatNum(out, (int)pt->wSecond, 2, 0); *out++ = '.'; out += api.WriteFormatNum(out, (int)pt->wMilliseconds, 3, 0); } else if(*fmt == 'y' && fmt[1] == 'y') { int len; len = 2; if(*(fmt + 2) == 'y' && *(fmt + 3) == 'y') len = 4; out += api.WriteFormatNum(out, (len==2)?(int)pt->wYear%100:(int)pt->wYear, len, 0); fmt += len; } else if(*fmt == 'm') { if(*(fmt + 1) == 'm' && *(fmt + 2) == 'e') { *out++ = m_MonthEng[pt->wMonth-1][0]; *out++ = m_MonthEng[pt->wMonth-1][1]; *out++ = m_MonthEng[pt->wMonth-1][2]; fmt += 3; } else if(fmt[1] == 'm' && fmt[2] == 'm') { if(*(fmt + 3) == 'm') { fmt += 4; for(pos=m_MonthLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_MonthShort; *pos; ) *out++=*pos++; } } else { if(fmt[1] == 'm') { fmt += 2; *out++ = (char)((int)pt->wMonth / 10) + '0'; } else { ++fmt; if(pt->wMonth > 9) *out++ = (char)((int)pt->wMonth / 10) + '0'; } *out++ = (char)((int)pt->wMonth % 10) + '0'; } } else if(*fmt == 'a' && fmt[1] == 'a' && fmt[2] == 'a') { if(*(fmt + 3) == 'a') { fmt += 4; for(pos=m_DayOfWeekLong; *pos; ) *out++=*pos++; } else { fmt += 3; for(pos=m_DayOfWeekShort; *pos; ) *out++=*pos++; } } else if(*fmt=='d') { if(fmt[1]=='d' && fmt[2]=='e'){ fmt+=3; for(pos=m_DayOfWeekEng[pt->wDayOfWeek]; *pos; ) *out++=*pos++; }else if(fmt[1]=='d' && fmt[2]=='d') { fmt+=3; if(*fmt=='d'){ ++fmt; pos=m_DayOfWeekLong; }else{ pos=m_DayOfWeekShort; } for(; *pos; ) *out++=*pos++; }else{ if(fmt[1]=='d') { fmt+=2; *out++ = (char)((int)pt->wDay / 10) + '0'; }else{ ++fmt; if(pt->wDay > 9) *out++ = (char)((int)pt->wDay / 10) + '0'; } *out++ = (char)((int)pt->wDay % 10) + '0'; } } else if(*fmt=='h') { int hour = pt->wHour; while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; if(fmt[1] == 'h') { fmt += 2; *out++ = (char)(hour / 10) + '0'; } else { ++fmt; if(hour > 9) *out++ = (char)(hour / 10) + '0'; } *out++ = (char)(hour % 10) + '0'; } else if(*fmt=='H') { if(fmt[1] == 'H') { fmt += 2; *out++ = (char)(pt->wHour / 10) + '0'; } else { ++fmt; if(pt->wHour > 9) *out++ = (char)(pt->wHour / 10) + '0'; } *out++ = (char)(pt->wHour % 10) + '0'; } else if((*fmt=='w'||*fmt=='W') && (fmt[1]=='+'||fmt[1]=='-')) { char is_12h = (*fmt == 'w'); char is_negative = (*++fmt == '-'); int hour = 0; for(; *++fmt<='9'&&*fmt>='0'; ){ hour *= 10; hour += *fmt-'0'; } if(is_negative) hour = -hour; hour = (pt->wHour + hour)%24; if(hour < 0) hour += 24; if(is_12h){ while(hour >= 12) // faster than mod 12 if "hour" <= 24 hour -= 12; if(!hour && !g_bHourZero) hour = 12; } *out++ = (char)(hour / 10) + '0'; *out++ = (char)(hour % 10) + '0'; } else if(*fmt == 'n') { if(fmt[1] == 'n') { fmt += 2; *out++ = (char)((int)pt->wMinute / 10) + '0'; } else { ++fmt; if(pt->wMinute > 9) *out++ = (char)((int)pt->wMinute / 10) + '0'; } *out++ = (char)((int)pt->wMinute % 10) + '0'; } else if(*fmt == 's') { if(fmt[1] == 's') { fmt += 2; *out++ = (char)((int)pt->wSecond / 10) + '0'; } else { ++fmt; if(pt->wSecond > 9) *out++ = (char)((int)pt->wSecond / 10) + '0'; } *out++ = (char)((int)pt->wSecond % 10) + '0'; } else if(*fmt == 't' && fmt[1] == 't') { fmt += 2; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else if(*fmt == 'A' && fmt[1] == 'M') { if(fmt[2] == '/' && fmt[3] == 'P' && fmt[4] == 'M') { if(pt->wHour < 12) *out++ = 'A'; //--+++--> 2010 - Noon / MidNight Decided Here! else *out++ = 'P'; *out++ = 'M'; fmt += 5; } else if(fmt[2] == 'P' && fmt[3] == 'M') { fmt += 4; if(pt->wHour < 12) pos = m_AM; else pos = m_PM; while(*pos) *out++ = *pos++; } else *out++ = *fmt++; } else if(*fmt == 'a' && fmt[1] == 'm' && fmt[2] == '/' && fmt[3] == 'p' && fmt[4] == 'm') { if(pt->wHour < 12) *out++ = 'a'; else *out++ = 'p'; *out++ = 'm'; fmt += 5; } // internet time else if(*fmt == '@' && fmt[1] == '@' && fmt[2] == '@') { fmt += 3; *out++ = '@'; *out++ = (char)(beat100 / 10000) + '0'; *out++ = (char)((beat100 % 10000) / 1000) + '0'; *out++ = (char)((beat100 % 1000) / 100) + '0'; if(*fmt=='.' && fmt[1]=='@') { fmt += 2; *out++ = '.'; *out++ = (char)((beat100 % 100) / 10) + '0'; if(*fmt=='@'){ ++fmt; *out++ = (char)((beat100 % 10)) + '0'; } } } // alternate calendar else if(*fmt == 'Y' && m_AltYear > -1) { int n = 1; while(*fmt == 'Y') { n *= 10; ++fmt; } if(n < m_AltYear) { n = 1; while(n < m_AltYear) n *= 10; } for(;;) { *out++ = (char)((m_AltYear % n) / (n/10)) + '0'; if(n == 10) break; n /= 10; } } else if(*fmt == 'g') { for(pos=m_EraStr; *pos&&*fmt=='g'; ){ char* p2 = CharNextExA(m_codepage, pos, 0); while(pos != p2) *out++ = *pos++; ++fmt; } while(*fmt == 'g') fmt++; } else if(*fmt == 'L' && strncmp(fmt, "LDATE", 5) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 5; } else if(*fmt == 'D' && strncmp(fmt, "DATE", 4) == 0) { GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'T' && strncmp(fmt, "TIME", 4) == 0) { GetTimeFormat(LOCALE_USER_DEFAULT, 0, pt, NULL, out, (int)(bufend-out)); for(; *out; ++out); fmt += 4; } else if(*fmt == 'S') { // uptime int width, padding, num; const char* old_fmt = ++fmt; char specifier = api.GetFormat(&fmt, &width, &padding); if(!TickCount) TickCount = api.GetTickCount64(); switch(specifier){ case 'd'://days num = (int)(TickCount/86400000); break; case 'a'://hours total num = (int)(TickCount/3600000); break; case 'h'://hours (max 24) num = (TickCount/3600000)%24; break; case 'n'://minutes num = (TickCount/60000)%60; break; case 's'://seconds num = (TickCount/1000)%60; break; case 'T':{// ST, uptime as h:mm:ss ULONGLONG past = TickCount/1000; int hour, minute; num = past%60; past /= 60; minute = past%60; past /= 60; hour = (int)past; out += api.WriteFormatNum(out, hour, width, padding); *out++ = ':'; out += api.WriteFormatNum(out, minute, 2, 0); *out++ = ':'; width = 2; padding = 0; break;} default: specifier = '\0'; fmt = old_fmt; *out++ = 'S'; } if(specifier) out += api.WriteFormatNum(out, num, width, padding); } else if(*fmt == 'W') { // Week-of-Year struct tm tmnow; time_t ts = time(NULL); localtime_r(&ts, &tmnow); ++fmt; if(*fmt == 's') { // Week-Of-Year Starts Sunday out += strftime(out, bufend-out, "%U", &tmnow); ++fmt; } else if(*fmt == 'm') { // Week-Of-Year Starts Monday out += strftime(out, bufend-out, "%W", &tmnow); ++fmt; } else if(*fmt == 'i') { // ISO-8601 week (1st version by henriko.se, 2nd by White-Tiger) int wday,borderdays,week; for(;;){ wday = (!tmnow.tm_wday?6:tmnow.tm_wday-1); // convert from Sun-Sat to Mon-Sun (0-5) borderdays = (tmnow.tm_yday + 7 - wday) % 7; // +7 to prevent it from going negative week = (tmnow.tm_yday + 6 - wday) / 7; if(borderdays >= 4){ // year starts with at least 4 days ++week; } else if(!week){ // we're still in last year's week --tmnow.tm_year; tmnow.tm_mon = 11; tmnow.tm_mday = 31; tmnow.tm_isdst = -1; if(mktime(&tmnow)==-1){ // mktime magically updates tm_yday, tm_wday week = 1; break; // fail safe } tmnow.tm_mon = 0; // just to speed up the "if" below, since we know that it can't be week 1 continue; // repeat (once) } if(tmnow.tm_mon==11 && tmnow.tm_mday>=29){ // end of year, could be week 1 borderdays = 31 - tmnow.tm_mday + wday; if(borderdays < 3) week = 1; } break; } out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'u') { int week = 1 + (tmnow.tm_yday + 6 - tmnow.tm_wday) / 7; out += wsprintf(out,"%d",week); ++fmt; } else if(*fmt == 'w') { // SWN (Simple Week Number) out += wsprintf(out,"%d",1 + tmnow.tm_yday / 7); ++fmt; } } //================================================================================================ //======================================= JULIAN DATE Code ======================================== else if(*fmt == 'J' && *(fmt + 1) == 'D') { double y, M, d, h, m, s, bc, JD; struct tm Julian; int id, is, i=0; char* szJulian; time_t UTC = time(NULL); gmtime_r(&UTC, &Julian); y = Julian.tm_year +1900; // Year M = Julian.tm_mon +1; // Month d = Julian.tm_mday; // Day h = Julian.tm_hour; // Hours m = Julian.tm_min; // Minutes s = Julian.tm_sec; // Seconds // This Handles the January 1, 4713 B.C up to bc = 100.0 * y + M - 190002.5; // Year 0 Part. JD = 367.0 * y; JD -= floor(7.0*(y + floor((M+9.0)/12.0))/4.0); JD += floor(275.0*M/9.0); JD += d; JD += (h + (m + s/60.0)/60.0)/24.0; JD += 1721013.5; // BCE 2 November 18 00:00:00.0 UT - Tuesday JD -= 0.5*bc/fabs(bc); JD += 0.5; szJulian = _fcvt(JD, 4, &id, &is); // Make it a String while(*szJulian) { if(i == id) { //--//-++-> id = Decimal Point Precision/Position *out++ = '.'; // ReInsert the Decimal Point Where it Belongs. } else { *out++ = *szJulian++; //--+++--> Done! } i++; } fmt +=2; } //================================================================================================ //======================================= ORDINAL DATE Code ======================================= else if(*fmt == 'O' && *(fmt + 1) == 'D') { //--------+++--> Ordinal Date UTC: char szOD[16] = {0}; struct tm today; time_t UTC = time(NULL); char* od; gmtime_r(&UTC, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'O' && *(fmt + 1) == 'd') { //------+++--> Ordinal Date Local: char szOD[16] = {0}; struct tm today; time_t ts = time(NULL); char* od; localtime_r(&ts, &today); strftime(szOD, 16, "%Y-%j", &today); od = szOD; while(*od) *out++ = *od++; fmt +=2; } //========================================================================== else if(*fmt == 'D' && strncmp(fmt, "DOY", 3) == 0) { //--+++--> Day-Of-Year: char szDoy[8] = {0}; struct tm today; time_t ts = time(NULL); char* doy; localtime_r(&ts, &today); strftime(szDoy, 8, "%j", &today); doy = szDoy; while(*doy) *out++ = *doy++; fmt +=3; } //========================================================================== else if(*fmt == 'P' && strncmp(fmt, "POSIX", 5) == 0) { //-> Posix/Unix Time: char szPosix[16] = {0}; // This will Give the Number of Seconds That Have char* posix; //--+++--> Elapsed Since the Unix Epoch: 1970-01-01 00:00:00 wsprintf(szPosix, "%ld", time(NULL)); posix = szPosix; while(*posix) *out++ = *posix++; fmt +=5; } //========================================================================== else if(*fmt == 'T' && strncmp(fmt, "TZN", 3) == 0) { //--++-> TimeZone Name: #ifndef __GNUC__ /* forces us to link with msvcr100 */ char szTZName[TZNAME_MAX] = {0}; size_t lRet; char* tzn; int iDST; _get_daylight(&iDST); if(iDST) { _get_tzname(&lRet, szTZName, TZNAME_MAX, 1); } else { _get_tzname(&lRet, szTZName, TZNAME_MAX, 0); } tzn = szTZName; while(*tzn) *out++ = *tzn++; #endif fmt +=3; } //================================================================================================= else { for(pos=CharNext(fmt); fmt!=pos; ) *out++=*fmt++; } } *out='\0'; return (unsigned)(out-buf); }
static void test_xcvt(void) { char *str; int i, decpt, sign, err; for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){ decpt = sign = 100; str = _ecvt( test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign); ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15), "_ecvt() bad return, got \n'%s' expected \n'%s'\n", str, test_cvt_testcases[i].expstr_e); ok( decpt == test_cvt_testcases[i].expdecpt_e, "_ecvt() decimal point wrong, got %d expected %d\n", decpt, test_cvt_testcases[i].expdecpt_e); ok( sign == test_cvt_testcases[i].expsign, "_ecvt() sign wrong, got %d expected %d\n", sign, test_cvt_testcases[i].expsign); } for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){ decpt = sign = 100; str = _fcvt( test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign); ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15), "_fcvt() bad return, got \n'%s' expected \n'%s'\n", str, test_cvt_testcases[i].expstr_f); ok( decpt == test_cvt_testcases[i].expdecpt_f, "_fcvt() decimal point wrong, got %d expected %d\n", decpt, test_cvt_testcases[i].expdecpt_f); ok( sign == test_cvt_testcases[i].expsign, "_fcvt() sign wrong, got %d expected %d\n", sign, test_cvt_testcases[i].expsign); } if (p__ecvt_s) { str = malloc(1024); for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){ decpt = sign = 100; err = p__ecvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign); ok(err == 0, "_ecvt_s() failed with error code %d\n", err); ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_e, 15), "_ecvt_s() bad return, got \n'%s' expected \n'%s'\n", str, test_cvt_testcases[i].expstr_e); ok( decpt == test_cvt_testcases[i].expdecpt_e, "_ecvt_s() decimal point wrong, got %d expected %d\n", decpt, test_cvt_testcases[i].expdecpt_e); ok( sign == test_cvt_testcases[i].expsign, "_ecvt_s() sign wrong, got %d expected %d\n", sign, test_cvt_testcases[i].expsign); } free(str); } else win_skip("_ecvt_s not available\n"); if (p__fcvt_s) { int i; str = malloc(1024); /* invalid arguments */ err = p__fcvt_s(NULL, 0, 0.0, 0, &i, &i); ok(err == EINVAL, "got %d, expected EINVAL\n", err); err = p__fcvt_s(str, 0, 0.0, 0, &i, &i); ok(err == EINVAL, "got %d, expected EINVAL\n", err); str[0] = ' '; str[1] = 0; err = p__fcvt_s(str, -1, 0.0, 0, &i, &i); ok(err == 0, "got %d, expected 0\n", err); ok(str[0] == 0, "got %c, expected 0\n", str[0]); ok(str[1] == 0, "got %c, expected 0\n", str[1]); err = p__fcvt_s(str, 1, 0.0, 0, NULL, &i); ok(err == EINVAL, "got %d, expected EINVAL\n", err); err = p__fcvt_s(str, 1, 0.0, 0, &i, NULL); ok(err == EINVAL, "got %d, expected EINVAL\n", err); for( i = 0; strcmp( test_cvt_testcases[i].expstr_e, "END"); i++){ decpt = sign = 100; err = p__fcvt_s(str, 1024, test_cvt_testcases[i].value, test_cvt_testcases[i].nrdigits, &decpt, &sign); ok(err == 0, "_fcvt_s() failed with error code %d\n", err); ok( 0 == strncmp( str, test_cvt_testcases[i].expstr_f, 15), "_fcvt_s() bad return, got '%s' expected '%s'. test %d\n", str, test_cvt_testcases[i].expstr_f, i); ok( decpt == test_cvt_testcases[i].expdecpt_f, "_fcvt_s() decimal point wrong, got %d expected %d\n", decpt, test_cvt_testcases[i].expdecpt_f); ok( sign == test_cvt_testcases[i].expsign, "_fcvt_s() sign wrong, got %d expected %d\n", sign, test_cvt_testcases[i].expsign); } free(str); } else win_skip("_fcvt_s not available\n"); }
static void test_fcvt(void) { char *str; int dec=100, sign=100; /* Numbers less than 1.0 with different precisions */ str = _fcvt(0.0001, 1, &dec, &sign ); ok( 0 == strcmp(str,""), "bad return '%s'\n", str); ok( -3 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.0001, -10, &dec, &sign ); ok( 0 == strcmp(str,""), "bad return '%s'\n", str); ok( -3 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.0001, 10, &dec, &sign ); ok( 0 == strcmp(str,"1000000"), "bad return '%s'\n", str); ok( -3 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); /* Basic sign test */ str = _fcvt(-111.0001, 5, &dec, &sign ); ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 1 == sign, "sign wrong\n"); str = _fcvt(111.0001, 5, &dec, &sign ); ok( 0 == strcmp(str,"11100010"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong\n"); ok( 0 == sign, "sign wrong\n"); /* 0.0 with different precisions */ str = _fcvt(0.0, 5, &dec, &sign ); ok( 0 == strcmp(str,"00000"), "bad return '%s'\n", str); ok( 0 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.0, 0, &dec, &sign ); ok( 0 == strcmp(str,""), "bad return '%s'\n", str); ok( 0 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.0, -1, &dec, &sign ); ok( 0 == strcmp(str,""), "bad return '%s'\n", str); ok( 0 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); /* Numbers > 1.0 with 0 or -ve precision */ str = _fcvt(-123.0001, 0, &dec, &sign ); ok( 0 == strcmp(str,"123"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 1 == sign, "sign wrong\n"); str = _fcvt(-123.0001, -1, &dec, &sign ); ok( 0 == strcmp(str,"12"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 1 == sign, "sign wrong\n"); str = _fcvt(-123.0001, -2, &dec, &sign ); ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 1 == sign, "sign wrong\n"); str = _fcvt(-123.0001, -3, &dec, &sign ); ok( 0 == strcmp(str,""), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 1 == sign, "sign wrong\n"); /* Numbers > 1.0, but with rounding at the point of precision */ str = _fcvt(99.99, 1, &dec, &sign ); ok( 0 == strcmp(str,"1000"), "bad return '%s'\n", str); ok( 3 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); /* Numbers < 1.0 where rounding occurs at the point of precision */ str = _fcvt(0.00636, 2, &dec, &sign ); ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str); ok( -1 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.00636, 3, &dec, &sign ); ok( 0 == strcmp(str,"6"), "bad return '%s'\n", str); ok( -2 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.09999999996, 2, &dec, &sign ); ok( 0 == strcmp(str,"10"), "bad return '%s'\n", str); ok( 0 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); str = _fcvt(0.6, 0, &dec, &sign ); ok( 0 == strcmp(str,"1"), "bad return '%s'\n", str); ok( 1 == dec, "dec wrong %d\n", dec); ok( 0 == sign, "sign wrong\n"); }
inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char*) { return _fcvt((double)x, n, pt, sign); }
inline char* _Stl_fcvtR(double x, int n, int* pt, int* sign, char*) { return _fcvt(x, n, pt, sign); }
bool double_to_char (double number, int precision, char* string, short size) { char *buffer,*temp ; bool rc = true; int decimal_spot, sign, count, current_location = 0, length; *string = 0; temp = _fcvt (number, precision, &decimal_spot, &sign) ; length = strlen(temp); if (length == 0) { return use_gcvt(number,string,size); } if (length > precision) buffer = (char *) malloc (length + 3) ; else buffer = (char *) malloc (precision + 3) ; if (buffer == NULL) return false; /* Add negative sign if required. */ if (sign) buffer [current_location++] = '-' ; /* Place decimal point in the correct location. */ if (decimal_spot > 0) { strncpy (&buffer [current_location], temp, decimal_spot) ; buffer [decimal_spot + current_location] = '.' ; strcpy (&buffer [decimal_spot + current_location + 1], &temp [decimal_spot]) ; } else { buffer [current_location] = '.' ; for(count = current_location; count< abs(decimal_spot)+current_location; count++) buffer [count + 1] = '0' ; strcpy (&buffer [count + 1], temp) ; } rSup(buffer); length = strlen(buffer); if (buffer[0] == '.' || (buffer[0] == '-' && buffer[1] == '.')) length++; if (length>size) rc = use_gcvt(number,string,size); else { if (buffer[0] == '.') { strcpy( string, "0"); strcat( string, buffer); } else if (buffer[0] == '-' && buffer[1] == '.') { strcpy( string, "-0"); strcat( string, &buffer[1]); } else strcpy( string, buffer); } free (buffer); return rc; }
inline char* _Stl_qfcvtR(long double x, int n, int* pt, int* sign, char* buf) { strcpy(buf, _fcvt((double)x, n, pt, sign)); return buf; }
void CBfxFile::_SaveRaw(IFileStore::IOutputStream *pStream, _RgdEntry *pSource, lua_State *Lmap, bool bNumericID, size_t iIndentLvl) { for(size_t i = 0; i < iIndentLvl; ++i) pStream->VWriteString("\t"); //if(iIndentLvl == 1) bNumericID = true; if(!bNumericID && pSource->sName) { pStream->VWriteString(pSource->sName); pStream->VWriteString(" = "); } char sFpBuffer[35]; switch(pSource->Type) { case DT_Float: sprintf((char*)sFpBuffer, "%.5g,\r\n", pSource->Data.f); if(strchr(sFpBuffer,'.')) { int iDec,iSign; char* sDigits = _fcvt(pSource->Data.f, 5, &iDec, &iSign); char* sMyBuf = new char[strlen(sDigits) + 8 + (iDec < 0 ? -iDec : 0) ]; char* sPtr = sMyBuf; if(iSign) { *sPtr = '-'; ++sPtr; } if(iDec <= 0) { *sPtr = '0'; ++sPtr; *sPtr = '.'; ++sPtr; for(int i = 0; i < -iDec; ++i) { *sPtr = '0'; ++sPtr; } iDec = -1; } for(int i = 0; sDigits[i]; ++i) { if(i == iDec) { *sPtr = '.'; ++sPtr; } *sPtr = sDigits[i]; ++sPtr; } *sPtr = ','; ++sPtr; *sPtr = '\r'; ++sPtr; *sPtr = '\n'; ++sPtr; *sPtr = '\0'; ++sPtr; pStream->VWriteString(sMyBuf); delete[] sMyBuf; break; } pStream->VWriteString(sFpBuffer); break; case DT_Integer: sprintf((char*)sFpBuffer, "%lu,\r\n", pSource->Data.i); pStream->VWriteString(sFpBuffer); break; case DT_Bool: sprintf((char*)sFpBuffer, "%s,\r\n", pSource->Data.b ? "true" : "false"); pStream->VWriteString(sFpBuffer); break; case DT_String: { const char *sToPrint = pSource->Data.s; if(Lmap) { lua_pushstring(Lmap, pSource->Data.s); lua_gettable(Lmap, -2); sToPrint = lua_tostring(Lmap, -1); } size_t iL = strlen(sToPrint); for(size_t i = 0; i < iL; ++i) { if(sToPrint[i] == '\\' || sToPrint[i] == '\'' || sToPrint[i] == '\"') goto use_autoescape; } pStream->VWriteString("\""); pStream->VWriteString(sToPrint); pStream->VWriteString("\",\r\n"); if(Lmap) lua_pop(Lmap, 1); break; use_autoescape: //pStream->VWriteString("[["); //pStream->VWriteString(sToPrint); //pStream->VWriteString("]],\r\n"); pStream->VWriteString("\""); for(size_t i = 0; i < iL; ++i) { if(sToPrint[i] == '\\' || sToPrint[i] == '\'' || sToPrint[i] == '\"') { pStream->VWriteString("\\"); } pStream->VWrite(1,1,sToPrint+i); } pStream->VWriteString("\",\r\n"); if(Lmap) lua_pop(Lmap, 1); break; } case DT_WString: pStream->VWriteString("nil -- unicode string\r\n"); break; case DT_Table: case sk_TableInt: { pStream->VWriteString(" \r\n"); for(size_t i = 0; i < iIndentLvl; ++i) pStream->VWriteString("\t"); pStream->VWriteString("{\r\n"); std::vector<_RgdEntry*> vEntries; for(std::vector<_RgdEntry*>::iterator itr = pSource->Data.t->begin(); itr != pSource->Data.t->end(); ++itr) { vEntries.push_back(*itr); } bool bIsNumericT = true; for(size_t i = 0; i < vEntries.size(); ++i) { _RgdEntry* p = vEntries[i]; const char* sT = p->sName; if(p->sName == 0) { bIsNumericT = false; break; } do { if(*sT < '0' || *sT > '9') { bIsNumericT = false; break; } ++sT; }while(*sT); } sort(vEntries.begin(), vEntries.end(), bIsNumericT ? _SortOutEntriesNum : _SortCMetaTableChildren); for(size_t i = 0; i < vEntries.size(); ++i) { _SaveRaw(pStream, vEntries.at(i), Lmap, bIsNumericT, iIndentLvl + 1); } for(size_t i = 0; i < iIndentLvl; ++i) pStream->VWriteString("\t"); if(iIndentLvl == 0) pStream->VWriteString("}\r\n\r\n"); else pStream->VWriteString("},\r\n"); break; } case DT_NoData: pStream->VWriteString("nil -- no data\r\n"); break; }; }