#include #include #include #include #include #include /**********************/ /* Endianness of host */ typedef enum { LITTLE, BIG } ENDIAN; ENDIAN theEndian; // Function void sun_read(ptr, size, nitems, stream) {{{ void sun_read(ptr, size, nitems, stream) char *ptr; int size; int nitems; FILE *stream; { int i, j; for (j = 0; j < nitems; j++) for (i = size - 1; i >= 0; i--) fread(ptr + j * size + i, 1, 1, stream); } // }}} // Function void sun_write(ptr, size, nitems, stream) {{{ void sun_write(ptr, size, nitems, stream) char *ptr; int size; int nitems; FILE *stream; { int i, j; for (j = 0; j < nitems; j++) for (i = size - 1; i >= 0; i--) fwrite(ptr + j * size + i, 1, 1, stream); } // }}} // Function void XGRead(ptr, size, nitems, stream, type) {{{ void XGRead(ptr, size, nitems, stream, type) void *ptr; char *type; FILE *stream; int size, nitems; { #ifdef UNICOS if (!strcmp(type, "float")) unicos_read_float((float *) ptr, nitems, stream); else if (!strcmp(type, "int")) unicos_read_int((int *) ptr, nitems, stream); else if (!strcmp(type, "char")) fread(ptr, size, nitems, stream); else puts("XGRead: unrecognized type"); #else if (theEndian == LITTLE) { fread(ptr, size, nitems, stream); } else { sun_read(ptr, size, nitems, stream); } #endif } // }}} // Function void XGWrite(void *ptr, size, nitems, FILE * stream, char *type) {{{ void XGWrite(void *ptr, int size, int nitems, FILE * stream, char *type) { #ifdef UNICOS if (!strcmp(type, "float")) unicos_write_float((float *) ptr, nitems, stream); else if (!strcmp(type, "int")) unicos_write_int((int *) ptr, nitems, stream); else if (!strcmp(type, "char")) fwrite(ptr, size, nitems, stream); else puts("XGWrite: unrecognized type"); #else if (theEndian == LITTLE) { fwrite(ptr, size, nitems, stream); } else { sun_write(ptr, size, nitems, stream); } #endif } // }}} #ifdef UNICOS // Function unicos_read_float(ptr, nitems, stream) {{{ unicos_read_float(ptr, nitems, stream) float *ptr; FILE *stream; int nitems; { unsigned long *iptr; unsigned char a, b, c, d, t; int j; for (j = 0; j < nitems; j++) { iptr = (unsigned long *) (ptr + j); fread(&d, 1, 1, stream); fread(&c, 1, 1, stream); fread(&b, 1, 1, stream); fread(&a, 1, 1, stream); if (a == 0 && b == 0 && c == 0 && d == 0) *iptr = 0; else { t = ((a << 1) | (b >> 7)); *iptr = ((a >> 7) << 63) | ((t - 126 + 040000) << 48); *iptr |= (b | 0x80) << 40; *iptr |= c << 32; *iptr |= d << 24; } } } // }}} #endif #ifdef UNICOS // Function unicos_read_int(ptr, nitems, stream) {{{ unicos_read_int(ptr, nitems, stream) int *ptr; FILE *stream; int nitems; { unsigned long *iptr; unsigned char a, b, c, d, t; int j; for (j = 0; j < nitems; j++) { iptr = (unsigned long *) (ptr + j); fread(&a, 1, 1, stream); fread(&b, 1, 1, stream); fread(&c, 1, 1, stream); fread(&d, 1, 1, stream); *iptr = d << 24 | c << 16 | b << 8 | a; *iptr |= (d >> 7) ? 0xffffffff00000000 : 0x0; } } // }}} #endif #ifdef UNICOS // Function unicos_write_float(ptr, nitems, stream) {{{ float *ptr; FILE *stream; int nitems; { unsigned long *iptr; unsigned char a, b, c, d, t; int j; for (j = 0; j < nitems; j++) { iptr = (unsigned long *) (ptr + j); t = ((*iptr >> 48) & 0xff) - 040000 + 126; a = ((*iptr & (1 << 63)) ? 1 << 7 : 0) | t >> 1; b = (t & 0x1) << 7 | ((*iptr >> 40) & 0x7f); c = *iptr >> 32; d = *iptr >> 24; fwrite(&d, 1, 1, stream); fwrite(&c, 1, 1, stream); fwrite(&b, 1, 1, stream); fwrite(&a, 1, 1, stream); } } // }}} #endif #ifdef UNICOS // Function unicos_write_int(ptr, nitems, stream) {{{ unicos_write_int(ptr, nitems, stream) int *ptr; FILE *stream; int nitems; { unsigned long *iptr; unsigned char a, b, c, d, t; int j; for (j = 0; j < nitems; j++) { iptr = (unsigned long *) (ptr + j); a = *iptr & 0xff; b = *iptr >> 8 & 0xff; c = *iptr >> 16 & 0xff; d = *iptr >> 24 & 0xff; fwrite(&a, 1, 1, stream); fwrite(&b, 1, 1, stream); fwrite(&c, 1, 1, stream); fwrite(&d, 1, 1, stream); } } // }}} #endif // Modeline for ViM {{{1 // vim:set ts=4: // vim600:fdm=marker fdl=0 fdc=3: // 1}}}