NO

Author Topic: ecvt missing  (Read 3553 times)

akko

  • Guest
ecvt missing
« 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?

cane

  • Guest
ecvt missing
« Reply #1 on: March 04, 2007, 08:58:40 pm »
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:

Code: [Select]

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;
}

akko

  • Guest
ecvt missing
« Reply #2 on: March 05, 2007, 06:02:52 pm »
I'm on Windows.
The crtdll.dll contains _ecvt.
Could one use that?

cane

  • Guest
ecvt missing
« Reply #3 on: March 05, 2007, 06:29:44 pm »
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

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1845
ecvt missing
« Reply #4 on: March 05, 2007, 09:52:49 pm »
use polib.exe

polib.exe /OUT:crtdll.lib crtdll.dll

or just that function

polib.exe /DEF:crtdll_ecvt.def /OUT:crtdll_ecvt.lib


Code: [Select]
;crtdll_ecvt.def
LIBRARY crtdll

EXPORTS
_ecvt
May the source be with you