1 /*
2 GUIDO Library
3 Copyright (C) 2006 Grame
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
20 research@grame.fr
21
22 */
23
24 #ifdef WIN32
25 # pragma warning (disable : 4786 4244 4018 4065)
26 # pragma warning (disable : 4996)
27 # pragma warning (disable : 4102)
28 #endif
29
30 #include <iostream>
31
32 #include "gmnreader.h"
33 guido::gmnreader * gReader;
34
35 #define yyleng guidoarleng
36 #define yyin guidoarin
37 #define yyrestart guidoarrestart
38 #define yy_current_buffer guidoar_current_buffer
39 #define yy_delete_buffer guidoar_delete_buffer
40 #define yy_scan_string guidoar_scan_string
41
42 //#define yyDebug
43 #ifdef yyDebug
44 #define YYDEBUG 1
45 extern int yydebug;
46 #endif
47 #include "guidoparse.c++"
48 #include "unicode.c++"
49
50 using namespace std;
51
52 void convert_from_unicode (const char *filename);
53
54 namespace guido
55 {
56
parse(FILE * fd)57 static int parse (FILE *fd)
58 {
59 if (!fd) {
60 gReader->error("Invalid file descriptor", 0 );
61 return(-1);
62 }
63 yyin = fd;
64 #ifdef yyDebug
65 yydebug = 1;
66 #endif
67 int res = yyparse();
68
69 yyrestart(yyin);
70 BEGIN(INITIAL);
71 return res;
72 }
73
parse(const char * filename)74 static int parse (const char *filename)
75 {
76 int res;
77 if( !filename ) return -1; // parse error
78 FILE * fd = fopen(filename,"r");
79 if (fd == NULL){
80 gReader->error("Cannot not open file", 0 );
81 return(-1);
82 }
83
84 res = parse(fd);
85 fclose(fd);
86 return res;
87 }
88
readstring(const char * buffer,gmnreader * r)89 bool readstring (const char * buffer, gmnreader * r)
90 {
91 gReader = r;
92
93 if (!*buffer) return false; // error for empty buffers
94
95 YY_BUFFER_STATE b;
96 /*Copy string into new buffer and Switch buffers*/
97 b = yy_scan_string (buffer);
98
99 /*Parse the string*/
100 int ret = yyparse();
101
102 /*Delete the new buffer*/
103 yy_delete_buffer(b);
104
105 BEGIN(INITIAL);
106 return ret==0;
107 }
108
readfile(FILE * fd,gmnreader * r)109 bool readfile (FILE* fd, gmnreader * r)
110 {
111 gReader = r;
112 int ret = parse (fd);
113 return ret==0;
114 }
115
readfile(const char * file,gmnreader * r)116 bool readfile (const char * file, gmnreader * r)
117 {
118 gReader = r;
119 convert_from_unicode (file);
120 int ret = parse (file);
121 return ret==0;
122 }
123
124 #ifdef MAIN
125
126 class testreader : public gmnreader
127 {
128 public:
noteInit(const char * id)129 void noteInit (const char *id){ cout << "noteInit " << id << endl; }
noteAcc(int n)130 void noteAcc (int n) { cout << "noteAcc " << n << endl; }
noteOct(int n)131 void noteOct (int n) { cout << "noteOct " << n << endl; }
noteEnum(long int n)132 void noteEnum (long int n) { cout << "noteEnum " << n << endl; }
noteDenom(long int n)133 void noteDenom (long int n) { cout << "noteDenom " << n << endl; }
noteDot(void)134 void noteDot (void) { cout << "noteDot " << endl; }
noteDdot(void)135 void noteDdot (void) { cout << "noteDdot " << endl; }
noteAbsDur(long int n)136 void noteAbsDur (long int n) { cout << "noteAbsDur " << n << endl; }
seqAppendNote(void)137 void seqAppendNote (void) { cout << "seqAppendNote " << endl; }
138
chordInit(void)139 void chordInit (void) { cout << "chordInit " << endl; }
chordInitNote(void)140 void chordInitNote (void) { cout << "chordInitNote " << endl; }
chordAppendNote(void)141 void chordAppendNote(void) { cout << "chordAppendNote " << endl; }
seqAppendChord(void)142 void seqAppendChord (void) { cout << "seqAppendChord " << endl; }
143
seqInit(void)144 void seqInit (void) { cout << "seqInit " << endl; }
seqExit(void)145 void seqExit (void) { cout << "seqExit " << endl; }
146
segmInit(void)147 void segmInit (void) { cout << "segmInit " << endl; }
segmExit(void)148 void segmExit (void) { cout << "segmExit " << endl; }
segmAppendSeq(void)149 void segmAppendSeq(void) { cout << "segmAppendSeq " << endl; }
150
tagStart(const char * id,long int no)151 void tagStart (const char* id, long int no) { cout << "tagStart " << id << " - " << no << endl; }
tagIntArg(long int n)152 void tagIntArg (long int n) { cout << "tagIntArg " << n << endl; }
tagFloatArg(double r)153 void tagFloatArg (double r) { cout << "tagFloatArg " << r << endl; }
tagArgUnit(const char * unit)154 void tagArgUnit (const char* unit) { cout << "tagArgUnit " << unit << endl; }
tagStrArg(char * s)155 void tagStrArg (char *s) { cout << "tagStrArg " << s << endl; }
tagAdd(void)156 void tagAdd (void) { cout << "tagAdd " << endl; }
tagAddArg(const char * s)157 void tagAddArg (const char *s) { cout << "tagAddArg " << s << endl; }
tagEnd(void)158 void tagEnd (void) { cout << "tagEnd " << endl; }
159
tagRange()160 void tagRange() { cout << "tagRange " << endl; }
error(const char * msg,int lineno,int charno)161 int error (const char* msg, int lineno, int charno)
162 { cerr << msg << " on line " << lineno << " near character " << charno << endl; return 0; }
163 };
164
165
main(int argc,char * argv[])166 int main (int argc, char * argv[])
167 {
168 if (argc > 1) {
169 testreader r;
170 return readfile (argv[1], &r) ? 0 : 1;
171 }
172 return 0;
173 }
174 #endif
175
176 } // namespace
177
178