Pelles C forum
C language => Expert questions => Topic started by: akko on March 04, 2007, 08:52:46 AM
-
I know ecvt et al are obsolete and "should be replaced by snprintf"
However that makes them not disappear from existing code.
What can I do?
-
That function is not part of the standard C library. You can either modify the code in a thorough and sensible way, making it use snprintf() or you can write the function ecvt() yourself.
I think that ecvt() is present in glibc:
http://www.gnu.org/software/libc/
although maybe it would be difficult to single that only fragment of code out.
A very quick-and-dirty (and unreliable) implementation I've managed to code, and based on the description I've found here:
http://www.gnu.org/software/libc/manual/html_node/System-V-Number-Conversion.html#System-V-Number-Conversion
is this:
char *ecvt1(double value, int ndigit, int *decpt, int *sign) {
static char buffer[348];
static const int n = sizeof(buffer)/sizeof(buffer[0])-1;
ptrdiff_t pd;
int len;
int zeros=0;
if (ndigit > n) ndigit=n;
while (value > -1.0 && value < 1.0 ) {
value*=10.0;
zeros++;
}
sprintf(buffer,"%.*f",DBL_DIG,value);
len=strlen(buffer);
if (buffer[0]=='-') {
memmove(buffer, buffer+1, --len);
buffer[len]='\0';
*sign=1;
}
else *sign = 0;
pd=strchr(buffer,'.')-buffer;
memmove(buffer+pd,buffer+pd+1,--len-pd);
buffer[len]='\0';
*decpt=(int)pd-zeros;
if (len >= ndigit) buffer[ndigit]='\0';
else {
memset(buffer+len,'0',ndigit-len);
buffer[ndigit]='\0';
}
return buffer;
}
-
I'm on Windows.
The crtdll.dll contains _ecvt.
Could one use that?
-
I think it's possible as long as you have a proper prototype for ecvt() and you are able to call that function from the dll (and I think this implies modifications to your existing code).
I'm not really so much into Windows programming to tell you exactly how to proceed, but you maybe make yourself an idea reading here:
http://msdn2.microsoft.com/en-us/library/1ez7dh12(VS.80).aspx
-
use polib.exe
polib.exe /OUT:crtdll.lib crtdll.dll
or just that function
polib.exe /DEF:crtdll_ecvt.def /OUT:crtdll_ecvt.lib
;crtdll_ecvt.def
LIBRARY crtdll
EXPORTS
_ecvt