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