Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
cds.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 #include "defs.h"
00035 
00036  char *
00037 #ifdef KR_headers
00038 cds(s, z0)
00039         char *s;
00040         char *z0;
00041 #else
00042 cds(char *s, char *z0)
00043 #endif
00044 {
00045         int ea, esign, et, i, k, nd = 0, sign = 0, tz;
00046         char c, *z;
00047         char ebuf[24];
00048         long ex = 0;
00049         static char etype[Table_size], *db;
00050         static int dblen = 64;
00051 
00052         if (!db) {
00053                 etype['E'] = 1;
00054                 etype['e'] = 1;
00055                 etype['D'] = 1;
00056                 etype['d'] = 1;
00057                 etype['+'] = 2;
00058                 etype['-'] = 3;
00059                 db = Alloc(dblen);
00060                 }
00061 
00062         while((c = *s++) == '0');
00063         if (c == '-')
00064                 { sign = 1; c = *s++; }
00065         else if (c == '+')
00066                 c = *s++;
00067         k = strlen(s) + 2;
00068         if (k >= dblen) {
00069                 do dblen <<= 1;
00070                         while(k >= dblen);
00071                 free(db);
00072                 db = Alloc(dblen);
00073                 }
00074         if (etype[(unsigned char)c] >= 2)
00075                 while(c == '0') c = *s++;
00076         tz = 0;
00077         while(c >= '0' && c <= '9') {
00078                 if (c == '0')
00079                         tz++;
00080                 else {
00081                         if (nd)
00082                                 for(; tz; --tz)
00083                                         db[nd++] = '0';
00084                         else
00085                                 tz = 0;
00086                         db[nd++] = c;
00087                         }
00088                 c = *s++;
00089                 }
00090         ea = -tz;
00091         if (c == '.') {
00092                 while((c = *s++) >= '0' && c <= '9') {
00093                         if (c == '0')
00094                                 tz++;
00095                         else {
00096                                 if (tz) {
00097                                         ea += tz;
00098                                         if (nd)
00099                                                 for(; tz; --tz)
00100                                                         db[nd++] = '0';
00101                                         else
00102                                                 tz = 0;
00103                                         }
00104                                 db[nd++] = c;
00105                                 ea++;
00106                                 }
00107                         }
00108                 }
00109         if (et = etype[(unsigned char)c]) {
00110                 esign = et == 3;
00111                 c = *s++;
00112                 if (et == 1) {
00113                         if(etype[(unsigned char)c] > 1) {
00114                                 if (c == '-')
00115                                         esign = 1;
00116                                 c = *s++;
00117                                 }
00118                         }
00119                 while(c >= '0' && c <= '9') {
00120                         ex = 10*ex + (c - '0');
00121                         c = *s++;
00122                         }
00123                 if (esign)
00124                         ex = -ex;
00125                 }
00126         switch(c) {
00127                 case 0:
00128                         break;
00129 #ifndef VAX
00130                 case 'i':
00131                 case 'I':
00132                         Fatal("Overflow evaluating constant expression.");
00133                 case 'n':
00134                 case 'N':
00135                         Fatal("Constant expression yields NaN.");
00136 #endif
00137                 default:
00138                         Fatal("unexpected character in cds.");
00139                 }
00140         ex -= ea;
00141         if (!nd) {
00142                 if (!z0)
00143                         z0 = mem(4,0);
00144                 strcpy(z0, "-0.");
00145                 sign = 0;
00146                 }
00147         else if (ex > 2 || ex + nd < -2) {
00148                 sprintf(ebuf, "%ld", ex + nd - 1);
00149                 k = strlen(ebuf) + nd + 3;
00150                 if (nd > 1)
00151                         k++;
00152                 if (!z0)
00153                         z0 = mem(k,0);
00154                 z = z0;
00155                 *z++ = '-';
00156                 *z++ = *db;
00157                 if (nd > 1) {
00158                         *z++ = '.';
00159                         for(k = 1; k < nd; k++)
00160                                 *z++ = db[k];
00161                         }
00162                 *z++ = 'e';
00163                 strcpy(z, ebuf);
00164                 }
00165         else {
00166                 k = (int)(ex + nd);
00167                 i = nd + 3;
00168                 if (k < 0)
00169                         i -= k;
00170                 else if (ex > 0)
00171                         i += ex;
00172                 if (!z0)
00173                         z0 = mem(i,0);
00174                 z = z0;
00175                 *z++ = '-';
00176                 if (ex >= 0) {
00177                         for(k = 0; k < nd; k++)
00178                                 *z++ = db[k];
00179                         while(--ex >= 0)
00180                                 *z++ = '0';
00181                         *z++ = '.';
00182                         }
00183                 else {
00184                         for(i = 0; i < k;)
00185                                 *z++ = db[i++];
00186                         *z++ = '.';
00187                         while(++k <= 0)
00188                                 *z++ = '0';
00189                         while(i < nd)
00190                                 *z++ = db[i++];
00191                         }
00192                 *z = 0;
00193                 }
00194         return sign ? z0 : z0+1;
00195         }