tomwalters@0: /* tomwalters@0: hdr.c Operations on AIM header. tomwalters@0: ----- tomwalters@0: tomwalters@0: Usage: hdr [options] [file] tomwalters@0: tomwalters@0: Read an AIM output file. If no filename is given then input is expected tomwalters@0: on the stdin. The input from AIM is expected to consist of an AIM header tomwalters@0: followed by some data. tomwalters@0: tomwalters@0: Options take the form: = tomwalters@0: and abbreviated option names are recognised provided they are unambiguous tomwalters@0: wrt other names in the input header. tomwalters@0: tomwalters@0: The operations are header stripping, querying, or modifying:- tomwalters@0: tomwalters@0: 1. If no options are given, then strip the header and write the data which tomwalters@0: follows the header on the stdout. tomwalters@0: Eg: tomwalters@0: gensai output=stdout ... | hdr | ... tomwalters@0: tomwalters@0: 2. An empty value field (ie. =) is interpreted as a query, and the tomwalters@0: corresponding option value found in the header is printed on the stderr. tomwalters@0: Eg., to query the value of option pwidth_aid: tomwalters@0: tomwalters@0: hdr pwid= file tomwalters@0: tomwalters@0: The special option names are: tomwalters@0: tomwalters@0: "all" causes all option values to be printed. tomwalters@0: "format" causes the format of the AIM output file to be printed. tomwalters@0: tomwalters@0: 3. Each option of the form = is substituted for the corresponding tomwalters@0: option found in the input header. A new header is constructed using the tomwalters@0: input header and all given substitutions, and the new header is written tomwalters@0: on the stdout, followed by the data which follows the input header. tomwalters@0: Eg., to change the value of pwidth_aid to 32ms: tomwalters@0: tomwalters@0: hdr pwid=32ms file > file2 tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "header.h" tomwalters@0: #include "strmatch.h" tomwalters@0: tomwalters@0: char **nameptr, *tmpptr ; tomwalters@0: int *namespn, tmpspn ; tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc ; tomwalters@0: char *argv[] ; tomwalters@0: { tomwalters@0: FILE *fp=stdin, *fopen() ; tomwalters@0: char *header, *Header(), str[64], *s, c ; tomwalters@0: int i, j, min ; tomwalters@0: int modify=0 ; tomwalters@0: short data ; tomwalters@0: tomwalters@0: namespn = ( int *)malloc( (argc-1) * sizeof(int) ) ; tomwalters@0: nameptr = (char **)malloc( (argc-1) * sizeof(char *) ) ; tomwalters@0: tomwalters@0: if ( iststr( "-h", argv[argc-1] ) || iststr( "help=", argv[argc-1] ) ) tomwalters@0: printhelp(); tomwalters@0: tomwalters@0: /* search for "=" to get span of each option name, and check option */ tomwalters@0: tomwalters@0: if ( argc == 1 || strchr( argv[argc-1], '=' ) != (char *)0 ) tomwalters@0: fp = stdin ; tomwalters@0: else if ( ( fp = fopen( argv[--argc], "r" ) ) == (FILE *)0 ) { tomwalters@0: fprintf(stderr, "can't open %s\n", argv[argc] ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: for ( i=1 ; i=)\n", argv[i] ); tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: else if ( namespn[i] == strlen( argv[i] ) - 1 ) { /* query */ tomwalters@0: argv[i][namespn[i]] = '\0' ; tomwalters@0: namespn[i] = 0 ; /* query denoted by zero span */ tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: if ( ( header = ReadHeader( fp ) ) == (char *) 0 ) { tomwalters@0: fprintf(stderr,"hdr: header not found\n"); tomwalters@0: exit(1); tomwalters@0: } tomwalters@0: tomwalters@0: /* if no options, then just strip header */ tomwalters@0: tomwalters@0: if ( argc == 1 ) { tomwalters@0: while ( fread(&c, sizeof(char),1,fp) ) tomwalters@0: fwrite(&c,sizeof(char),1,stdout); tomwalters@0: fclose( fp ) ; tomwalters@0: exit( 0 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: /* find the position of each option name in the header */ tomwalters@0: tomwalters@0: for ( i=1 ; i 0 ) { tomwalters@0: strncpy( str, argv[i], namespn[i] ) ; tomwalters@0: str[namespn[i]] = '\0' ; tomwalters@0: if ( ( nameptr[i] = HeaderStrings( header , str ) ) == (char *)0 ) { tomwalters@0: fprintf(stderr,"hdr: option [%s] ambiguous or not found in header\n", str); tomwalters@0: exit(1); tomwalters@0: } tomwalters@0: modify++ ; tomwalters@0: } tomwalters@0: tomwalters@0: else { tomwalters@0: if ( isstr( argv[i], "all" ) ) tomwalters@0: WriteHeader( header, stderr ) ; tomwalters@0: else if ( isstr( argv[i], "format" ) ) tomwalters@0: WriteFormat( header, stderr ) ; tomwalters@0: else { tomwalters@0: if ( ( s = HeaderValueString( header, argv[i] ) ) == (char *)0 ) tomwalters@0: fprintf( stderr,"option %s ambiguous or not found\n", argv[i] ) ; tomwalters@0: else tomwalters@0: fprintf( stderr, "%s=%s\n", HeaderNameString( header, argv[i] ), s ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: if ( modify ) { tomwalters@0: tomwalters@0: /* sort the name lists into the order the names appear in the header */ tomwalters@0: tomwalters@0: for ( i=1 ; i 0 ) { tomwalters@0: min = i ; tomwalters@0: for ( j=i+1 ; j 0 ) { tomwalters@0: tomwalters@0: p2 = nameptr[i] ; tomwalters@0: while( p1 < p2 ) tomwalters@0: *p0++ = *p1++ ; tomwalters@0: tomwalters@0: sprintf(str,"%s\n", argv[i] + namespn[i] + 1 ); tomwalters@0: for (s = str ; *s != '\n' ; ) tomwalters@0: *p0++ = *s++; tomwalters@0: *p0++ = *s; tomwalters@0: while (*p1 != '\n') tomwalters@0: *p1++; tomwalters@0: *p1++; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: /** copy rest of header **/ tomwalters@0: tomwalters@0: p2 = HeaderString( oldheader , "Version" ) ; tomwalters@0: while ( p1 < p2 ) tomwalters@0: *p0++ = *p1++ ; tomwalters@0: tomwalters@0: while (*p1 != '\n') tomwalters@0: *p0++ = *p1++ ; tomwalters@0: *p0++ = *p1++ ; tomwalters@0: tomwalters@0: tomwalters@0: /** update header_bytes **/ tomwalters@0: tomwalters@0: sprintf(str, "%0*d", 7, p0-newheader); tomwalters@0: p0 = HeaderString( newheader , "header_bytes" ) ; tomwalters@0: s = str; tomwalters@0: while(*p0 != '\n') tomwalters@0: *p0++ = *s++ ; tomwalters@0: tomwalters@0: tomwalters@0: return newheader; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: WriteFormat( header, fp ) tomwalters@0: char *header ; tomwalters@0: FILE *fp ; tomwalters@0: { tomwalters@0: int applic, format, rows, cols, frames ; tomwalters@0: tomwalters@0: if ( ( applic = Applic( header) ) < 0 ) { tomwalters@0: fprintf(stderr,"hdr: application name not found in header\n" ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: format = Format( applic ) ; tomwalters@0: tomwalters@0: frame_to_matrix( format, &rows, &cols, &frames, tomwalters@0: HeaderInt( header, "frameheight" ), tomwalters@0: HeaderInt( header, "framewidth" ), tomwalters@0: HeaderInt( header, "frames" ) ) ; tomwalters@0: tomwalters@0: fprintf( fp, "gen%s output file\n", gen_applics[ applic ] ) ; tomwalters@0: tomwalters@0: switch ( format ) { tomwalters@0: case 0 : fprintf( fp, "wave format (array of time points)\n" ) ; tomwalters@0: break ; tomwalters@0: case 1 : fprintf( fp, "activity-pattern format (by columns, lowest centre-frequency first in each)\n" ) ; tomwalters@0: break ; tomwalters@0: case 2 : fprintf( fp, "spectrogram format (by columns, lowest centre-frequency first in each)\n" ) ; tomwalters@0: break ; tomwalters@0: case 3 : fprintf( fp, "excitation-pattern format (array of frequency points per frame)\n" ) ; tomwalters@0: break ; tomwalters@0: case 4 : fprintf( fp, "auditory-image format (by rows, lowest centre-frequency first per frame)\n" ) ; tomwalters@0: break ; tomwalters@0: } tomwalters@0: tomwalters@0: fprintf( fp, " rows (channels) = %d per frame\n", rows ) ; tomwalters@0: fprintf( fp, " cols (samples) = %d per frame\n", cols ) ; tomwalters@0: fprintf( fp, " frames = %d\n", frames ) ; tomwalters@0: fprintf( fp, " framesize = %d bytes\n", rows*cols*2 ) ; tomwalters@0: tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: printhelp() tomwalters@0: { tomwalters@0: fprintf(stderr,"hdr: Operations on AIM header.\n"); tomwalters@0: fprintf(stderr,"Usage: hdr [options] [file]\n"); tomwalters@0: fprintf(stderr,"options comment \n"); tomwalters@0: fprintf(stderr,"---------- ---------- \n"); tomwalters@0: fprintf(stderr," strip header (when no options given) \n"); tomwalters@0: fprintf(stderr,"= query header (for each empty options field) \n"); tomwalters@0: fprintf(stderr,"= modify header by substituting given values\n"); tomwalters@0: fprintf(stderr,"\n"); tomwalters@0: fprintf(stderr,"option names for special queries: \n"); tomwalters@0: fprintf(stderr,"all= query all options in header \n"); tomwalters@0: fprintf(stderr,"format= query format of AIM output file \n"); tomwalters@0: exit( 0 ) ; tomwalters@0: } tomwalters@0: