annotate tools/hdr.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 hdr.c Operations on AIM header.
tomwalters@0 3 -----
tomwalters@0 4
tomwalters@0 5 Usage: hdr [options] [file]
tomwalters@0 6
tomwalters@0 7 Read an AIM output file. If no filename is given then input is expected
tomwalters@0 8 on the stdin. The input from AIM is expected to consist of an AIM header
tomwalters@0 9 followed by some data.
tomwalters@0 10
tomwalters@0 11 Options take the form: <name>=<value>
tomwalters@0 12 and abbreviated option names are recognised provided they are unambiguous
tomwalters@0 13 wrt other names in the input header.
tomwalters@0 14
tomwalters@0 15 The operations are header stripping, querying, or modifying:-
tomwalters@0 16
tomwalters@0 17 1. If no options are given, then strip the header and write the data which
tomwalters@0 18 follows the header on the stdout.
tomwalters@0 19 Eg:
tomwalters@0 20 gensai output=stdout ... | hdr | ...
tomwalters@0 21
tomwalters@0 22 2. An empty value field (ie. <name>=) is interpreted as a query, and the
tomwalters@0 23 corresponding option value found in the header is printed on the stderr.
tomwalters@0 24 Eg., to query the value of option pwidth_aid:
tomwalters@0 25
tomwalters@0 26 hdr pwid= file
tomwalters@0 27
tomwalters@0 28 The special option names are:
tomwalters@0 29
tomwalters@0 30 "all" causes all option values to be printed.
tomwalters@0 31 "format" causes the format of the AIM output file to be printed.
tomwalters@0 32
tomwalters@0 33 3. Each option of the form <name>=<value> is substituted for the corresponding
tomwalters@0 34 option found in the input header. A new header is constructed using the
tomwalters@0 35 input header and all given substitutions, and the new header is written
tomwalters@0 36 on the stdout, followed by the data which follows the input header.
tomwalters@0 37 Eg., to change the value of pwidth_aid to 32ms:
tomwalters@0 38
tomwalters@0 39 hdr pwid=32ms file > file2
tomwalters@0 40
tomwalters@0 41 */
tomwalters@0 42
tomwalters@0 43 #include <stdio.h>
tomwalters@0 44 #include <math.h>
tomwalters@0 45 #include "header.h"
tomwalters@0 46 #include "strmatch.h"
tomwalters@0 47
tomwalters@0 48 char **nameptr, *tmpptr ;
tomwalters@0 49 int *namespn, tmpspn ;
tomwalters@0 50
tomwalters@0 51 main(argc, argv)
tomwalters@0 52 int argc ;
tomwalters@0 53 char *argv[] ;
tomwalters@0 54 {
tomwalters@0 55 FILE *fp=stdin, *fopen() ;
tomwalters@0 56 char *header, *Header(), str[64], *s, c ;
tomwalters@0 57 int i, j, min ;
tomwalters@0 58 int modify=0 ;
tomwalters@0 59 short data ;
tomwalters@0 60
tomwalters@0 61 namespn = ( int *)malloc( (argc-1) * sizeof(int) ) ;
tomwalters@0 62 nameptr = (char **)malloc( (argc-1) * sizeof(char *) ) ;
tomwalters@0 63
tomwalters@0 64 if ( iststr( "-h", argv[argc-1] ) || iststr( "help=", argv[argc-1] ) )
tomwalters@0 65 printhelp();
tomwalters@0 66
tomwalters@0 67 /* search for "=" to get span of each option name, and check option */
tomwalters@0 68
tomwalters@0 69 if ( argc == 1 || strchr( argv[argc-1], '=' ) != (char *)0 )
tomwalters@0 70 fp = stdin ;
tomwalters@0 71 else if ( ( fp = fopen( argv[--argc], "r" ) ) == (FILE *)0 ) {
tomwalters@0 72 fprintf(stderr, "can't open %s\n", argv[argc] ) ;
tomwalters@0 73 exit( 1 ) ;
tomwalters@0 74 }
tomwalters@0 75
tomwalters@0 76 for ( i=1 ; i<argc ; i++ ) {
tomwalters@0 77
tomwalters@0 78 if ( iststr( "-h", argv[argc-1] ) || iststr( "help=", argv[argc-1] ) )
tomwalters@0 79 printhelp();
tomwalters@0 80
tomwalters@0 81 if ( ( namespn[i] = strspnbrk( argv[i], "=" ) ) == ( -1 ) ) {
tomwalters@0 82 fprintf(stderr,"hdr: [%s] incorrect option syntax (<name>=<value>)\n", argv[i] );
tomwalters@0 83 exit( 1 ) ;
tomwalters@0 84 }
tomwalters@0 85 else if ( namespn[i] == strlen( argv[i] ) - 1 ) { /* query */
tomwalters@0 86 argv[i][namespn[i]] = '\0' ;
tomwalters@0 87 namespn[i] = 0 ; /* query denoted by zero span */
tomwalters@0 88 }
tomwalters@0 89 }
tomwalters@0 90
tomwalters@0 91 if ( ( header = ReadHeader( fp ) ) == (char *) 0 ) {
tomwalters@0 92 fprintf(stderr,"hdr: header not found\n");
tomwalters@0 93 exit(1);
tomwalters@0 94 }
tomwalters@0 95
tomwalters@0 96 /* if no options, then just strip header */
tomwalters@0 97
tomwalters@0 98 if ( argc == 1 ) {
tomwalters@0 99 while ( fread(&c, sizeof(char),1,fp) )
tomwalters@0 100 fwrite(&c,sizeof(char),1,stdout);
tomwalters@0 101 fclose( fp ) ;
tomwalters@0 102 exit( 0 ) ;
tomwalters@0 103 }
tomwalters@0 104
tomwalters@0 105
tomwalters@0 106 /* find the position of each option name in the header */
tomwalters@0 107
tomwalters@0 108 for ( i=1 ; i<argc ; i++ ) {
tomwalters@0 109
tomwalters@0 110
tomwalters@0 111 if ( namespn[i] > 0 ) {
tomwalters@0 112 strncpy( str, argv[i], namespn[i] ) ;
tomwalters@0 113 str[namespn[i]] = '\0' ;
tomwalters@0 114 if ( ( nameptr[i] = HeaderStrings( header , str ) ) == (char *)0 ) {
tomwalters@0 115 fprintf(stderr,"hdr: option [%s] ambiguous or not found in header\n", str);
tomwalters@0 116 exit(1);
tomwalters@0 117 }
tomwalters@0 118 modify++ ;
tomwalters@0 119 }
tomwalters@0 120
tomwalters@0 121 else {
tomwalters@0 122 if ( isstr( argv[i], "all" ) )
tomwalters@0 123 WriteHeader( header, stderr ) ;
tomwalters@0 124 else if ( isstr( argv[i], "format" ) )
tomwalters@0 125 WriteFormat( header, stderr ) ;
tomwalters@0 126 else {
tomwalters@0 127 if ( ( s = HeaderValueString( header, argv[i] ) ) == (char *)0 )
tomwalters@0 128 fprintf( stderr,"option %s ambiguous or not found\n", argv[i] ) ;
tomwalters@0 129 else
tomwalters@0 130 fprintf( stderr, "%s=%s\n", HeaderNameString( header, argv[i] ), s ) ;
tomwalters@0 131 }
tomwalters@0 132 }
tomwalters@0 133 }
tomwalters@0 134
tomwalters@0 135 if ( modify ) {
tomwalters@0 136
tomwalters@0 137 /* sort the name lists into the order the names appear in the header */
tomwalters@0 138
tomwalters@0 139 for ( i=1 ; i<argc ; i++ ) {
tomwalters@0 140 if ( namespn[i] > 0 ) {
tomwalters@0 141 min = i ;
tomwalters@0 142 for ( j=i+1 ; j<argc ; j++ ) {
tomwalters@0 143 if ( nameptr[j] < nameptr[min] )
tomwalters@0 144 min = j ;
tomwalters@0 145 }
tomwalters@0 146 if ( min != i ) {
tomwalters@0 147 tmpptr = nameptr[i] ; nameptr[i] = nameptr[min] ; nameptr[min] = tmpptr ;
tomwalters@0 148 tmpptr = argv[i] ; argv[i] = argv[min] ; argv[min] = tmpptr ;
tomwalters@0 149 tmpspn = namespn[i] ; namespn[i] = namespn[min] ; namespn[min] = tmpspn ;
tomwalters@0 150 }
tomwalters@0 151 }
tomwalters@0 152 }
tomwalters@0 153
tomwalters@0 154 header = Header( header, argc, argv ) ;
tomwalters@0 155 WriteHeader( header, stdout );
tomwalters@0 156
tomwalters@0 157 /* echo the rest of the file */
tomwalters@0 158
tomwalters@0 159 while ( fread( &data, sizeof(short), 1, fp ) != NULL )
tomwalters@0 160 fwrite( &data, sizeof(short), 1, stdout) ;
tomwalters@0 161
tomwalters@0 162 }
tomwalters@0 163 fclose( fp ) ;
tomwalters@0 164 }
tomwalters@0 165
tomwalters@0 166
tomwalters@0 167
tomwalters@0 168 /*
tomwalters@0 169 Copy the original header to a new header, changing the ordered options.
tomwalters@0 170 Then update the new header_bytes, and return the new header.
tomwalters@0 171 */
tomwalters@0 172
tomwalters@0 173 char *Header( oldheader, argc, argv )
tomwalters@0 174 char *oldheader ;
tomwalters@0 175 int argc ;
tomwalters@0 176 char *argv[] ;
tomwalters@0 177 {
tomwalters@0 178 char *newheader;
tomwalters@0 179 char *p0, *p1, *p2, *s, str[64];
tomwalters@0 180 int i ;
tomwalters@0 181
tomwalters@0 182 newheader = (char *)malloc( strlen(oldheader) + 64 ) ;
tomwalters@0 183
tomwalters@0 184 p0 = newheader ;
tomwalters@0 185 p1 = oldheader ;
tomwalters@0 186
tomwalters@0 187 /** copy up to each new option **/
tomwalters@0 188
tomwalters@0 189 for ( i=1 ; i<argc ; i++ ) {
tomwalters@0 190
tomwalters@0 191 if ( namespn[i] > 0 ) {
tomwalters@0 192
tomwalters@0 193 p2 = nameptr[i] ;
tomwalters@0 194 while( p1 < p2 )
tomwalters@0 195 *p0++ = *p1++ ;
tomwalters@0 196
tomwalters@0 197 sprintf(str,"%s\n", argv[i] + namespn[i] + 1 );
tomwalters@0 198 for (s = str ; *s != '\n' ; )
tomwalters@0 199 *p0++ = *s++;
tomwalters@0 200 *p0++ = *s;
tomwalters@0 201 while (*p1 != '\n')
tomwalters@0 202 *p1++;
tomwalters@0 203 *p1++;
tomwalters@0 204 }
tomwalters@0 205 }
tomwalters@0 206
tomwalters@0 207 /** copy rest of header **/
tomwalters@0 208
tomwalters@0 209 p2 = HeaderString( oldheader , "Version" ) ;
tomwalters@0 210 while ( p1 < p2 )
tomwalters@0 211 *p0++ = *p1++ ;
tomwalters@0 212
tomwalters@0 213 while (*p1 != '\n')
tomwalters@0 214 *p0++ = *p1++ ;
tomwalters@0 215 *p0++ = *p1++ ;
tomwalters@0 216
tomwalters@0 217
tomwalters@0 218 /** update header_bytes **/
tomwalters@0 219
tomwalters@0 220 sprintf(str, "%0*d", 7, p0-newheader);
tomwalters@0 221 p0 = HeaderString( newheader , "header_bytes" ) ;
tomwalters@0 222 s = str;
tomwalters@0 223 while(*p0 != '\n')
tomwalters@0 224 *p0++ = *s++ ;
tomwalters@0 225
tomwalters@0 226
tomwalters@0 227 return newheader;
tomwalters@0 228 }
tomwalters@0 229
tomwalters@0 230
tomwalters@0 231 WriteFormat( header, fp )
tomwalters@0 232 char *header ;
tomwalters@0 233 FILE *fp ;
tomwalters@0 234 {
tomwalters@0 235 int applic, format, rows, cols, frames ;
tomwalters@0 236
tomwalters@0 237 if ( ( applic = Applic( header) ) < 0 ) {
tomwalters@0 238 fprintf(stderr,"hdr: application name not found in header\n" ) ;
tomwalters@0 239 exit( 1 ) ;
tomwalters@0 240 }
tomwalters@0 241 format = Format( applic ) ;
tomwalters@0 242
tomwalters@0 243 frame_to_matrix( format, &rows, &cols, &frames,
tomwalters@0 244 HeaderInt( header, "frameheight" ),
tomwalters@0 245 HeaderInt( header, "framewidth" ),
tomwalters@0 246 HeaderInt( header, "frames" ) ) ;
tomwalters@0 247
tomwalters@0 248 fprintf( fp, "gen%s output file\n", gen_applics[ applic ] ) ;
tomwalters@0 249
tomwalters@0 250 switch ( format ) {
tomwalters@0 251 case 0 : fprintf( fp, "wave format (array of time points)\n" ) ;
tomwalters@0 252 break ;
tomwalters@0 253 case 1 : fprintf( fp, "activity-pattern format (by columns, lowest centre-frequency first in each)\n" ) ;
tomwalters@0 254 break ;
tomwalters@0 255 case 2 : fprintf( fp, "spectrogram format (by columns, lowest centre-frequency first in each)\n" ) ;
tomwalters@0 256 break ;
tomwalters@0 257 case 3 : fprintf( fp, "excitation-pattern format (array of frequency points per frame)\n" ) ;
tomwalters@0 258 break ;
tomwalters@0 259 case 4 : fprintf( fp, "auditory-image format (by rows, lowest centre-frequency first per frame)\n" ) ;
tomwalters@0 260 break ;
tomwalters@0 261 }
tomwalters@0 262
tomwalters@0 263 fprintf( fp, " rows (channels) = %d per frame\n", rows ) ;
tomwalters@0 264 fprintf( fp, " cols (samples) = %d per frame\n", cols ) ;
tomwalters@0 265 fprintf( fp, " frames = %d\n", frames ) ;
tomwalters@0 266 fprintf( fp, " framesize = %d bytes\n", rows*cols*2 ) ;
tomwalters@0 267
tomwalters@0 268 }
tomwalters@0 269
tomwalters@0 270
tomwalters@0 271 printhelp()
tomwalters@0 272 {
tomwalters@0 273 fprintf(stderr,"hdr: Operations on AIM header.\n");
tomwalters@0 274 fprintf(stderr,"Usage: hdr [options] [file]\n");
tomwalters@0 275 fprintf(stderr,"options comment \n");
tomwalters@0 276 fprintf(stderr,"---------- ---------- \n");
tomwalters@0 277 fprintf(stderr," strip header (when no options given) \n");
tomwalters@0 278 fprintf(stderr,"<name>= query header (for each empty options field) \n");
tomwalters@0 279 fprintf(stderr,"<name>=<value> modify header by substituting given values\n");
tomwalters@0 280 fprintf(stderr,"\n");
tomwalters@0 281 fprintf(stderr,"option names for special queries: \n");
tomwalters@0 282 fprintf(stderr,"all= query all options in header \n");
tomwalters@0 283 fprintf(stderr,"format= query format of AIM output file \n");
tomwalters@0 284 exit( 0 ) ;
tomwalters@0 285 }
tomwalters@0 286