FreeCalypso > hg > freecalypso-tools
comparison librftab/rftablerd.c @ 314:a0f79bba0ad8
librftab: reading of Tx ramp template files split from rftablerd module
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 22 Nov 2017 18:12:04 +0000 |
parents | 64bb50fc470f |
children | 059649902c7f |
comparison
equal
deleted
inserted
replaced
313:a9bd4b15f502 | 314:a0f79bba0ad8 |
---|---|
10 #include <string.h> | 10 #include <string.h> |
11 #include <strings.h> | 11 #include <strings.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include "../rvinterf/include/exitcodes.h" | 13 #include "../rvinterf/include/exitcodes.h" |
14 | 14 |
15 #define MAX_FIELDS_PER_LINE 64 | 15 #include "rdcommon.c" |
16 | 16 |
17 static char *filename; | |
18 static FILE *rdfile; | |
19 static unsigned lineno; | |
20 static char linebuf[256], *line_fields[MAX_FIELDS_PER_LINE]; | |
21 static unsigned line_nfields, line_field_ptr; | |
22 static char *format; | 17 static char *format; |
23 static u_char *writeptr; | 18 static u_char *writeptr; |
24 static unsigned written_size, maxsize; | 19 static unsigned written_size, maxsize; |
25 | |
26 static int | |
27 read_line() | |
28 { | |
29 char *cp; | |
30 | |
31 if (!fgets(linebuf, sizeof linebuf, rdfile)) | |
32 return(0); | |
33 lineno++; | |
34 cp = linebuf; | |
35 for (line_nfields = 0; ; ) { | |
36 while (isspace(*cp)) | |
37 cp++; | |
38 if (*cp == '\0' || *cp == '#') | |
39 break; | |
40 if (line_nfields >= MAX_FIELDS_PER_LINE) { | |
41 printf("%s line %d: too many fields on one line\n", | |
42 filename, lineno); | |
43 return(-1); | |
44 } | |
45 line_fields[line_nfields++] = cp; | |
46 while (*cp && !isspace(*cp)) | |
47 cp++; | |
48 if (*cp) | |
49 *cp++ = '\0'; | |
50 } | |
51 return(1); | |
52 } | |
53 | |
54 static | |
55 get_field(retp) | |
56 char **retp; | |
57 { | |
58 int rc; | |
59 | |
60 if (line_field_ptr < line_nfields) { | |
61 *retp = line_fields[line_field_ptr++]; | |
62 return(1); | |
63 } | |
64 do { | |
65 rc = read_line(); | |
66 if (rc <= 0) | |
67 return(rc); | |
68 } while (!line_nfields); | |
69 *retp = line_fields[0]; | |
70 line_field_ptr = 1; | |
71 return(1); | |
72 } | |
73 | 20 |
74 static | 21 static |
75 process_number(size) | 22 process_number(size) |
76 { | 23 { |
77 char *field; | 24 char *field; |
421 fclose(rdfile); | 368 fclose(rdfile); |
422 if (size_ret) | 369 if (size_ret) |
423 *size_ret = written_size; | 370 *size_ret = written_size; |
424 return(rc); | 371 return(rc); |
425 } | 372 } |
426 | |
427 static | |
428 process_txramp_number() | |
429 { | |
430 char *field; | |
431 int rc; | |
432 u_long number; | |
433 | |
434 rc = get_field(&field); | |
435 if (rc < 0) | |
436 return(ERROR_USAGE); | |
437 if (!rc) { | |
438 printf("error: %s is too short for a Tx ramp template\n", | |
439 filename); | |
440 return(ERROR_USAGE); | |
441 } | |
442 number = strtoul(field, 0, 0); | |
443 *writeptr++ = number; | |
444 return(0); | |
445 } | |
446 | |
447 static | |
448 read_txramp_16num() | |
449 { | |
450 int i, rc; | |
451 | |
452 for (i = 0; i < 16; i++) { | |
453 rc = process_txramp_number(); | |
454 if (rc) | |
455 return(rc); | |
456 } | |
457 return(0); | |
458 } | |
459 | |
460 read_tx_ramp_template(filename_arg, rdbuf) | |
461 char *filename_arg; | |
462 u_char *rdbuf; | |
463 { | |
464 char *field; | |
465 int rc; | |
466 | |
467 filename = filename_arg; | |
468 rdfile = fopen(filename, "r"); | |
469 if (!rdfile) { | |
470 perror(filename); | |
471 return(ERROR_UNIX); | |
472 } | |
473 lineno = 0; | |
474 line_nfields = 0; | |
475 rc = get_field(&field); | |
476 if (rc <= 0) { | |
477 not_valid_ramp_file: | |
478 printf("error: %s is not a valid Tx ramp template file\n", | |
479 filename); | |
480 fclose(rdfile); | |
481 return(ERROR_USAGE); | |
482 } | |
483 if (strcmp(field, "ramp-up")) | |
484 goto not_valid_ramp_file; | |
485 writeptr = rdbuf; | |
486 rc = read_txramp_16num(); | |
487 if (rc) { | |
488 fclose(rdfile); | |
489 return(rc); | |
490 } | |
491 rc = get_field(&field); | |
492 if (rc <= 0) | |
493 goto not_valid_ramp_file; | |
494 if (strcmp(field, "ramp-down")) | |
495 goto not_valid_ramp_file; | |
496 rc = read_txramp_16num(); | |
497 if (rc) { | |
498 fclose(rdfile); | |
499 return(rc); | |
500 } | |
501 rc = get_field(&field); | |
502 fclose(rdfile); | |
503 if (rc < 0) | |
504 return(ERROR_USAGE); | |
505 if (rc) { | |
506 printf("error: %s is too long for a Tx ramp template\n", | |
507 filename); | |
508 return(ERROR_USAGE); | |
509 } | |
510 return(0); | |
511 } |