comparison ringtools/imy/firstpass.c @ 882:fd4c9bc7835d

fc-imy2pwt program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Apr 2022 03:30:27 +0000
parents
children 0d6814238109
comparison
equal deleted inserted replaced
881:bb8ad7c0cee8 882:fd4c9bc7835d
1 /*
2 * This module implements the first pass of fc-imy2pwt processing:
3 * reading and parsing the iMelody input file at the level of lines,
4 * storing the melody for subsequent processing and catching the
5 * optional BEAT: line, if present.
6 */
7
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include "sizelimits.h"
14
15 extern char *imy_filename;
16 extern char melody_str_buf[MELODY_BUF_SIZE];
17 extern unsigned beats_per_min;
18
19 static char *melody_write_ptr, *melody_write_endp;
20 static int lineno;
21
22 static void
23 copy_melody_str(line)
24 char *line;
25 {
26 char *cp, *np;
27
28 cp = line;
29 while (isspace(*cp))
30 cp++;
31 if (!*cp) {
32 fprintf(stderr, "%s line %d: empty melody line\n",
33 imy_filename, lineno);
34 exit(1);
35 }
36 for (np = cp; *cp && !isspace(*cp); cp++)
37 ;
38 if (*cp)
39 *cp++ = '\0';
40 while (isspace(*cp))
41 cp++;
42 if (*cp) {
43 fprintf(stderr, "%s line %d: single melody string expected\n",
44 imy_filename, lineno);
45 exit(1);
46 }
47 if (strlen(np) > (melody_write_endp - melody_write_ptr)) {
48 fprintf(stderr, "%s line %d: melody buffer size exceeded\n",
49 imy_filename, lineno);
50 exit(1);
51 }
52 strcpy(melody_write_ptr, np);
53 melody_write_ptr += strlen(np);
54 }
55
56 static void
57 process_beat_line(line)
58 char *line;
59 {
60 if (!isdigit(*line)) {
61 fprintf(stderr, "%s line %d: number expected on BEAT: line\n",
62 imy_filename, lineno);
63 exit(1);
64 }
65 beats_per_min = atoi(line);
66 if (beats_per_min < 25 || beats_per_min > 900) {
67 fprintf(stderr, "%s line %d: bpm number is out of range\n",
68 imy_filename, lineno);
69 exit(1);
70 }
71 }
72
73 read_imy_firstpass()
74 {
75 FILE *inf;
76 char linebuf[LINE_BUF_SIZE];
77 int prev_line_is_melody;
78
79 inf = fopen(imy_filename, "r");
80 if (!inf) {
81 perror(imy_filename);
82 exit(1);
83 }
84 melody_write_ptr = melody_str_buf;
85 melody_write_endp = melody_str_buf + MELODY_BUF_SIZE - 1;
86 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
87 if (!index(linebuf, '\n')) {
88 fprintf(stderr,
89 "%s line %d: too long or unterminated\n",
90 imy_filename, lineno);
91 exit(1);
92 }
93 if (linebuf[0] == ' ' || linebuf[0] == '\t') {
94 if (lineno == 1) {
95 fprintf(stderr,
96 "%s line 1: invalid continuation\n",
97 imy_filename);
98 exit(1);
99 }
100 if (prev_line_is_melody)
101 copy_melody_str(linebuf);
102 continue;
103 }
104 if (!strncasecmp(linebuf, "MELODY:", 7)) {
105 copy_melody_str(linebuf + 7);
106 prev_line_is_melody = 1;
107 } else if (!strncasecmp(linebuf, "BEAT:", 5)) {
108 process_beat_line(linebuf + 5);
109 prev_line_is_melody = 0;
110 } else
111 prev_line_is_melody = 0;
112 }
113 fclose(inf);
114 if (!melody_str_buf[0]) {
115 fprintf(stderr, "error: no melody found in %s\n", imy_filename);
116 exit(1);
117 }
118 }