-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
204 lines (167 loc) · 4.5 KB
/
parse.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
/*
** https://www.mobilefish.com/tutorials/rtttl/rtttl_quickguide_specification.html
*/
#include "errors.h"
#include "mystrings.h"
#include "notes.h"
#include "ringtone.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int count_notes(char *s) {
if (!strlen(s))
return 0;
return count_chars(s, ',') + 1;
}
static int isnotename(int c) {
return ('a' <= c && c <= 'h') || (c == '#') || (c == 'p');
}
static int isdot(int c) { return c == '.'; }
static char *parse_note(char *s, struct ringtone *o, struct note *note) {
int i, j;
struct {
char buf[4];
int (*f)(int);
} items[] = {
{{0, 0, 0, 0}, &isdigit},
{{0, 0, 0, 0}, &isnotename},
{{0, 0, 0, 0}, &isdigit},
{{0, 0, 0, 0}, &isdot},
};
note->raw = s;
i = strlen(s);
if (i > 1 && isdigit(s[i - 1]) && isdot(s[i - 2]))
s[i - 2] = s[i - 1], s[i - 1] = '.';
for (i = 0; i < 4; i++) {
*(items[i].buf) = j = 0;
while (s[j] && items[i].f(s[j]) && j < 3) {
items[i].buf[j] = s[j];
items[i].buf[++j] = '\0';
}
s = s + j;
}
if (*s)
return ERR_NOTEPARSE;
/* dot */
s = items[3].buf;
i = (*s == '\0') || (*s == '.' && *(s + 1) == '\0');
if (!i)
return ERR_NOTEDOT;
j = *s == '.';
/* duration */
s = items[0].buf;
i = (*s == '\0') ? o->duration : atoi(s);
note->type = i, note->dotted = j;
note->duration = get_note_duration(o->bpm, i, j);
if (note->duration == -1.0)
return ERR_NOTEDUR;
note->dur = (uint32_t)(note->duration * (double)SFREQ);
/* octave */
s = items[2].buf;
i = (*s == '\0') ? o->octave : atoi(s);
if (!(4 <= i && i <= 7))
return ERR_NOTEOCT;
note->octave = i;
/* name/index */
s = items[1].buf;
note->index = get_note_index(s);
/* freq/pause */
if (note->index == -1) {
if (!(*s == 'p' && *(s + 1) == '\0'))
return ERR_NOTENAM;
note->steps = note->frequency = 0.0;
} else {
i = note->octave * 12 + note->index + o->transpose;
note->octave = i / 12, note->index = i % 12;
note->frequency = get_index_frequency(i);
}
if (note->frequency > 0.0) {
note->steps = (double)SFREQ / note->frequency;
}
return NULL;
}
static char *parse_notes(char *s, struct ringtone *o) {
char **arr, *error;
int i;
if (!(o->num_notes = count_notes(s)))
return ERR_NONOTES;
if (!(o->notes = calloc(o->num_notes, sizeof(struct note))))
return ERR_MALLOC;
if (!(arr = str_split(s, ','))) {
return ERR_MALLOC;
}
error = NULL;
for (i = 0; arr[i]; i++) {
error = parse_note(arr[i], o, &(o->notes[i]));
if (error)
break;
}
(void)free(arr);
return error;
}
static char *parse_settings(char *s, struct ringtone *o) {
char **arr, *error;
int i, n;
if (count_chars(s, ',') != 2)
return ERR_NUMCOMMAS;
if (!(arr = str_split(s, ',')))
return ERR_MALLOC;
error = NULL;
for (i = 0; !error && i < 3; ++i) {
s = arr[i];
if (!((strlen(s) > 2) && (*(s + 1) == '=') && (is_all_digits(s + 2)))) {
error = ERR_BADKVSETT;
break;
}
n = atoi(s + 2);
switch (*s) {
case 'd':
o->duration = n;
break;
case 'o':
o->octave = n;
break;
case 'b':
o->bpm = n;
break;
default:
error = ERR_BADKVSETT;
}
}
(void)free(arr);
return error;
}
/* returns NULL on success, else error string */
char *parse_ringtone(char *s, struct ringtone *o) {
char *settings, *notes;
if (!(s = copy_without_spaces(s)))
return ERR_MALLOC;
o->name = s;
if (count_chars(s, ':') != 2) {
return ERR_NUMCOLONS;
}
(void)str_tolower(strchr(s, ':'));
o->copy = strdup(s);
settings = strchr(s, ':');
*settings++ = '\0';
notes = strchr(settings, ':');
*notes++ = '\0';
if ((s = parse_settings(settings, o))) {
return s;
}
return parse_notes(notes, o);
}
/* free notes first because "raw" is offset in "name" */
void free_ringtone_data(struct ringtone *o) {
if (o->notes)
(void)free(o->notes);
o->notes = NULL;
if (o->copy)
(void)free(o->copy);
o->copy = NULL;
if (o->name)
(void)free(o->name);
o->name = NULL;
}