Annotation of researchv10no/cmd/bcp/fioi.h, revision 1.1.1.1

1.1       root        1: /* Copyright (c) 1989, 1990 AT&T --- All Rights Reserved.              */
                      2: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T.                */
                      3: /* The copyright notice does not imply actual or intended publication. */
                      4: /* AUTHORS:                                            */
                      5: /*     H. S. Baird - ATT-BL MH - first versions        */
                      6: 
                      7: /* fioi.h - buffered I/O of machine-, OS-, and compiler-independent binary values.
                      8: 
                      9:    Machine-, OS-, and compiler-independence of 'fioi' functions require that:
                     10:       1) chars are exactly 8 bits and their contents are invariant under I/O;
                     11:       2) the order of chars, as written by putc() and read by getc(), is
                     12:         also invariant, across all machines, OS, and compilers.
                     13:    Both of these requirements are customarily met by UNIX OS & tools.
                     14: 
                     15:    All values are written/read as a stream of char's, ultimately
                     16:    using putc() & getc() macroes.
                     17:    To maximize speed, macroes are used wherever possible:
                     18:    -  the first argument F of each macro is a stream (FILE *).
                     19:    -  write macroes have the form fwri_TYPE(F,value); `value' is assigned to
                     20:       a variable of type TYPE before it is written (so compiler-dependent type
                     21:       coercions may apply).
                     22:    - read macroes have the form frdi_TYPE(F), returning a value of type TYPE.
                     23:    Arguments to these macroes are evaluated exactly once.
                     24:    */
                     25: 
                     26: /** MACHINE-DEPENDENT CUSTOMIZATION OF TYPES **/
                     27: /* For each machine, all these types must be supported in the sense that
                     28:    some roughly-equivalent machine-supported type exists.  If an exact
                     29:    equivalent doesn't exist, pick the shortest that is at least as long
                     30:    (in no. bytes), if possible.  If there is no machine representation
                     31:    at least as long as specified, the only penalty will be the inevitable one:
                     32:    when a value is read, it may be truncated to the maximum value that can be
                     33:    represented.
                     34:    */
                     35: #define int1 char              /* 8-bit signed 2's complement integer */
                     36: #define int2 short             /* 16-bit signed 2's complement integer */
                     37: #define int3 int               /* 24-bit signed 2's complement integer */
                     38: #define int4 int               /* 32-bit signed 2's complement integer */
                     39: #define int8 long              /* 64-bit signed 2's complement integer */
                     40: #define uint1 unsigned char    /* 8-bit unsigned integer */
                     41: #define uint2 unsigned short   /* 16-bit unsigned integer */
                     42: #define uint3 unsigned int     /* 24-bit unsigned integer */
                     43: #define uint4 unsigned int     /* 32-bit unsigned integer */
                     44: #define uint8 unsigned long    /* 64-bit unsigned integer */
                     45: 
                     46: /* IEEE 64-bit floating point format */
                     47: typedef struct Ieeed {
                     48:        int8 l;
                     49:        int8 h;
                     50:        } Ieeed;
                     51: 
                     52: /* Cope with unsigned chars on 3B2 & compensate for bug on MIPS */
                     53: #if CPU!=ATT3B && CPU!=MIPS
                     54: #define toint1(c) (int1)(c)
                     55: #else
                     56: #if MAIN
                     57: int toint1(c)
                     58:     register int c;
                     59: {      if ( ((1<<7)&c) !=0 ) return(c-256);
                     60:        else return c;
                     61:        }
                     62: #else
                     63: extern int toint1();
                     64: #endif
                     65: #endif
                     66: 
                     67: /** THE REST OF THIS FILE IS MACHINE-, OS-, and COMPILER-INDEPENDENT **/
                     68: 
                     69: typedef struct Fioi {
                     70:        int1 i1;
                     71:        int2 i2;
                     72:        int3 i3;
                     73:        int4 i4;
                     74:        int8 i8;
                     75:        uint1 ui1;
                     76:        uint2 ui2;
                     77:        uint3 ui3;
                     78:        uint4 ui4;
                     79:        uint8 ui8;
                     80:        char str[100];
                     81:        } Fioi;
                     82: #define Init_Fioi {0,0,0,0,0,0,0,0,0,0,""}
                     83: #if MAIN
                     84: Fioi _Fioi = Init_Fioi;
                     85: #else
                     86: extern Fioi _Fioi;
                     87: #endif
                     88: 
                     89: /* Characters */
                     90: #define fwri_ch(F,V) putc((V),(F))
                     91: #define frdi_ch(F) getc((F))
                     92: 
                     93: #if T  /* TRANSITIONAL:  used to read:  "#if FWRI" */
                     94: /* Strings are '\0'-terminated both in main memory & in peripheral files. */
                     95: #define fwri_str(F,S) { fputs((S),(F)); putc('\0',(F)); }
                     96: #else
                     97: /* Strings are '\0'-terminated in the variable and '\n'-terminated in the file.
                     98:    BAD, OBSOLESCENT POLICY:  both should use '\0'-termination */
                     99: #define fwri_str(F,S) { fputs((S),(F)); putc('\n',(F)); }
                    100: #endif
                    101: 
                    102: #define MAX_FIO_STRLEN (128)
                    103: 
                    104: #if T  /* TRANSITIONAL:  used to read:  "#if FRDI" */
                    105: 
                    106: /* Strings are '\0'-terminated both in main memory & in peripheral files. */
                    107: /*** TRANSITIONAL PATCH:  accept '\n' as a terminator also;  this will be disabled
                    108:      as soon as all classifier tables have been recreated with FWRI==T. ***/
                    109: /* Read string of maximum length MAX_FIO_STRLEN; return pointer to malloc space */
                    110: #if MAIN
                    111: char *frdi_str(f)
                    112:     FILE *f;
                    113: {   static char s[MAX_FIO_STRLEN+1];
                    114:     register int ch;
                    115:     register char *c,*ce;
                    116:     char *res;
                    117:        ce=(c=s)+MAX_FIO_STRLEN;
                    118:        while((c<ce)&&((ch=getc(f))!=EOF)&&(ch!='\0')&&(ch!='\n')) *(c++) = ch;
                    119:        *c='\0';
                    120:        if(c==ce) /* label is truncated; find end of it */ {
                    121:                while(((ch=getc(f))!=EOF)&&(ch!='\0')&&(ch!='\n')) ;
                    122:                };
                    123:        if((res=(char *)strdup(s))==NULL)
                    124:                abort("frdi_str: can't dup char *s[%d]",strlen(s));
                    125:        return(res);
                    126:        }
                    127: #else
                    128: char *frdi_str();
                    129: #endif
                    130: /* 's' is an already-allocated buffer of known length >=N (including '\0') */
                    131: #if MAIN
                    132: int frdi_strn(f,s,n)
                    133:     FILE *f;
                    134:     char s[];
                    135:     int n;
                    136: {   register int ch;
                    137:     register char *c,*ce;
                    138:        ce=(c=s)+n-1;
                    139:        while((c<ce)&&((ch=getc(f))!=EOF)&&(ch!='\0')&&(ch!='\n')) *(c++) = ch;
                    140:        *c='\0';
                    141:        if(c==ce) /* label is truncated; find end of it */ {
                    142:                while(((ch=getc(f))!=EOF)&&(ch!='\0')&&(ch!='\n')) ;
                    143:                };
                    144:        if(ch==EOF) return(0); else if(ferror(f)) return(-errno); else return(1);
                    145:        }
                    146: #else
                    147: int frdi_strn();
                    148: #endif
                    149: 
                    150: #else
                    151: 
                    152: /* Strings are '\0'-terminated in the variable and '\n'-terminated in the file.
                    153:    BAD, OBSOLESCENT POLICY:  both should use '\0'-termination */
                    154: /* Read string of maximum length MAX_FIO_STRLEN; return pointer to malloc space */
                    155: #if MAIN
                    156: char *frdi_str(f)
                    157:     FILE *f;
                    158: {   static char s[MAX_FIO_STRLEN+1];
                    159:     register int ch;
                    160:     register char *c,*ce;
                    161:     char *res;
                    162:        ce=(c=s)+MAX_FIO_STRLEN;
                    163:        while((c<ce)&&((ch=getc(f))!=EOF)&&(ch!='\n')) *(c++) = ch;
                    164:        *c='\0';
                    165:        if(c==ce) /* label is truncated; find end of it */ {
                    166:                while(((ch=getc(f))!=EOF)&&(ch!='\n')) ;
                    167:                };
                    168:        if((res=(char *)strdup(s))==NULL)
                    169:                abort("frdi_str: can't dup char *s[%d]",strlen(s));
                    170:        return(res);
                    171:        }
                    172: #else
                    173: char *frdi_str();
                    174: #endif
                    175: /* S is an already-allocated buffer of known length >=N (including '\0') */
                    176: #define frdi_strn(F,S,N) ( feof(F)? 0 : ( \
                    177:        fgets((S),(N),(F)), \
                    178:        ( (((S)[strlen((S))-1])=='\n')? (S)[strlen((S))-1]='\0': 1 ), \
                    179:        (ferror(F)? -errno: 1) ) )
                    180: 
                    181: #endif
                    182: 
                    183: /* Integers (unsigned and signed) are written low-order byte first. */
                    184: #define fwri_uint1(F,V) fwri_ch((F),(uint1)(V))
                    185: #define frdi_uint1(F) ((uint1)frdi_ch((F)))
                    186: 
                    187: #define fwri_uint2(F,V) { fwri_uint1((F),_Fioi.ui2=(V)); \
                    188:                        fwri_uint1((F),_Fioi.ui2>>8); }
                    189: #define frdi_uint2(F) ((uint2)( _Fioi.ui2=frdi_uint1((F)), \
                    190:                        (frdi_uint1((F))<<8)|_Fioi.ui2 ))
                    191: 
                    192: #define fwri_uint3(F,V) { fwri_uint2((F),_Fioi.ui3=(V)); \
                    193:                        fwri_uint1((F),_Fioi.ui3>>16); }
                    194: #define frdi_uint3(F) ((uint3)( _Fioi.ui3=frdi_uint2((F)), \
                    195:                        (frdi_uint1((F))<<16)|_Fioi.ui3 ))
                    196: 
                    197: #define fwri_uint4(F,V) { fwri_uint2((F),_Fioi.ui4=(V)); \
                    198:                        fwri_uint2((F),_Fioi.ui4>>16); }
                    199: #define frdi_uint4(F) ((uint4)( _Fioi.ui4=frdi_uint2((F)), \
                    200:                        (frdi_uint2((F))<<16)|_Fioi.ui4 ))
                    201: 
                    202: #define fwri_uint8(F,V) { fwri_uint4((F),_Fioi.ui8=(V)); \
                    203:                        fwri_uint4((F),_Fioi.ui8>>32); }
                    204: #define frdi_uint8(F) ((uint8)( _Fioi.ui8=frdi_uint4((F)), \
                    205:                        (frdi_uint4((F))<<32)|_Fioi.ui8 ))
                    206: 
                    207: /* Signed integers are assumed to be 2's complement.
                    208:    Note the suppression of inappropriate sign-extensions using judicious
                    209:    unsigned reads. */
                    210: 
                    211: #define fwri_int1(F,V) fwri_ch((F),_Fioi.i1=(V))
                    212: #define frdi_int1(F) (toint1(frdi_ch((F))))
                    213: 
                    214: #define fwri_int2(F,V) { fwri_int1((F),_Fioi.i2=(V)); \
                    215:                        fwri_int1((F),_Fioi.i2>>8); }
                    216: #define frdi_int2(F) ((int2)( _Fioi.i2=frdi_uint1((F)), \
                    217:                        (frdi_int1((F))<<8)|_Fioi.i2 ))
                    218: 
                    219: #define fwri_int3(F,V) { fwri_int2((F),_Fioi.i3=(V)); \
                    220:                        fwri_int1((F),_Fioi.i3>>16); }
                    221: #define frdi_int3(F) ((int3)( _Fioi.i3=frdi_uint2((F)), \
                    222:                        (frdi_int1((F))<<16)|_Fioi.i3 ))
                    223: 
                    224: #define fwri_int4(F,V) { fwri_int2((F),_Fioi.i4=(V)); \
                    225:                        fwri_int2((F),_Fioi.i4>>16); }
                    226: #define frdi_int4(F) ((int4)( _Fioi.i4=frdi_uint2((F)), \
                    227:                        (frdi_int2((F))<<16)|_Fioi.i4 ))
                    228: 
                    229: #define fwri_int8(F,V) { fwri_int4((F),_Fioi.i8=(V)); \
                    230:                        fwri_int4((F),_Fioi.i8>>32); }
                    231: #define frdi_int8(F) ((int8)( _Fioi.i8=frdi_uint4((F)), \
                    232:                        (frdi_int4((F))<<32)|_Fioi.i8 ))
                    233: 
                    234: 
                    235: /* Floating point */
                    236: #if MAIN
                    237: /* See Marsha Grabow & George Gilmer & KT */
                    238: void
                    239: dtoieeed(ieee,native)
                    240:     Ieeed *ieee;
                    241:     double native;
                    242: {
                    243:        double fr, ho, f;
                    244:        int iexp;
                    245: 
                    246:        if(native < 0) {
                    247:                dtoieeed(ieee, -native);
                    248:                ieee->h |= 0x80000000L;
                    249:                return;
                    250:        }
                    251:        if(native == 0) {
                    252:                ieee->l = 0;
                    253:                ieee->h = 0;
                    254:                return;
                    255:        }
                    256:        fr = frexp(native, &iexp);
                    257:        f = 2097152L;           /* shouldnt use fp constants here */
                    258:        fr = modf(fr*f, &ho);
                    259:        ieee->h = ho;
                    260:        ieee->h &= 0xfffffL;
                    261:        ieee->h |= (iexp+1022L) << 20;
                    262:        f = 65536L;
                    263:        fr = modf(fr*f, &ho);
                    264:        ieee->l = ho;
                    265:        ieee->l <<= 16;
                    266:        ieee->l |= (long)(fr*f);
                    267: }
                    268: #else
                    269: void dtoieeed();
                    270: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.