#include #include #include "../dans.h" #define DEBUG main(int argc, char *argv[]) { DARRAY *buffer; int file, i, eof, data_type=RE; long rec_length, l; char comments[MAX_COMMENTS]; if (argc <= 1) { printf(" **E** need a filename\n"); return; } if ( (file=open(argv[1],O_RDONLY)) == -1) { printf(" **E** unable to open filename '%s'\n",argv[1]); return; } if (argc > 2) { i=atoi(argv[2]); if (check_range(i,0,MAX_BUFF-1) == FALSE) { printf(" **E** buffer %d out of range 0 to %d, ignored\n",i,MAX_BUFF-1); i=-1; } if (argc > 3) printf(" **W** extra input, '%s' etc. ignored\n",argv[3]); } do { /* keep reading records from file, until EoF */ if ( ( eof = read(file,&rec_length,4) ) != 4) { /* returns 0 at EoF */ if (eof != 0) /* some error, not just an End-of-File */ printf(" **E** could not read record length from file '%s', abort\n",argv[1]); return; } #ifdef DEBUG else printf(" **I** getting %Ld bytes\n",rec_length); #endif /* * know how long the record is, allocate memory and get it in */ if ( (buffer = (DARRAY *) malloc(rec_length) ) == NULL) { printf(" **E** could not allocate %Ld bytes of memory, aborting\n",rec_length); return; } #ifdef DEBUG else printf(" **I** malloc OK\n"); #endif if ( ( i = read(file,buffer,rec_length) ) != rec_length) { printf(" **E** could not read %Ld bytes of file '%s', abort\n",rec_length,argv[1]); free(buffer); return; } #ifdef DEBUG else printf(" **I** read OK\n"); #endif /* * if the first integer is negative, then this is a parameter block. * The fixed part of the header consists of 16 integers followed by 16 reals. */ if ( buffer[0].i < 0) { /* we have a header */ data_type=buffer[2].i; #ifdef DEBUG printf(" **I** Header size: %d 4-byte words\n",abs(2*buffer[0].i)); printf(" **I** No of points: %d\n",buffer[1].i); printf(" **I** Data type: %s\n",type_name[buffer[2].i]); printf(" **I** Data domain: %s\n",domain_name[buffer[3].i]); printf(" **I** Axis type: %s\n",xaxis_name[buffer[4].i]); printf(" **I** SW (Hz): %f\n",buffer[17].f); /* DANS quirk: the 20th field is an integer */ if(strncmp(buffer[12].c ,"DANS",4)) printf(" **I** FT staddress: %d\n",buffer[20].i); #endif if(buffer[12].i != 0) { /* have a comments field */ comments[0] = '\0'; for (i=0; i < buffer[12].i && i < MAX_COMMENTS; i++) strncat(comments,4,buffer[buffer[11].i].c); comments[MAX_COMMENTS] = '\0'; #ifdef DEBUG printf(" **I** Comments:\n[%s]\n",comments); #endif } #ifdef DEBUG else printf(" **I** Comments: none\n"); #endif } else { /* we have a data record */ printf("-------------data[%4d]--------------\n",buffer[0].i); if ( data_type == RE_IM) for (i=1; i < 21; i+=2) printf("%12.5g \t%12.5g\n",buffer[i].f,buffer[i+1].f); else if ( data_type == RE) for (i=1; i < 11; i++) printf("\t%12.5g\n",buffer[i].f); printf("-----------------etc-----------------\n"); } free(buffer); read(file,&l,4); /* trailer must match */ if (l != rec_length) printf(" **W** data in '%s' may be corrupted\n",argv[1]); } while (eof != 0); close(file); }