-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnlParse.c
executable file
·976 lines (777 loc) · 26.1 KB
/
nlParse.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* NotelistParser, version 2.4
Sample parser for Nightingale's notelist files. Reads input from stdin and
writes output to stdout (on Macintosh, using Symantec C's ccommand dialog interface).
For each valid line, fills in fields in a struct, which it does nothing with other
than writing out. The effect is to "canonize" notelists by (1) converting older
formats to the current format and (2) adding explicit default values for omitted
fields. However, it could easily be used as the basis for programs to process
notelists in various ways, especially to modify them for compositional, analytic,
or other purposes.
Original version by John Gibson, 1992; revisions by Donald Byrd, 1997-98.
Revisions by Donald Byrd (with Tim Crawford), Oct. 2000 - September 2001:
- Make more portable (across platforms)
- Optionally use an input file given on the command line
- Improve error handling
- Parse _every_ field of every line
- Unless SUMMARY_ONLY is #defined, echo input from the parsed lines
- Ignore empty lines
- Make it handle notelist files from all platforms (handle all newline sequences)
Ported to CodeWarrior 5.3 by Donald Byrd, August 2002.
Further improvements from NotelistModify ported to this program, May 2003; the most
important is fixing a memory-management bug that caused crashes on notes with "mod=".
NB: Tab stops in this file should be set to 3 spaces. */
#include "nlParse.h"
/* Formats of lines in a Notelist file:
Note N t=%ld v=%d npt=%d stf=%d dur=%d dots=%d nn=%d acc=%d eAcc=%d pDur=%d vel=%d %s appear=%d
Rest R t=%ld v=%d npt=%d stf=%d dur=%d dots=%d %s appear=%d
Grace note G t=-1 v=%d npt=%d stf=%d dur=%d dots=%d nn=%d acc=%d eAcc=%d pDur=%d vel=%d %c appear=%d
Barline / t=%ld type=%d
Clef C stf=%d type=%d
Key signature K stf=%d KS=%d %c
Time signature T stf=%d num=%d denom=%d
Dynamic D stf=%d type=%d
Text A v=%d npt=%d stf=%d %c '%s'
Tempo mark M stf=%d '%s' %c[.]=%s
Tuplet P v=%d npt=%d num=%d denom=%d appear=%d
Comment % (anything; lines beginning "%%" should be structured comments)
*/
int main(int, char **);
void printfXMLComment(char *comment);
void ParseAndProcessNotelist(FILE *file);
Boolean ParseAndProcessClef(void);
Boolean ParseAndProcessKeySig(void);
Boolean ParseAndProcessTimeSig(void);
Boolean ParseNote(ANOTE *pNote);
Boolean ParseAndProcessNote(void);
Boolean ParseRest(AREST *pRest);
Boolean ParseAndProcessRest(void);
Boolean ParseAndProcessBarline(void);
Boolean ParseAndProcessTuplet(void);
Boolean ParseAndProcessDynamic(void);
Boolean ParseAndProcessText(void);
Boolean ParseAndProcessTempoMark(void);
Boolean ParseAndProcessBeam(void);
char gInBuf[MAX_LINELENGTH];
short gErrorCount;
short gNotelistVersion;
short gTiedNoteCount;
short gLineCount;
short gClefCount = 0;
short gKeySigCount = 0;
short gTimeSigCount = 0;
short gNoteCount = 0;
short gGraceNoteCount = 0;
short gRestCount = 0;
short gBarlineCount = 0;
short gTupletCount = 0;
short gDynamicCount = 0;
short gTextCount = 0;
short gTempoCount = 0;
short gBeamCount = 0;
char gFilename[64];
short gPartStaves[MAXPARTS+1]; /* 1-based array giving number of staves (value) in each part (index) */
short gNumNLStaves; /* number of staves in Notelist system */
short gFirstMNNumber; /* number of first measure */
Boolean gDelAccs; /* delete redundant accidentals? */
static Boolean ParseAndProcessStructComment(void);
/************************************************************************************/
int main(int argc, char **argv)
{
FILE *theFile;
#ifdef THINK_C
/* Adjust size (default is 25 rows of 80 chars) and placement of console window */
console_options.top = 50; console_options.left = 10;
console_options.nrows = 40; console_options.ncols = 90;
#endif
#if defined(THINK_C) || defined(__MWERKS__)
argc = ccommand(&argv); /* provides Unix-like redirection of stdin and stdout */
#endif
(void)Initialize();
if (argc>1)
theFile = fopen(argv[1], "r");
else
theFile = stdin;
if (!theFile) {
STATUS_PRINTF("ERROR: can't open the input file.\n");
return 1;
}
/*
printf ( "%% NotelistParser 2.4 - sample Nightingale Notelist parser - version of 18 May 2003\n\n");
*/
ParseAndProcessNotelist(theFile);
fclose(theFile); /* almost certainly unnecessary */
(void)CleanUp();
return 0;
}
void ParseAndProcessNotelist(FILE *file)
{
char firstChar;
if (!fgetsAllPlatform(gInBuf, MAX_LINELENGTH, file)) {
STATUS_PRINTF("ERROR: can't read first line of notelist.\n");
return;
}
gNotelistVersion = NotelistVersion();
if (gNotelistVersion<0) return;
if (!ParseAndProcessStructComment()) {
STATUS_PRINTF("FATAL ERROR (ParseAndProcessNotelist): problem with notelist header line.\n");
return;
}
gLineCount = 1;
/* parse successive lines of input file */
while (fgetsAllPlatform(gInBuf, MAX_LINELENGTH, file)) {
gLineCount++;
/* dispatch to specific parsing routines depending on first char */
sscanf(gInBuf, "%c", &firstChar);
if (firstChar=='\n' || firstChar=='\r') continue; /* Ignore empty lines */
switch (firstChar) {
case CLEF_CHAR:
if (!ParseAndProcessClef()) gErrorCount++;
break;
case KEYSIG_CHAR:
if (!ParseAndProcessKeySig()) gErrorCount++;
break;
case TIMESIG_CHAR:
if (!ParseAndProcessTimeSig()) gErrorCount++;
break;
case NOTE_CHAR:
case GRACE_CHAR:
if (!ParseAndProcessNote()) gErrorCount++;
break;
case REST_CHAR:
if (!ParseAndProcessRest()) gErrorCount++;
break;
case BAR_CHAR:
if (!ParseAndProcessBarline()) gErrorCount++;
break;
case TUPLET_CHAR:
if (!ParseAndProcessTuplet()) gErrorCount++;
break;
case DYNAMIC_CHAR:
if (!ParseAndProcessDynamic()) gErrorCount++;
break;
case GRAPHIC_CHAR:
if (!ParseAndProcessText()) gErrorCount++;
break;
case TEMPO_CHAR:
if (!ParseAndProcessTempoMark()) gErrorCount++;
break;
case BEAM_CHAR:
if (!ParseAndProcessBeam()) gErrorCount++;
break;
case '*': /* it's a comment (long-obsolete form) */
case COMMENT_CHAR: /* it's a comment */
CopyLine();
break;
default:
ReportParseFailure("ParseAndProcessNotelist");
gErrorCount++;
}
if (gErrorCount>25) {
STATUS_PRINTF("FATAL ERROR (ParseAndProcessNotelist): TOO MANY ERRORS.\n");
return;
}
}
}
/* Parse a line like this:
* C stf=1 type=2
*/
Boolean ParseAndProcessClef()
{
char stfStr[32], typeStr[32];
short count;
long along;
ACLEF clef;
count = sscanf(gInBuf, "%*c%s%s", stfStr, typeStr);
if (count<2) goto Broken;
if (!ExtractLong(stfStr, &along)) goto Broken;
clef.stf = along;
if (!ExtractLong(typeStr, &along)) goto Broken;
clef.type = along;
/* Now handle the clef as needed. */
ProcessClef(clef);
gClefCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessClef");
return FALSE;
}
/* Parse a line like this:
* K stf=1 KS=2 #
*/
Boolean ParseAndProcessKeySig()
{
char stfStr[32], numAccStr[32], shpFltStr[32];
short count;
long along;
AKEYSIG keySig;
count = sscanf(gInBuf, "%*c%s%s%s", stfStr, numAccStr, shpFltStr);
if (count<3) goto Broken;
if (!ExtractLong(stfStr, &along)) goto Broken;
keySig.stf = along;
if (!ExtractLong(numAccStr, &along)) goto Broken;
keySig.numAcc = along;
if (!(*shpFltStr=='#' || *shpFltStr=='b')) goto Broken;
keySig.shpFlt = *shpFltStr;
/* Now handle the keySig as needed. */
ProcessKeySig(keySig);
gKeySigCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessKeySig");
return FALSE;
}
/* Parse a line like this:
* T stf=1 num=2 denom=2 displ=3
*/
Boolean ParseAndProcessTimeSig()
{
char stfStr[32], numStr[32], denomStr[32], displStr[32];
short count;
long along;
ATIMESIG timeSig;
count = sscanf(gInBuf, "%*c%s%s%s%s", stfStr, numStr, denomStr, displStr);
if (count<3) goto Broken;
if (!ExtractLong(stfStr, &along)) goto Broken;
timeSig.stf = along;
if (!ExtractLong(numStr, &along)) goto Broken;
timeSig.num = along;
if (!ExtractLong(denomStr, &along)) goto Broken;
timeSig.denom = along;
timeSig.displ = 1; /* Default = normal timesig (n/d) */
if (count>3) {
if (!ExtractLong(displStr, &along)) goto Broken;
timeSig.displ = along;
}
/* Now handle the timeSig as needed. */
ProcessTimeSig(timeSig);
gTimeSigCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessTimeSig");
return FALSE;
}
#define NLERR_BADTIME(t) (t<0L)
#define NLERR_BADVOICE(v) (v<1L || v>31L)
#define NLERR_BADPART(npt) (npt<1L || npt>(long)MAXSTAVES)
#define NLERR_BADSTAFF(stf) (stf<1L || stf>(long)MAXSTAVES)
#define NLERR_BADNOTENUM(nn) (nn<0L || nn>127L)
#define NLERR_BADPDUR(pdur) (pdur<0L || pdur>32000L)
#define NLERR_BADVELOCITY(vel) (vel<0L || vel>127L)
/* Parse a line like these:
* N t=0 v=1 npt=1 stf=1 dur=4 dots=0 nn=72 acc=0 eAcc=3 pDur=456 vel=75 ...... appear=3
* G t=-1 v=1 npt=1 stf=1 dur=5 dots=0 nn=74 acc=0 eAcc=3 pDur=240 vel=75 . appear=1
* N t=480 v=1 npt=1 stf=1 dur=4 dots=0 nn=70 acc=2 eAcc=2 pDur=456 vel=75 ...... appear=1 mods=14,12
*
* This is a bit tricky because note lines contain a variable number of fields, and
* because grace notes have only one flag and no modifiers. The line will normally have
* a field of the form "appear=x", but in very early Nightingale notelists, that field is
* present only if the note/grace note's appearance is something other than NORMAL_VIS (=1).
* If the note has any modifiers, an optional mods field follows the flags field (or the
* appearance field, if there is one). The mods field gives a list of modCodes separated
* by commas, with optional data values trailing the modCode (and separated from it by a
* colon). E.g.:
* mods=11 [just 1 modifier having modCode=11]
* mods=0,2,3 [3 modifiers]
* mods=3:50,2 [2 modifiers, the first having a data value of 50]
* To sort all this out, we note the number of words read by sscanf (returned in <count>)
* and then switch on that to do the appropriate parsing.
*
* IMPORTANT: the calling function must allocate <pNote->modList> before calling this!
*/
Boolean ParseNote(ANOTE *pNote)
{
char noteKind, timeStr[32], voiceStr[32], partStr[32], staffStr[32], durStr[32],
dotsStr[32], noteNumStr[32], accStr[32], eAccStr[32], pDurStr[32],
velStr[32], flagsStr[32], penultStr[64], finalStr[64];
short count, numMods, j;
long along;
AMOD tmpMods[MAX_MODNRS];
pNote->appearance = NORMAL_VIS;
pNote->numMods = 0;
count = sscanf(gInBuf, "%c%s%s%s%s%s%s%s%s%s%s%s%s%s%s", ¬eKind,
timeStr, voiceStr, partStr, staffStr, durStr, dotsStr,
noteNumStr, accStr, eAccStr, pDurStr, velStr, flagsStr,
penultStr, finalStr);
if (count<13) goto Broken;
pNote->isGrace = (noteKind=='G');
if (!ExtractLong(timeStr, &along)) goto Broken;
if (!pNote->isGrace && NLERR_BADTIME(along)) goto Broken; /* grace note time is ignored */
pNote->t = along;
if (!ExtractLong(voiceStr, &along)) goto Broken;
if (NLERR_BADVOICE(along)) goto Broken; /* NB: user, not internal, voice */
pNote->v = along;
if (!ExtractLong(partStr, &along)) goto Broken;
if (NLERR_BADPART(along)) goto Broken;
pNote->npt = along;
if (!ExtractLong(staffStr, &along)) goto Broken;
if (NLERR_BADSTAFF(along)) goto Broken;
pNote->stf = along;
if (!ExtractLong(durStr, &along)) goto Broken;
pNote->dur = along;
if (!ExtractLong(dotsStr, &along)) goto Broken;
pNote->dots = along;
if (!ExtractLong(noteNumStr, &along)) goto Broken;
if (NLERR_BADNOTENUM(along)) goto Broken;
pNote->nn = along;
if (!ExtractLong(accStr, &along)) goto Broken;
pNote->acc = along;
if (!ExtractLong(eAccStr, &along)) goto Broken;
pNote->eAcc = along;
if (!ExtractLong(pDurStr, &along)) goto Broken;
if (!pNote->isGrace && NLERR_BADPDUR(along)) goto Broken;
pNote->pDur = along;
if (!ExtractLong(velStr, &along)) goto Broken;
if (NLERR_BADVELOCITY(along)) goto Broken;
pNote->vel = along;
if (!ExtractNoteFlags(flagsStr, pNote)) goto Broken;
switch (count) {
case 13: /* no appearance or mod strings */
break;
case 15: /* has appearance AND mod strings (in that order) */
if (!ExtractNoteMods(finalStr, &numMods, tmpMods)) goto Broken;
if (numMods<0 || numMods>=MAX_MODNRS) { printf("BUG: numMods=%d!\n", numMods); goto Broken; }
pNote->numMods = numMods;
for (j = 0; j<numMods; j++)
pNote->modList[j] = tmpMods[j];
case 14: /* has appearance OR mod string */
if (!strncmp(penultStr, "appear=", (size_t)7)) { /* it's an appearance string */
if (!ExtractLong(penultStr, &along)) goto Broken;
pNote->appearance = along;
}
else {
if (!ExtractNoteMods(finalStr, &numMods, tmpMods)) goto Broken;
if (numMods<0 || numMods>=MAX_MODNRS) { printf("BUG: numMods=%d!\n", numMods); goto Broken; }
pNote->numMods = numMods;
for (j = 0; j<numMods; j++)
pNote->modList[j] = tmpMods[j];
}
break;
default:
goto Broken;
}
return TRUE;
Broken:
ReportParseFailure("ParseNote");
return FALSE;
}
Boolean ParseAndProcessNote()
{
ANOTE note;
Boolean okay;
size_t modListSize;
modListSize = sizeof(AMOD) * MAX_MODNRS;
note.modList = (AMOD *)malloc(modListSize);
if (!note.modList) {
ReportParseFailure("ParseAndProcessNote/malloc");
return FALSE;
}
okay = ParseNote(¬e);
/* Now handle the note as needed. */
if (okay) {
if (note.isGrace) ProcessNote(note, FALSE, TRUE);
else ProcessNote(note, FALSE, FALSE);
}
/* Don't delete the next line or you'll have a memory leak! */
if (note.modList) free(note.modList);
if (note.isGrace) gGraceNoteCount++;
else gNoteCount++;
return okay;
}
/* Parse a line like these:
* R t=9810 v=1 npt=1 stf=1 dur=3 dots=0 ...... appear=1 mods=10
* R t=480 v=1 npt=1 stf=1 dur=4 dots=0 ...... appear=1
* ??Of the flags, we handle only <inTuplet>; actually, the slurred and
* and tied flags can occur on rests, though they rarely do.
*
* IMPORTANT: the calling function must allocate <pRest->modList> before calling this!
*/
Boolean ParseRest(AREST *pRest)
{
char timeStr[32], voiceStr[32], partStr[32], staffStr[32], durStr[32],
dotsStr[32], flagsStr[32], appearStr[32], modsStr[64];
short count, numMods, j;
long along;
ANOTE tempNote;
AMOD tmpMods[MAX_MODNRS];
count = sscanf(gInBuf, "%*c%s%s%s%s%s%s%s%s%s", timeStr, voiceStr, partStr, staffStr,
durStr, dotsStr, flagsStr, appearStr, modsStr);
if (count<8) goto Broken;
if (!ExtractLong(timeStr, &along)) goto Broken;
if (NLERR_BADTIME(along)) goto Broken;
pRest->t = along;
if (!ExtractLong(voiceStr, &along)) goto Broken;
if (NLERR_BADVOICE(along)) goto Broken; /* NB: user, not internal, voice */
pRest->v = along;
if (!ExtractLong(partStr, &along)) goto Broken;
if (NLERR_BADPART(along)) goto Broken;
pRest->npt = along;
if (!ExtractLong(staffStr, &along)) goto Broken;
if (NLERR_BADSTAFF(along)) goto Broken;
pRest->stf = along;
if (!ExtractLong(durStr, &along)) goto Broken;
pRest->dur = along;
if (!ExtractLong(dotsStr, &along)) goto Broken;
pRest->dots = along;
tempNote.isGrace = FALSE; /* Rest lines have all note flags */
if (!ExtractNoteFlags(flagsStr, &tempNote)) goto Broken;
pRest->inTuplet = tempNote.inTuplet;
if (!ExtractLong(appearStr, &along)) goto Broken;
pRest->appearance = along;
pRest->numMods = 0;
if (count>=9) {
if (!ExtractNoteMods(modsStr, &numMods, tmpMods)) goto Broken;
if (numMods<0 || numMods>=MAX_MODNRS) { printf("BUG: numMods=%d!\n", numMods); goto Broken; }
pRest->numMods = numMods;
for (j = 0; j<numMods; j++)
pRest->modList[j] = tmpMods[j];
}
return TRUE;
Broken:
ReportParseFailure("ParseRest");
return FALSE;
}
Boolean ParseAndProcessRest()
{
AREST rest;
Boolean okay;
size_t modListSize;
modListSize = sizeof(AMOD) * MAX_MODNRS;
rest.modList = (AMOD *)malloc(modListSize);
if (!rest.modList) {
ReportParseFailure("ParseAndProcessRest/malloc");
return FALSE;
}
ANOTE note;
okay = ParseRest(&rest);
/* Now handle the rest as needed. */
if (okay) {
/* here's a hack call ProcessNote with our rest struct */
/* and assign it all the values we need from our AREST */
note.t = rest.t;
note.v = rest.v;
note.npt = rest.npt;
note.stf = rest.stf;
note.dur = rest.dur;
note.dots = rest.dots;
note.nn = ' ';
note.acc = 0;
note.eAcc = 0;
note.vel = ' ';
note.pDur = 0;
note.tiedL = FALSE;
note.tiedR = FALSE;
note.slurredL = FALSE;
note.slurredR = FALSE;
note.inTuplet = rest.inTuplet;
note.inChord = FALSE;
note.mainNote = FALSE;
note.isGrace = FALSE;
note.appearance = rest.appearance;
note.numMods = rest.numMods;
/* AMOD *modList;*/
/* ProcessRest(rest); */
/* for some reason first rest on each staff contains null/zero values */
if (note.dur != 0) {
ProcessNote(note, TRUE, FALSE);
}
}
/* Don't delete the next line or you'll have a memory leak! */
if (rest.modList) free(rest.modList);
gRestCount++;
return okay;
}
/* Parse a line like this:
* / t=1440 type=1
*/
Boolean ParseAndProcessBarline()
{
char timeStr[32], typeStr[32];
short count;
long along;
ABARLINE barLine;
count = sscanf(gInBuf, "%*c%s%s", timeStr, typeStr);
if (count<2) goto Broken;
if (!ExtractLong(timeStr, &along)) goto Broken;
if (NLERR_BADTIME(along)) goto Broken;
barLine.t = along;
if (!ExtractLong(typeStr, &along)) goto Broken;
barLine.type = along;
/* Now handle the barline as needed. */
ProcessBarline(barLine);
gBarlineCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessBarline");
return FALSE;
}
/* Parse a line like this:
* P v=2 npt=1 num=3 denom=2 appear=101
*/
Boolean ParseAndProcessTuplet()
{
char voiceStr[32], partStr[32], numStr[32], denomStr[32], appearStr[32];
short count;
long along;
ATUPLET tuplet;
count = sscanf(gInBuf, "%*c%s%s%s%s%s", voiceStr, partStr, numStr, denomStr, appearStr);
if (count<5) goto Broken;
if (!ExtractLong(voiceStr, &along)) goto Broken;
tuplet.v = along;
if (!ExtractLong(partStr, &along)) goto Broken;
tuplet.npt = along;
if (!ExtractLong(numStr, &along)) goto Broken;
tuplet.num = along;
if (!ExtractLong(denomStr, &along)) goto Broken;
tuplet.denom = along;
if (!ExtractLong(appearStr, &along)) goto Broken;
tuplet.appearance = along;
/* Now handle the tuplet as needed. */
ProcessTuplet(tuplet);
gTupletCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessTuplet");
return FALSE;
}
/* Parse a line like this:
* D stf=1 type=2
*/
Boolean ParseAndProcessDynamic()
{
char stfStr[32], typeStr[32];
short count;
long along;
ADYNAMIC dynamic;
count = sscanf(gInBuf, "%*c%s%s", stfStr, typeStr);
if (count<2) goto Broken;
if (!ExtractLong(stfStr, &along)) goto Broken;
if (NLERR_BADSTAFF(along)) goto Broken;
dynamic.stf = along;
if (!ExtractLong(typeStr, &along)) goto Broken;
dynamic.type = along;
/* Now handle the dynamic as needed. */
ProcessDynamic(dynamic);
gDynamicCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessDynamic");
return FALSE;
}
/* Parse a line like this:
* A v=2 npt=1 stf=2 S3 'strepitoso'
*/
Boolean ParseAndProcessText()
{
char voiceStr[32], partStr[32], staffStr[32], codeStr[32];
char textStr[256], styleDigit;
short count;
long along;
ATEXT text;
count = sscanf(gInBuf, "%*c%s%s%s%s", voiceStr, partStr, staffStr, codeStr);
if (count<4) goto Broken;
if (!ExtractLong(voiceStr, &along)) goto Broken;
text.v = along;
if (!ExtractLong(partStr, &along)) goto Broken;
text.npt = along;
if (!ExtractLong(staffStr, &along)) goto Broken;
text.stf = along;
if (!(*codeStr=='S' || *codeStr=='L')) goto Broken;
text.code = *codeStr;
styleDigit = *(codeStr+1);
if (isdigit(styleDigit))
text.style = (short)styleDigit-(short)'0'; /* Should be 1 to 5 */
else
text.style = 0; /* "Unknown" */
/* Get the quoted string, which can contain embedded whitespace. NB: the call below
* assumes the preceding stuff on the line doesn't contain a single quote: that's
* not completely safe, but I don't see an easy way to start scanning in the
* correct position of <gInBuf>.
*/
if (ExtractDelimString(gInBuf, '\'', '\'', textStr, 256)<0) goto Broken;
/*
* Storing <textStr> is somewhat of a problem: it could be pretty long but very
* rarely will be. So storing it in a manner that's grossly wasteful of space is
* trivial, but being efficient is much harder (though still not difficult). For
* now, we don't bother with efficiency.
*/
strcpy(text.textStr, textStr);
/* Now handle the text as needed. */
ProcessText(text);
gTextCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessText");
return FALSE;
}
/* Parse a line like this:
* M stf=1 'Prestissimo' q=100
* ??The "100" is really a string (e.g., "c.100", "96-104"), and I think it can contain
* embedded space: we handle strings but not with embedded space.
*/
Boolean ParseAndProcessTempoMark()
{
char staffStr[32], tempoStr[256], metroStr[256], metroUnit, dotOrEquals;
short count;
long along, startPos, startMetroPos, whitespaceLen;
ATEMPO tempo;
Boolean dotted;
count = sscanf(gInBuf, "%*c%s", staffStr);
if (count<1) goto Broken;
/* Get the 1st quoted string, which can contain embedded whitespace. NB: the call
* below assumes the preceding stuff on the line doesn't contain a single quote:
* that's not completely safe, but I don't see an easy way to start scanning in the
* correct position of <gInBuf>.
*/
startPos = ExtractDelimString(gInBuf, '\'', '\'', tempoStr, 256);
if (startPos<0) goto Broken;
/* Get the metronome-unit character and the metronome mark. Note these are optional:
* if they're absent, we'll set <metroUnit> to the newline (or maybe return) char.
*/
startMetroPos = startPos+strlen(tempoStr)+2;
whitespaceLen = strspn(gInBuf+startMetroPos, " \t");
startMetroPos += whitespaceLen; /* Advance to metronome unit */
sscanf(gInBuf+startMetroPos, "%c%c", &metroUnit, &dotOrEquals);
dotted = (dotOrEquals=='.');
startMetroPos += (dotted? 3 : 2); /* Advance past equals sign */
sscanf(gInBuf+startMetroPos, "%s", metroStr);
if (!ExtractLong(staffStr, &along)) goto Broken;
tempo.stf = along;
/*
* Storing <tempoStr> and <metroStr> have an efficiency problem like <textStr>,
* but they can't be very long. We just go ahead and waste space.
*/
strcpy(tempo.tempoStr, tempoStr);
tempo.metroUnit = metroUnit;
tempo.dotted = dotted;
strcpy(tempo.metroStr, metroStr);
/* Now handle the tempo mark as needed. */
ProcessTempoMark(tempo);
gTempoCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessTempoMark");
return FALSE;
}
/* Parse a line like this:
* B v=1 npt=1 count=2
*/
Boolean ParseAndProcessBeam()
{
char voiceStr[32], nptStr[32], beamCountStr[32];
short count;
long along;
ABEAM beam;
count = sscanf(gInBuf, "%*c%s%s%s", voiceStr, nptStr, beamCountStr);
if (count<3) goto Broken;
if (!ExtractLong(voiceStr, &along)) goto Broken;
beam.v = along;
if (!ExtractLong(nptStr, &along)) goto Broken;
beam.npt = along;
if (!ExtractLong(beamCountStr, &along)) goto Broken;
beam.count = along;
/* Now handle the beam as needed. */
ProcessBeam(beam);
gBeamCount++;
return TRUE;
Broken:
ReportParseFailure("ParseAndProcessBeam");
return FALSE;
}
/* ---------------------------------------- ParseAndProcessStructComment and allies -- */
/* Given a pointer to the beginning of one of the optional fields on the header
structured comment, parse it and return a pointer to the char after its end. If
we don't recognize the field, or there's no non-whitespace in the string, just
return NULL. */
static char *ParseField(char *p, Boolean *pOkay);
static char *ParseField(char *p, Boolean *pOkay)
{
char str[256];
short ans, number;
*pOkay = TRUE;
/* Point to first non-whitespace char. If there's nothing following, do nothing. */
for (p++; *p; p++)
if (!isspace((int)*p)) break;
ans = sscanf(p, "%s", str);
if (ans<1) return NULL;
if (strcmp(str, "delaccs")==0) {
gDelAccs = TRUE;
p += strlen(str);
return p;
}
else if (strncmp(str, "startmeas=", strlen("startmeas="))==0) {
p = strchr(p, '='); /* Skip to the next '=' in input */
if (!p) return (char *)0;
p++;
ans = sscanf(p, "%s", str);
p += strlen(str);
number = (short)strtol(str, (char **)NULL, 10);
gFirstMNNumber = number;
return p;
}
else {
*pOkay = FALSE;
return NULL; /* Unrecognized field */
}
}
/* Parse a line like this (a header):
%%Score file='Untitled' partstaves=1 1 1 0
In the future there may be other kinds of structured comment.
*/
#define COMMENT_SCORE "%%Score"
#define COMMENT_NOTELIST "%%Notelist"
static Boolean ParseAndProcessStructComment()
{
char str[64], *p, nstaves;
short ans, i;
long startPos;
Boolean okay;
if (gNotelistVersion<=1)
okay = (strncmp(gInBuf, COMMENT_SCORE, strlen(COMMENT_SCORE))==0);
else
okay = (strncmp(gInBuf, COMMENT_NOTELIST, strlen(COMMENT_NOTELIST))==0);
/* Extract score name, which is enclosed in single-quotes and can contain whitespace.
It should not be longer than 31 chars (excluding terminating null).
*/
startPos = ExtractDelimString(gInBuf, '\'', '\'', gFilename, 64);
if (startPos<0) goto Broken;
p = gInBuf+startPos+1; /* Advance past the opening delimiter */
p = strchr(p, '\''); /* Advance to the closing delimiter */
gNumNLStaves = 0;
gFirstMNNumber = 1;
/* Construct a 1-based array giving the arrangement of parts and staves.
Indices represent part numbers; values represent the number of staves
in a part.
*/
for (i=0; i<=MAXPARTS; i++)
gPartStaves[i] = 0;
p = strchr(p, '='); /* Skip to the next '=' in input */
if (!p) goto Broken;
for (p++, i=1; i<=MAXPARTS; i++, p++) {
ans = sscanf(p, "%s", str);
if (ans<1) goto Broken;
nstaves = (char)strtol(str, (char **)NULL, 10);
if (nstaves<=0) {
/* Found the terminator for the part/staff info: skip over it. */
p = strchr(p, ' ');
if (!p) goto Done;
break;
}
gPartStaves[i] = nstaves;
gNumNLStaves += nstaves;
p = strchr(p, ' ');
if (!p) goto Done;
}
/* Parse and make use of any optional fields that appear at the end of the line. If any
* are unrecognized, complain, but don't return failure. */
do
p = ParseField(p, &okay);
while (p);
if (!okay) {
STATUS_PRINTF("ERROR (ParseAndProcessStructComment): the Notelist's header line contained unrecognized field(s).\n");
}
Done:
return ProcessStructComment();
Broken:
ReportParseFailure("ParseAndProcessStructComment");
return FALSE;
}