int main(int argc, char *argv[]) { endianity(); int32_t data = 256; dmap d; d.data = data; if(d.data == data) { // little endian int32_t cdata = endian_convert(data); printf("Data: %d Big-endian: %d \n", data, cdata); printf("Data Little-endian: %d\n", endian_convert(cdata)); } else { // Big endian int32_t cdata = endian_convert(cdata); printf("Data: %d Little-endian: %d \n", data, cdata); printf("Data Big-endian: %d\n", endian_convert(cdata)); } return 0; }
static inline int check_header(header_t * h, int * correct_endianness){ if( strcmp("pulpo", h -> text) != 0 ) return 5; if( h -> version != 0 ) return 5; /* Check the endianness of integer values and fix header components */ if(h -> one_32 != 1) { endian_convert(4, (char *) &(h -> one_32)); if( h -> one_32 != 1 ) return 5; endian_convert(4, (char *) &(h -> type)); } if(h -> one_64 != 1) { endian_convert(8, (char *) &(h -> one_64)); if( h -> one_64 != 1 ) return 5; endian_convert(8, (char *) &(h -> np)); } /* Check the endianness of floating point values */ *correct_endianness = 0; if ( base_size_of[h -> type] == 4 ) { if(h -> one_f != 1.0) { endian_convert(4, (char *) &(h -> one_f)); if(h -> one_f != 1.0) return 5; *correct_endianness = 1; } } else { if(h -> one_d != 1.0) { endian_convert(8, (char *) &(h -> one_d)); if(h -> one_d != 1.0) return 5; *correct_endianness = 1; } } return 0; }
void FC_FUNC_(read_binary,READ_BINARY) (const int * np, const int * offset, byte * f, int * output_type, int * ierr, STR_F_TYPE fname STR_ARG1) { header_t * h; char * filename; int fd, i; ssize_t moved; int correct_endianness; byte * read_f; TO_C_STR1(fname, filename); fd = open(filename, O_RDONLY); free(filename); if(fd < 0){ *ierr = 2; return; } h = (header_t *) malloc(sizeof(header_t)); assert(h != NULL); /* read header */ moved = read(fd, h, sizeof(header_t)); if ( moved != sizeof(header_t) ) { /* we couldn't read the complete header */ *ierr = 3; return; } *ierr = check_header(h, &correct_endianness); if( *ierr != 0 ) return; /* check whether the sizes match */ if( h->np < *np + *offset ){ *ierr = 4; return; } if( h->type == *output_type){ /* format is the same, we just read */ read_f = f; } else { /*format is not the same, we store into a temporary array */ read_f =(byte *) malloc((*np)*size_of[h->type]); } /* set the start point */ if(*offset != 0) lseek(fd, (*offset)*size_of[h->type], SEEK_CUR); /* now read the values and close the file */ moved = read(fd, read_f, (*np)*size_of[h->type]); if ( moved != (*np)*size_of[h->type]) { /* we couldn't read the whole dataset */ *ierr = 3; return; } close(fd); /* convert endianness */ if(correct_endianness) { for(i=0; i < (*np)*size_of[h->type] ; i+=base_size_of[h->type]) endian_convert(base_size_of[h->type], (char *) (read_f + i)); } /* convert values if it is necessary */ if( h->type != *output_type ){ if(is_integer[h->type] || is_integer[*output_type]){ *ierr = 5; } else { for(i=0; i < *np ; i++) convert( (multi *) (read_f + i*size_of[h->type]), (multi *) (f + i*size_of[*output_type]), h->type, *output_type); free(read_f); /* set the error code according to the conversion done (see src/out_inc.F90 ) */ if ( h->type == TYPE_FLOAT ) *ierr = -1; if ( h->type == TYPE_FLOAT_COMPLEX ) *ierr = -2; if ( h->type == TYPE_DOUBLE ) *ierr = -3; if ( h->type == TYPE_DOUBLE_COMPLEX ) *ierr = -4; } } free(h); }