1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 
6 #include "general.h"
7 #include "fileoutput.h"
8 
9 /*
10 
11 Ringtone Tools - Copyright 2001-2005 Michael Kohn (mike@mikekohn.net)
12 This falls under the Kohnian license.  Please read
13 it at http://ringtonetools.mikekohn.net/
14 
15 This program is NOT opensourced.  You may not use any part
16 of this program for your own software.
17 
18 */
19 
20 extern int pushback;
21 extern int gettoken(FILE *fp, char *token, int flag);
22 
get_3210_tempo(FILE * fp)23 int get_3210_tempo(FILE *fp)
24 {
25 int marker;
26 char token[100];
27 int t,ch;
28 
29   marker=ftell(fp);
30   gettoken(fp,token,1);
31 
32   if (strcasecmp(token,"tempo")!=0)
33   {
34     pushback=-1;
35     fseek(fp,marker,0);
36     return 63;
37   }
38 
39   gettoken(fp,token,1);
40   if (strcmp(token,"=")!=0) printf("Expecting '=' and got '%s'\n",token);
41   gettoken(fp,token,1);
42 
43   t=atoi(token);
44   if (t<1) return 63;
45 
46   while (1)
47   {
48     ch=getc(fp);
49     if (ch=='\n' || ch=='\r' || ch==':' || ch=='\t') continue;
50     if (ch==EOF) { printf("Premature end of file\n"); break; }
51       else
52     {
53       ungetc(ch,fp);
54       break;
55     }
56   }
57 
58   return t;
59 }
60 
parse_3210(FILE * in,FILE * out,int out_type,struct note_t * note)61 int parse_3210(FILE *in, FILE *out, int out_type, struct note_t *note)
62 {
63 char token[16];
64 int t,l,ch,f;
65 int tone_modifier;
66 
67   l=0; t=0; f=0;
68   if (note->songname[0]!=0) t=1;
69   while(1)
70   {
71     ch=getc(in);
72     if (f==0 && (ch=='\n' || ch=='\r')) continue;
73     if (ch==':' || ch=='\n' || ch=='\r') break;
74     f=1;
75     if (ch==EOF)
76     {
77       printf("Error: Illegal keypress format file\n");
78       return -1;
79     }
80 
81     if (l<SONGNAME_LEN-1 && t==0) note->songname[l++]=ch;
82   }
83 
84   if (t!=0) note->songname[l]=0;
85 
86   note->bpm=get_3210_tempo(in);
87 
88 #ifdef DEBUG
89   printf("\n       Song Name: %s\n",note->songname);
90   printf("Beats Per Minute: %d\n",note->bpm);
91 #endif
92 
93   header_route(out,note,0,out_type);
94 
95   while(1)
96   {
97     tone_modifier=0;
98     note->modifier=0;
99 
100     t=gettoken(in,token,1);
101     if (t==-1)
102     { break; }
103 
104     if (token[0]==':') continue;
105 
106     note->length=atoi(token);
107 
108     if (note->length==1) note->length=0;
109       else
110     if (note->length==2) note->length=1;
111       else
112     if (note->length==4) note->length=2;
113       else
114     if (note->length==8) note->length=3;
115       else
116     if (note->length==16) note->length=4;
117       else
118     if (note->length==32) note->length=5;
119 
120     while(1)
121     {
122       t=gettoken(in,token,1);
123 
124       if (strcmp(token,".")==0)
125       { note->modifier++; }
126         else
127       if (strcmp(token,"#")==0)
128       { tone_modifier++; }
129         else
130       { break; }
131     }
132 
133     token[0]=tolower(token[0]);
134 
135     if (token[0]=='p' || token[0]=='-') note->tone=0;
136       else
137     if (token[0]=='a') note->tone=10;
138       else
139     if (token[0]=='h' || token[0]=='b') note->tone=12;
140       else
141     if (token[0]=='c') note->tone=1;
142       else
143     if (token[0]=='d') note->tone=3;
144       else
145     if (token[0]=='e') note->tone=5;
146       else
147     if (token[0]=='f') note->tone=6;
148       else
149     if (token[0]=='g') note->tone=8;
150 
151     note->tone=note->tone+tone_modifier;
152 
153     if (note->tone!=0)
154     {
155       t=gettoken(in,token,1);
156       note->scale=atoi(token)-1;
157     }
158 
159     if (note->tone>12)
160     {
161       note->tone=note->tone-12;
162       note->scale++;
163     }
164 
165     note_route(out,note,0,out_type);
166   }
167 
168   footer_route(out,note,0,out_type);
169 
170   return (0);
171 }
172 
173 
174 
175