Annotation of gforth/ecvt.c, revision 1.1

1.1     ! pazsan      1: #include <sys/time.h>
        !             2: #include <setjmp.h>
        !             3: 
        !             4: jmp_buf throw_jmp_buf;
        !             5: 
        !             6: /* select replacement for DOS computers for ms only */
        !             7: void select(int n, int a, int b, int c, struct timeval * timeout)
        !             8: {
        !             9:    struct timeval time1;
        !            10:    struct timeval time2;
        !            11:    struct timezone zone1;
        !            12: 
        !            13:    gettimeofday(&time1,&zone1);
        !            14:    time1.tv_sec += timeout->tv_sec;
        !            15:    time1.tv_usec += timeout->tv_usec;
        !            16:    if(time1.tv_usec >= 1000000)
        !            17:      {
        !            18:        time1.tv_sec += time1.tv_usec / 1000000;
        !            19:        time1.tv_usec %= 1000000;
        !            20:      }
        !            21:    do
        !            22:      {
        !            23:        gettimeofday(&time2,&zone1);
        !            24:      }
        !            25:    while(time2.tv_sec < time1.tv_sec);
        !            26: 
        !            27:    do
        !            28:      {
        !            29:        gettimeofday(&time2,&zone1);
        !            30:      }
        !            31:    while(time2.tv_usec < time1.tv_usec &&
        !            32:         time2.tv_sec == time1.tv_sec);
        !            33: 
        !            34: }
        !            35: 
        !            36: /* cheap ecvt replacement */
        !            37: 
        !            38: #include <math.h>
        !            39: 
        !            40: #define MAXCONV 0x40
        !            41: char scratch[MAXCONV];
        !            42: 
        !            43: char* ecvt(double x, int len, int* exp, int* sign)
        !            44: {
        !            45:    int i, j;
        !            46:    double z;
        !            47:    
        !            48:    if(len > (MAXCONV-1)) len = MAXCONV-1;
        !            49:    
        !            50:    if(x<0)
        !            51:      {
        !            52:        *sign = 1;
        !            53:        x = -x;
        !            54:      }
        !            55:    else
        !            56:      {
        !            57:        *sign = 0;
        !            58:      }
        !            59:    
        !            60:    if(x==0)
        !            61:      {
        !            62:        *exp=0;
        !            63:        return "0";
        !            64:      }
        !            65:    
        !            66:    *exp=(int)floor(log10(x));
        !            67:    x = x / pow10((double)*exp);
        !            68:    
        !            69:    *exp += 1;
        !            70:    
        !            71:    for(i=0; i < len; i++)
        !            72:      {
        !            73:        z=floor(x);
        !            74:        scratch[i]='0'+(char)((int)z);
        !            75:        x = (x-z)*10;
        !            76:      }
        !            77:    
        !            78:    if((x >= 5) && i)
        !            79:      {
        !            80:        for(j=i-1; j>=0; j--)
        !            81:          {
        !            82:             if(scratch[j]!='9')
        !            83:               {
        !            84:                  scratch[j]+=1; break;
        !            85:               }
        !            86:             else
        !            87:               {
        !            88:                  scratch[j]='0';
        !            89:               }
        !            90:          }
        !            91:        if(j==0)
        !            92:          {
        !            93:             scratch[0]='1';
        !            94:             *exp += 1;
        !            95:          }
        !            96:      }
        !            97:    
        !            98:    scratch[i]='\0';
        !            99:    
        !           100:    return scratch;
        !           101: }
        !           102: 
        !           103: #ifdef TEST
        !           104: int main(int argc, char ** argv)
        !           105: {
        !           106:    int a, b;
        !           107:    char * conv=ecvt(PI*1e10,20,&a,&b);
        !           108:    
        !           109:    printf("ecvt Test: %f -> %s, %d, %d\n",PI,conv,a,b);
        !           110: }
        !           111: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>