-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathfileformat.c
407 lines (372 loc) · 14.9 KB
/
fileformat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/** @file
Various utility functions handling file formats.
Copyright (C) 2018 Christian Zuckschwerdt
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include <string.h>
#include <stdlib.h>
#ifdef _MSC_VER
#ifndef strncasecmp // Microsoft Visual Studio
#define strncasecmp _strnicmp
#endif
#else
#include <strings.h>
#endif
//#include "optparse.h"
#include "fileformat.h"
#ifdef _WIN32
#define PATH_SEPARATOR '\\'
#else
#define PATH_SEPARATOR '/'
#endif
char const *file_basename(char const *path)
{
char const *p = strrchr(path, PATH_SEPARATOR);
if (p)
return p + 1;
else
return path;
}
void file_info_clear(file_info_t *info)
{
if (info) {
*info = (file_info_t const){0};
}
}
void file_info_check_read(file_info_t *info)
{
if (info->format != CU8_IQ
&& info->format != CS8_IQ
&& info->format != CS16_IQ
&& info->format != CF32_IQ
&& info->format != S16_AM
&& info->format != PULSE_OOK) {
fprintf(stderr, "File type not supported as input (%s).\n", info->spec);
exit(1);
}
}
void file_info_check_write(file_info_t *info)
{
if (info->format != CU8_IQ
&& info->format != CS8_IQ
&& info->format != S16_AM
&& info->format != S16_FM
&& info->format != CS16_IQ
&& info->format != CF32_IQ
&& info->format != F32_AM
&& info->format != F32_FM
&& info->format != F32_I
&& info->format != F32_Q
&& info->format != U8_LOGIC
&& info->format != VCD_LOGIC) {
fprintf(stderr, "File type not supported as output (%s).\n", info->spec);
exit(1);
}
}
char const *file_info_string(file_info_t *info)
{
switch (info->format) {
case CU8_IQ: return "CU8 IQ (2ch uint8)";
case S16_AM: return "S16 AM (1ch int16)";
case S16_FM: return "S16 FM (1ch int16)";
case CF32_IQ: return "CF32 IQ (2ch float32)";
case CS16_IQ: return "CS16 IQ (2ch int16)";
case F32_AM: return "F32 AM (1ch float32)";
case F32_FM: return "F32 FM (1ch float32)";
case F32_I: return "F32 I (1ch float32)";
case F32_Q: return "F32 Q (1ch float32)";
case VCD_LOGIC: return "VCD logic (text)";
case U8_LOGIC: return "U8 logic (1ch uint8)";
case PULSE_OOK: return "OOK pulse data (text)";
default: return "Unknown";
}
}
char const *file_info_to_sigmf_type(file_info_t *info)
{
switch (info->format) {
case CU8_IQ: return "cu8";
case CS8_IQ: return "ci8";
//case CU16_IQ: return "cu16_le";
case CS16_IQ: return "ci16_le";
//case CU32_IQ: return "cu32_le";
//case CS32_IQ: return "ci32_le";
case CF32_IQ: return "cf32_le";
//case CF64_IQ: return "cf64_le";
//case U8_IQ: return "ru8";
//case S8_IQ: return "ri8";
//case U16_IQ: return "ru16_le";
//case S16_IQ: return "ri16_le";
//case U32_IQ: return "ru32_le";
//case S32_IQ: return "ri32_le";
//case F32_IQ: return "rf32_le";
//case F64_IQ: return "rf64_le";
default: return "Unknown";
}
}
uint32_t file_info_from_sigmf_type(char const *sigmf_datatype)
{
if (!sigmf_datatype) return 0;
else if (!strcmp("cu8", sigmf_datatype)) return F_CU8;
else if (!strcmp("ci8", sigmf_datatype)) return F_CS8;
else if (!strcmp("cu16_le", sigmf_datatype)) return F_CU16;
else if (!strcmp("ci16_le", sigmf_datatype)) return F_CS16;
else if (!strcmp("cu32_le", sigmf_datatype)) return F_CU32;
else if (!strcmp("ci32_le", sigmf_datatype)) return F_CS32;
else if (!strcmp("cf32_le", sigmf_datatype)) return F_CF32;
else if (!strcmp("cf64_le", sigmf_datatype)) return F_CF64;
else if (!strcmp("ru8", sigmf_datatype)) return F_U8;
else if (!strcmp("ri8", sigmf_datatype)) return F_S8;
else if (!strcmp("ru16_le", sigmf_datatype)) return F_U16;
else if (!strcmp("ri16_le", sigmf_datatype)) return F_S16;
else if (!strcmp("ru32_le", sigmf_datatype)) return F_U32;
else if (!strcmp("ri32_le", sigmf_datatype)) return F_S32;
else if (!strcmp("rf32_le", sigmf_datatype)) return F_F32;
else if (!strcmp("rf64_le", sigmf_datatype)) return F_F64;
else return 0;
}
static void file_type_set_format(uint32_t *type, uint32_t val)
{
*type = (*type & 0xffff0000) | val;
}
static void file_type_set_content(uint32_t *type, uint32_t val)
{
*type = (*type & 0x0000ffff) | val;
}
static uint32_t file_type_guess_auto_format(uint32_t type)
{
if (type == 0) return CU8_IQ;
else if (type == F_IQ) return CU8_IQ;
else if (type == F_AM) return S16_AM;
else if (type == F_FM) return S16_FM;
else if (type == F_I) return F32_I;
else if (type == F_Q) return F32_Q;
else if (type == F_LOGIC) return U8_LOGIC;
else if (type == F_CU8) return CU8_IQ;
else if (type == F_CS8) return CS8_IQ;
else if (type == F_S16) return S16_AM;
else if (type == F_U8) return U8_LOGIC;
else if (type == F_VCD) return VCD_LOGIC;
else if (type == F_OOK) return PULSE_OOK;
else if (type == F_CS16) return CS16_IQ;
else if (type == F_CF32) return CF32_IQ;
else return type;
}
static void file_type(char const *filename, file_info_t *info)
{
if (!filename || !*filename) {
return;
}
char const *p = filename;
while (*p) {
if (*p >= '0' && *p <= '9') {
char const *n = p; // number starts here
while (*p >= '0' && *p <= '9')
++p;
if (*p == '.') {
++p;
// if not [0-9] after '.' abort
if (*p < '0' || *p > '9')
continue;
while (*p >= '0' && *p <= '9')
++p;
}
char const *s = p; // number ends and unit starts here
while ((*p >= 'A' && *p <= 'Z')
|| (*p >= 'a' && *p <= 'z'))
++p;
double num = atof(n); // atouint32_metric() ?
size_t len = p - s;
double scale = 1.0;
switch (*s) {
case 'k':
case 'K':
scale *= 1e3;
break;
case 'M':
case 'm':
scale *= 1e6;
break;
case 'G':
case 'g':
scale *= 1e9;
break;
}
if (len == 1 && !strncasecmp("M", s, 1)) info->center_frequency = num * 1e6;
else if (len == 1 && !strncasecmp("k", s, 1)) info->sample_rate = num * 1e3;
else if (len == 2 && !strncasecmp("Hz", s, 2)) info->center_frequency = num;
else if (len == 3 && !strncasecmp("sps", s, 3)) info->sample_rate = num;
else if (len == 3 && !strncasecmp("Hz", s+1, 2) && scale > 1.0) info->center_frequency = num * scale;
else if (len == 4 && !strncasecmp("sps", s+1, 3) && scale > 1.0) info->sample_rate = num * scale;
//fprintf(stderr, "Got number %g, f is %u, s is %u\n", num, info->center_frequency, info->sample_rate);
} else if ((*p >= 'A' && *p <= 'Z')
|| (*p >= 'a' && *p <= 'z')) {
char const *t = p; // type starts here
while ((*p >= '0' && *p <= '9')
|| (*p >= 'A' && *p <= 'Z')
|| (*p >= 'a' && *p <= 'z'))
++p;
size_t len = p - t;
if (len == 1 && !strncasecmp("i", t, 1)) file_type_set_content(&info->format, F_I);
else if (len == 1 && !strncasecmp("q", t, 1)) file_type_set_content(&info->format, F_Q);
else if (len == 2 && !strncasecmp("iq", t, 2)) file_type_set_content(&info->format, F_IQ);
else if (len == 2 && !strncasecmp("am", t, 2)) file_type_set_content(&info->format, F_AM);
else if (len == 2 && !strncasecmp("fm", t, 2)) file_type_set_content(&info->format, F_FM);
else if (len == 2 && !strncasecmp("u8", t, 2)) file_type_set_format(&info->format, F_U8);
else if (len == 2 && !strncasecmp("s8", t, 2)) file_type_set_format(&info->format, F_S8);
else if (len == 3 && !strncasecmp("cu8", t, 3)) file_type_set_format(&info->format, F_CU8);
else if (len == 4 && !strncasecmp("data", t, 4)) file_type_set_format(&info->format, F_CU8); // compat
else if (len == 3 && !strncasecmp("cs8", t, 3)) file_type_set_format(&info->format, F_CS8);
else if (len == 3 && !strncasecmp("u16", t, 3)) file_type_set_format(&info->format, F_U16);
else if (len == 3 && !strncasecmp("s16", t, 3)) file_type_set_format(&info->format, F_S16);
else if (len == 3 && !strncasecmp("u32", t, 3)) file_type_set_format(&info->format, F_U32);
else if (len == 3 && !strncasecmp("s32", t, 3)) file_type_set_format(&info->format, F_S32);
else if (len == 3 && !strncasecmp("f32", t, 3)) file_type_set_format(&info->format, F_F32);
else if (len == 3 && !strncasecmp("vcd", t, 3)) file_type_set_content(&info->format, F_VCD);
else if (len == 3 && !strncasecmp("ook", t, 3)) file_type_set_content(&info->format, F_OOK);
else if (len == 4 && !strncasecmp("cs16", t, 4)) file_type_set_format(&info->format, F_CS16);
else if (len == 4 && !strncasecmp("cs32", t, 4)) file_type_set_format(&info->format, F_CS32);
else if (len == 4 && !strncasecmp("cf32", t, 4)) file_type_set_format(&info->format, F_CF32);
else if (len == 5 && !strncasecmp("cfile", t, 5)) file_type_set_format(&info->format, F_CF32); // compat
else if (len == 5 && !strncasecmp("logic", t, 5)) file_type_set_content(&info->format, F_LOGIC);
else if (len == 10 && !strncasecmp("complex16u", t, 10)) file_type_set_format(&info->format, F_CU8); // compat
else if (len == 10 && !strncasecmp("complex16s", t, 10)) file_type_set_format(&info->format, F_CS8); // compat
else if (len == 7 && !strncasecmp("complex", t, 7)) file_type_set_format(&info->format, F_CF32); // compat
else if (len == 5 && !strncasecmp("sigmf", t, 5)) info->container = FILEFMT_SIGMF;
//else fprintf(stderr, "Skipping type (len %ld) %s\n", len, t);
} else {
p++; // skip non-alphanum char otherwise
}
}
}
// return the last colon not followed by a backslash, otherwise NULL
static char const *last_plain_colon(char const *p)
{
char const *found = NULL;
char const *next = strchr(p, ':');
while (next && next[1] != '\\') {
found = next;
next = strchr(next+1, ':');
}
return found;
}
/**
This will detect file info and overrides.
Parse "[0-9]+(\.[0-9]+)?[A-Za-z]"
as frequency (suffix "M" or "[kMG]?Hz")
or sample rate (suffix "k" or "[kMG]?sps")
Parse "[A-Za-z][0-9A-Za-z]+" as format or content specifier:
2ch formats: "cu8", "cs8", "cs16", "cs32", "cf32"
1ch formats: "u8", "s8", "s16", "u16", "s32", "u32", "f32"
text formats: "vcd", "ook"
content types: "iq", "i", "q", "am", "fm", "logic"
Parses left to right, with the exception of a prefix up to the last colon ":"
This prefix is the forced override, parsed last and removed from the filename.
All matches are case-insensitive.
default detection, e.g.: path/filename.am.s16
overrides, e.g.: am:s16:path/filename.ext
other styles are detected but discouraged, e.g.:
am-s16:path/filename.ext, am.s16:path/filename.ext, path/filename.am_s16
*/
int file_info_parse_filename(file_info_t *info, char const *filename)
{
if (!filename) {
return 0;
}
//if (sigmf_valid_filename(filename)) {
// // just set the container type
// // other info needs to be read later
//}
info->spec = filename;
char const *p = last_plain_colon(filename);
if (p && p - filename < 64) {
size_t len = p - filename;
char forced[64];
memcpy(forced, filename, len);
forced[len] = '\0';
p++;
file_type(p, info);
file_type(forced, info);
info->path = p;
} else {
file_type(filename, info);
info->path = filename;
}
info->raw_format = info->format;
info->format = file_type_guess_auto_format(info->format);
return info->format;
}
// Unit testing
#ifdef _TEST
static void assert_file_type(int check, char const *spec)
{
file_info_t info = {0};
int ret = file_info_parse_filename(&info, spec);
if (check != ret) {
fprintf(stderr, "\nTEST failed: determine_file_type(\"%s\", &foo) = %8x == %8x\n", spec, ret, check);
} else {
fprintf(stderr, ".");
}
}
static void assert_str_equal(char const *a, char const *b)
{
if (a != b && (!a || !b || strcmp(a, b))) {
fprintf(stderr, "\nTEST failed: \"%s\" == \"%s\"\n", a, b);
} else {
fprintf(stderr, ".");
}
}
int main(void)
{
fprintf(stderr, "Testing:\n");
assert_str_equal(last_plain_colon("foo:bar:baz"), ":baz");
assert_str_equal(last_plain_colon("foo"), NULL);
assert_str_equal(last_plain_colon(":foo"), ":foo");
assert_str_equal(last_plain_colon("foo:"), ":");
assert_str_equal(last_plain_colon("foo:bar:C:\\path.txt"), ":C:\\path.txt");
assert_str_equal(last_plain_colon("foo:bar:C:\\path.txt:baz"), ":C:\\path.txt:baz");
assert_file_type(CU8_IQ, "cu8:");
assert_file_type(CS16_IQ, "cs16:");
assert_file_type(CF32_IQ, "cf32:");
assert_file_type(S16_AM, "am:");
assert_file_type(S16_AM, "am.s16:");
assert_file_type(S16_AM, "am-s16:");
assert_file_type(S16_AM, "am_s16:");
assert_file_type(S16_AM, "s16.am:");
assert_file_type(S16_AM, "s16-am:");
assert_file_type(S16_AM, "s16_am:");
assert_file_type(S16_AM, "am-s16.am:");
assert_file_type(S16_FM, "fm:");
assert_file_type(S16_FM, "fm.s16:");
assert_file_type(S16_FM, "fm-s16:");
assert_file_type(S16_FM, "fm_s16:");
assert_file_type(S16_FM, "s16.fm:");
assert_file_type(S16_FM, "s16-fm:");
assert_file_type(S16_FM, "s16_fm:");
assert_file_type(S16_FM, "fm+s16:");
assert_file_type(S16_FM, "s16,fm:");
assert_file_type(CU8_IQ, ".cu8");
assert_file_type(CS16_IQ, ".cs16");
assert_file_type(CF32_IQ, ".cf32");
assert_file_type(S16_AM, ".am");
assert_file_type(S16_AM, ".am.s16");
assert_file_type(S16_AM, ".am-s16");
assert_file_type(S16_AM, ".am_s16");
assert_file_type(S16_AM, ".s16+am");
assert_file_type(S16_AM, ".s16.am");
assert_file_type(S16_AM, ".s16-am");
assert_file_type(S16_AM, ".am-s16.am");
assert_file_type(S16_FM, ".fm");
assert_file_type(S16_FM, ".fm.s16");
assert_file_type(S16_FM, ".fm-s16");
assert_file_type(S16_FM, ".fm_s16");
assert_file_type(S16_FM, ".fm+s16");
assert_file_type(S16_FM, ".s16.fm");
assert_file_type(S16_FM, ".s16-fm");
assert_file_type(S16_FM, ".s16_fm");
assert_file_type(S16_FM, ".s16,fm");
fprintf(stderr, "\nDone!\n");
}
#endif /* _TEST */