00001
00023
00024
00025
00026
00027 #ifndef FOR_MUTWIN
00028 #include <iostream>
00029 #endif
00030
00031 #include "Frac.h"
00032 #include "GIS.h"
00033 #include "GSP.h"
00034
00035
00036
00037
00038 const mutChar * Tags[NTAGS] =
00039 {
00040 mutT(""), mutT("intens"), mutT("slur"), mutT("beam"), mutT("text"),
00041 mutT("bar"), mutT("cresc"), mutT("dim"), mutT("crescBegin"), mutT("crescEnd"),
00042 mutT("dimBegin"), mutT("dimEnd"), mutT("tempo"), mutT("accel"), mutT("rit"),
00043 mutT("accelBegin"), mutT("accelEnd"), mutT("ritBegin"), mutT("ritEnd"), mutT("instr"),
00044 mutT("tie"), mutT("stacc"), mutT("accent"), mutT("ten"), mutT("marcato"),
00045 mutT("trill"), mutT("mord"), mutT("turn"), mutT("trem"), mutT("fermata"),
00046 mutT("grace"), mutT("cue"), mutT("repeatBegin"), mutT("repeatEnd"), mutT("clef"),
00047 mutT("meter"), mutT("key"), mutT("oct"), mutT("staff"), mutT("beamsAuto"),
00048 mutT("beamsOff"), mutT("stemsAuto"), mutT("stemsUp"), mutT("stemsDown"), mutT("doubleBar"),
00049 mutT("tactus"), mutT("title"), mutT("composer"), mutT("mark"), mutT("label"),
00050 mutT("alter"), mutT("mutabor")
00051 };
00052
00053 const mutChar * TagShorts[NTAGSHORTS] =
00054 {
00055 mutT(""), mutT("i"), mutT("sl"), mutT("bm"), mutT("t"), mutT("|")
00056 };
00057
00058 GisToken *Root;
00059
00060 GisToken **Current, *LastOpenBracket;
00061
00062 GisTagBegin *LastOpenRange;
00063
00064 char TagMode;
00065
00066 mutString TagName;
00067
00068 mutString TagSep;
00069
00070 GisToken *Para, *LastPara;
00071
00072 int LastOctave;
00073
00074 frac LastDuration;
00075
00076 #ifdef DEBUG
00077 int debugcount = 0;
00078
00079 #define GISDEBUG(para) \
00080 DEBUGLOG2(other,_T("")); std::cerr << para << std::endl;
00081
00082 #else
00083 #define GISDEBUG(para)
00084 #endif
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #ifndef FOR_MUTWIN
00101
00102 void GisToken::Stream(ostream &out, char sep)
00103 {
00104 if ( sep ) out << Sep;
00105
00106 if ( Next ) Next->Stream(out, sep);
00107 }
00108
00109 void GisSegment::Stream(ostream &out, char sep)
00110
00111 {
00112 if ( sep )
00113 out << "{" << Sep;
00114 else
00115 out << "{ ";
00116
00117 if ( Contents ) Contents->Stream(out, sep);
00118
00119 if ( sep )
00120 out << "}" << Sep2;
00121 else
00122 out << "} ";
00123
00124 if ( Next ) Next->Stream(out, sep);
00125 }
00126
00127 void GisSequenz::Stream(ostream &out, char sep)
00128 {
00129 if ( sep )
00130 out << "[" << Sep;
00131 else
00132 out << "[ ";
00133
00134 LastOctave = 1;
00135
00136 LastDuration = frac(1, 4);
00137
00138 if ( Contents ) Contents->Stream(out, sep);
00139
00140 if ( sep )
00141 out << "]" << Sep2;
00142 else
00143 out << "] ";
00144
00145 if ( Next ) Next->Stream(out, sep);
00146 }
00147
00148 void GisTag::Stream(ostream &out, char sep)
00149 {
00150 if ( !strcmp(Name, "|") )
00151 out << "|";
00152 else
00153 out << "\\" << Name;
00154
00155 if ( sep )
00156 out << Sep;
00157 else
00158 if ( Para ) out << "<";
00159
00160 if ( Para ) Para->Stream(out, sep);
00161
00162 if ( !sep && Para )
00163 out << ">";
00164
00165 if ( Next ) Next->Stream(out, sep);
00166 }
00167
00168 #endif
00169
00170 GisType GisTag::GetParaType(int nr)
00171 {
00172 GisToken *P = Para;
00173
00174 while ( P ) {
00175 if ( nr <= 1 ) break;
00176
00177 P = P->Next;
00178
00179 nr--;
00180 }
00181
00182 if ( P )
00183 return P->Type();
00184 else
00185 return GTNull;
00186 }
00187
00188 GisToken *GisTag::GetPara(int nr)
00189 {
00190 GisToken *P = Para;
00191
00192 while ( P ) {
00193 if ( nr <= 1 ) break;
00194
00195 P = P->Next;
00196
00197 nr--;
00198 }
00199
00200 return P;
00201 }
00202
00203 #ifndef FOR_MUTWIN
00204
00205 void GisTagBegin::Stream(ostream &out, char sep)
00206 {
00207 if ( Name && Name[0] ) out << "\\";
00208
00209 if ( sep )
00210 out << Name << Sep;
00211 else {
00212 out << Name;
00213
00214 if ( Para ) out << "<";
00215 }
00216
00217 if ( Para ) Para->Stream(out, sep);
00218
00219 if ( !sep ) {
00220 if ( Para )
00221 out << ">";
00222
00223 out << "( ";
00224 }
00225
00226 if ( Next ) Next->Stream(out, sep);
00227 }
00228
00229 void GisTagEnd::Stream(ostream &out, char sep)
00230 {
00231 if ( sep )
00232 out << ")" << Sep;
00233 else
00234 out << ") ";
00235
00236 if ( Next ) Next->Stream(out, sep);
00237 }
00238
00239 #endif
00240
00241 int Name2Key(const mutString name)
00242 {
00243 mutChar notes[] = mutT("c d ef g a b");
00244 size_t l = mutLen(name);
00245 int i = 11;
00246
00247 if ( l == 1 ) {
00248 while ( i >= 0 ) {
00249 if ( notes[i] == name[0] )
00250 break;
00251
00252 i--;
00253 }
00254
00255 if ( name[0] == 'h' )
00256 return 11;
00257 else
00258 return i;
00259 } else if ( l == 3 ) {
00260 while ( i >= 0 ) {
00261 if ( notes[i] == name[0] )
00262 break;
00263
00264 i--;
00265 }
00266
00267 if ( i != -1 ) i++;
00268
00269 return i;
00270 } else
00271 return -1;
00272 }
00273
00274 int Acc2Int(const mutString acc)
00275
00276 {
00277 if ( !acc ) return 0;
00278
00279 int i = 0;
00280
00281 #ifdef WX
00282 for (size_t j = 0; j < mutLen(acc); j++)
00283 #else
00284 for (int j = 0; acc[j]; j++)
00285 #endif
00286 {
00287
00288 if ( acc[j] == '&' )
00289 i--;
00290 else if ( acc[j] == '#' )
00291 i++;
00292 }
00293 return i;
00294 }
00295
00296 #ifdef WX
00297 #define strdupchr(a) (a)
00298 #else
00299 char *strdupchr(char a)
00300 {
00301 char s[2] = "w";
00302 s[0] = a;
00303 return strdup(s);
00304 }
00305
00306 #endif
00307
00308 GisNote::GisNote(int key, int octave, int acc, mutString sep, GisToken *next)
00309 : GisToken(sep, next)
00310 {
00311 if ( key == NO_KEY ) {
00312 #ifdef WX
00313 Name = mutT("_");
00314 #else
00315 Name = strdup("_");
00316 #endif
00317 Accedentials = mutEmptyString;
00318 ;
00319 Octave = 0;
00320 } else {
00321 mutChar Flats[] = mutT("cddeefggaabb");
00322 mutChar FlatsA[] = mutT(" & & & & & ");
00323 mutChar Sharps[] = mutT("ccddeffggaab");
00324 mutChar SharpsA[] = mutT(" # # # # # ");
00325 mutChar accs;
00326 int Index = key % 12;
00327 int Abstand = key /12;
00328
00329 if ( Index < 0 ) {
00330 Index += 12;
00331 Abstand--;
00332 }
00333
00334 if ( acc < 0 ) {
00335 Name = strdupchr(Flats[Index]);
00336 accs = FlatsA[Index];
00337 } else {
00338 Name = strdupchr(Sharps[Index]);
00339 accs = SharpsA[Index];
00340 }
00341
00342 if ( accs != mutT(' ') )
00343 Accedentials = strdupchr(accs);
00344 else
00345 Accedentials = mutEmptyString;
00346
00347 Octave = Abstand - 5 - octave;
00348 }
00349
00350 Duration = 0;
00351 }
00352
00353 int GisNote::GetKey()
00354
00355 {
00356 int Key = Name2Key(Name);
00357
00358 if ( Key == -1 ) return -1;
00359
00360 Key += ( Octave + 5) * 12;
00361
00362 Key += Acc2Int(Accedentials);
00363
00364 return Key;
00365 }
00366
00367 #ifndef FOR_MUTWIN
00368
00369 void GisNote::Stream(ostream &out, char sep)
00370 {
00371 out << Name << Accedentials;
00372
00373 if ( Name[0] != '_' )
00374 if ( Octave != LastOctave ) {
00375 out << Octave;
00376 LastOctave = Octave;
00377 }
00378
00379 if ( Duration != LastDuration ) {
00380 if ( Duration.n != 1 || Duration == frac(1,1) )
00381 out << "*" << Duration.n;
00382
00383 if ( Duration.d != 1 )
00384 out << "/" << Duration.d;
00385
00386 LastDuration = Duration;
00387 }
00388
00389 if ( sep )
00390 out << Sep;
00391 else
00392 out << " ";
00393
00394 if ( Next ) Next->Stream(out, sep);
00395 }
00396
00397
00398 void GisParaInt::Stream(ostream &out, char sep)
00399 {
00400 if ( sep )
00401 out << i << Sep;
00402 else {
00403 out << i;
00404
00405 if ( Next ) out << ", ";
00406 }
00407
00408 if ( Next ) Next->Stream(out, sep);
00409 }
00410
00411 void GisParaReal::Stream(ostream &out, char sep)
00412 {
00413 if ( sep )
00414 out << x << Sep;
00415 else {
00416 out << x;
00417
00418 if ( Next ) out << ", ";
00419 }
00420
00421 if ( Next ) Next->Stream(out, sep);
00422 }
00423
00424 void GisParaStr::Stream(ostream &out, char sep)
00425 {
00426 if ( sep )
00427 out << '"' << s << '"' << Sep;
00428 else {
00429 out << '"' << s << '"';
00430
00431 if ( Next ) out << ", ";
00432 }
00433
00434 if ( Next ) Next->Stream(out, sep);
00435 }
00436
00437 void GisComma::Stream(ostream &out, char sep)
00438 {
00439 out << ",";
00440
00441 if ( sep )
00442 out << Sep;
00443 else
00444 out << " ";
00445
00446 if ( Next ) Next->Stream(out, sep);
00447 }
00448
00449 #endif
00450
00451
00452
00453
00454
00455
00456 #ifdef WX
00457 #define AddStr(s1,s2,s3) (s1 += s2 + s3)
00458 void Clear(mutString * s)
00459 {
00460 *s = mutEmptyString;
00461 }
00462
00463 #else
00464 void Clear(char **s)
00465 {
00466 if ( *s ) {
00467 free(*s);
00468 *s = 0;
00469 }
00470 }
00471
00472
00473
00474 char *AddStr(char **s1, const char *s2, const char *s3)
00475 {
00476 int l = strlen(*s1) + strlen(s2) + strlen(s3) + 1;
00477 char *s = (char*) malloc(l);
00478 s[0] = 0;
00479
00480 if ( *s1 ) strcat(s, *s1);
00481
00482 if ( s2 ) strcat(s, s2);
00483
00484 if ( s3 ) strcat(s, s3);
00485
00486 Clear(s1);
00487
00488 *s1 = s;
00489
00490 return *s1;
00491 }
00492
00493 #endif
00494
00495 int BuildTag()
00496 {
00497 DEBUGLOG2(other,_T("TagName.len %d, '%s'"),TagName.Len(),TagName.c_str());
00498 GisTag *Tag = new GisTag(TagName, Para, TagSep);
00499 *Current = Tag;
00500 Current = &(Tag->Next);
00501 Clear(&TagName);
00502 Clear(&TagSep);
00503 Para = 0;
00504 LastPara = 0;
00505 TagMode = 0;
00506 return 0;
00507 }
00508
00509
00510
00511
00512 int StartSep()
00513 {
00514 DEBUGLOG2(other,_T("saving Sep %s"),Sep.c_str());
00515 *Current = new GisToken(Sep, 0);
00516 Current = &((*Current)->Next);
00517 return 0;
00518 };
00519
00520 int BeginSegment()
00521 {
00522 GISDEBUG("{")
00523
00524 if ( TagMode ) BuildTag();
00525
00526 GisSegment *Seg = new GisSegment(0, Sep, LastOpenBracket);
00527
00528 LastOpenBracket = Seg;
00529
00530 *Current = Seg;
00531
00532 Current = &(Seg->Contents);
00533
00534 return 0;
00535 };
00536
00537 int EndSegment()
00538 {
00539 GISDEBUG("}\n")
00540
00541 if ( TagMode ) BuildTag();
00542
00543 *Current = 0;
00544
00545 mutCopyString(((GisSegment*)(LastOpenBracket))->Sep2, Sep);
00546
00547 Current = &(LastOpenBracket->Next);
00548
00549 LastOpenBracket = LastOpenBracket->Next;
00550
00551 return 0;
00552 };
00553
00554 int BeginSequenz()
00555 {
00556 GISDEBUG("[")
00557
00558 if ( TagMode ) BuildTag();
00559
00560 GisSequenz *Seq = new GisSequenz(0, Sep, LastOpenBracket);
00561
00562 LastOpenBracket = Seq;
00563
00564 *Current = Seq;
00565
00566 Current = &(Seq->Contents);
00567
00568 return 0;
00569 };
00570
00571 int EndSequenz()
00572 {
00573 GISDEBUG("]\n")
00574
00575 if ( TagMode ) BuildTag();
00576
00577 *Current = 0;
00578
00579 mutCopyString(((GisSequenz*)LastOpenBracket)->Sep2, Sep);
00580
00581 Current = &(LastOpenBracket->Next);
00582
00583 LastOpenBracket = LastOpenBracket->Next;
00584
00585 return 0;
00586 };
00587
00588 int BeginParameter()
00589 {
00590 GISDEBUG("<")
00591 AddStr(TagSep, mutT("<"), Sep);
00592 return 0;
00593 };
00594
00595 int EndParameter()
00596 {
00597 GISDEBUG(">")
00598
00599 if ( Para )
00600 AddStr((LastPara->Sep), mutT(">"), Sep);
00601 else
00602 AddStr(TagSep, mutT(">"), Sep);
00603
00604 return 0;
00605 };
00606
00607 int BeginRange()
00608 {
00609 GISDEBUG("( ")
00610
00611 if ( LastPara )
00612 AddStr(LastPara->Sep, mutT("("), Sep);
00613 else if ( Para )
00614 AddStr(Para->Sep, mutT("("), Sep);
00615 else
00616 AddStr(TagSep, mutT("("), Sep);
00617
00618 GisTagBegin *Tag = new GisTagBegin(TagName, Para, TagSep, 0);
00619
00620 *Current = Tag;
00621
00622 Current = &(Tag->Next);
00623
00624 Tag->End = LastOpenRange;
00625
00626 LastOpenRange = Tag;
00627
00628 Clear(&TagName);
00629
00630 Clear(&TagSep);
00631
00632 Para = 0;
00633
00634 LastPara = 0;
00635
00636 TagMode = 0;
00637
00638 return 0;
00639 };
00640
00641 int EndRange()
00642 {
00643 GISDEBUG(")\n")
00644
00645 if ( TagMode ) BuildTag();
00646
00647 GisTagEnd *Tag = new GisTagEnd(LastOpenRange, Sep);
00648
00649 *Current = Tag;
00650
00651 Current = &(Tag->Next);
00652
00653 LastOpenRange = (GisTagBegin*) LastOpenRange->End;
00654
00655 (Tag->Begin)->End = Tag;
00656
00657 return 0;
00658 };
00659
00660 int NextSequenz()
00661 {
00662 GISDEBUG(", ");
00663
00664 if ( TagMode ) BuildTag();
00665
00666 return 0;
00667 };
00668
00669 int Note(const mutString name, const mutString accedentials, int octave, frac duration)
00670 {
00671 DEBUGLOG2(other,_T("Note '%s' %s Oktave %d "),
00672 name.c_str(),
00673 accedentials.c_str(),
00674 octave);
00675 GISDEBUG(name << accedentials << octave << "*" << duration << " ")
00676
00677 if ( TagMode ) BuildTag();
00678
00679 GisNote *Note = new GisNote(name, accedentials, octave, duration, Sep);
00680
00681 *Current = Note;
00682
00683 Current = &(Note->Next);
00684
00685 return 0;
00686 };
00687
00688 int Tag(const mutString tagName)
00689 {
00690 GISDEBUG("\n\\" << tagName)
00691
00692 if ( TagMode ) BuildTag();
00693
00694 mutCopyString(TagName, tagName);
00695
00696 mutCopyString(TagSep, Sep);
00697
00698 TagMode = 1;
00699
00700 return 0;
00701 };
00702
00703 int TagParaInt(long i)
00704 {
00705 GISDEBUG("ParaInt: " << i << ", ")
00706 GisParaInt *p = new GisParaInt(i, Sep);
00707
00708 if ( LastPara )
00709 LastPara->Next = p;
00710 else
00711 Para = p;
00712
00713 LastPara = p;
00714
00715 return 0;
00716 };
00717
00718 int TagParaReal(double x)
00719 {
00720 GISDEBUG("ParaReal: " << x << ", ")
00721 GisParaReal *p = new GisParaReal(x, Sep, 0);
00722
00723 if ( LastPara )
00724 LastPara->Next = p;
00725 else
00726 Para = p;
00727
00728 LastPara = p;
00729
00730 return 0;
00731 };
00732
00733 int TagParaStr(mutString s)
00734 {
00735 GISDEBUG("ParaStr: " << """" << s << """" << ", ")
00736 GisParaStr *p = new GisParaStr(s, Sep, 0);
00737
00738 if ( LastPara )
00739 LastPara->Next = p;
00740 else
00741 Para = p;
00742
00743 LastPara = p;
00744
00745 return 0;
00746 };
00747
00748 int Comma()
00749 {
00750 GISDEBUG(", ")
00751
00752 if ( TagMode ) BuildTag();
00753
00754 *Current = new GisComma(Sep, 0);
00755
00756 Current = &((*Current)->Next);
00757
00758 return 0;
00759 };
00760
00761
00762
00763
00764
00765 void UnRavel()
00766 {
00767 *Current = 0;
00768
00769
00770 while ( LastOpenBracket ) {
00771 GisToken *p = LastOpenBracket;
00772 LastOpenBracket = p->Next;
00773 p->Next = 0;
00774 }
00775
00776
00777 if ( TagName.size() ) Clear(&TagName);
00778
00779
00780 if ( Para ) delete Para;
00781 }
00782
00783
00784
00785
00786 GisType GetGisType(GisToken* token)
00787 {
00788 if ( token )
00789 return token->Type();
00790 else
00791 return GTNull;
00792 }
00793
00794 int GetTagId(const mutString &name, mutString ®istered)
00795
00796 {
00797 if (!name) {
00798 registered = Tags[0];
00799 return 0;
00800 }
00801
00802
00803 int i;
00804
00805 for (i = 0; i < NTAGS; i++) {
00806 if ( mutStrEq2(wxString(name), Tags[i]) ) {
00807 registered = Tags[i];
00808 return i;
00809 } ;
00810 }
00811
00812
00813 for (i = 0; i < NTAGSHORTS; i++) {
00814 DEBUGLOG2(other,_T("comparing '%s' with tag'%s'"),name.c_str(),Tags[i]);
00815
00816 if ( mutStrEq2(name, TagShorts[i]) ) {
00817 registered = TagShorts[i];
00818 return i;
00819 }
00820 }
00821
00822
00823 registered = mutEmptyString;
00824
00825 return -1;
00826 }
00827
00828 GisToken *CopyPara(GisToken *para)
00829
00830 {
00831 GisToken *C = 0;
00832 GisToken **Cursor = &C;
00833
00834 while ( para ) {
00835 *Cursor = para->Copy();
00836 Cursor = &(*Cursor)->Next;
00837 para = para->Next;
00838 }
00839
00840 return C;
00841 }
00842
00843 GisToken *GisParse(const mutString FileName)
00844 {
00845 Root = 0;
00846 Current = &Root;
00847 LastOpenBracket = 0;
00848 LastOpenRange = 0;
00849 TagMode = 0;
00850 TagName = mutEmptyString;
00851 TagSep = mutEmptyString;
00852 Para = 0;
00853 LastPara = 0;
00854 DEBUGLOG2(other,_T("TagName.len %d, '%s'"),TagName.Len(),TagName.c_str());
00855
00856 if ( GspParse(FileName) ) {
00857 UnRavel();
00858 delete Root;
00859 Root = 0;
00860 }
00861
00862 return Root;
00863 }
00864
00865 #ifdef WX
00866 wxString GISPrettyPrint(wxString s)
00867 {
00868 wxString ret = wxEmptyString;
00869 wxString pre = wxEmptyString;
00870
00871 for (size_t start = 0, current = 0 ; current < s.Len(); current++) {
00872 wxChar c = s[current];
00873
00874 if (c == _T('{')) {
00875 pre += _T(" ");
00876 } else if (c == _T('}')) {
00877 if (pre.Len() >= 2) pre = pre.Left(pre.Len()-2);
00878 } else if (c == _T('\n')) {
00879 ret += s (start, current - start + 1) + pre;
00880 start = current + 1;
00881 }
00882 }
00883
00884 return ret;
00885 }
00886
00887 #endif
00888