d692ac17d062c4b8f4e5a176a9d9176a81d02518
6 char *chuck_faust_template
[] = {
7 #include "chuck_faust.template.h"
11 char *ctrl_cget_query
[] = {
12 "\n func = make_new_mfun( \"float\", \"%var_label%\",",
13 " %dsp_name%_ctrl_%var_name% );",
14 " func->add_arg( \"float\", \"%var_label%\" );",
15 " if (!type_engine_import_mfun( env, func ))",
20 char *ctrl_cget_funcs
[] = {
21 "static void %dsp_name%_ctrl_%var_name%( Chuck_Object * SELF, void * ARGS,",
22 " Chuck_DL_Return * RETURN,",
23 " Chuck_VM_Shred * SHRED)",
25 " %dsp_name% *d = (%dsp_name%*)OBJ_MEMBER_UINT(SELF, %dsp_name%_offset_data);",
26 " d->%var_name% = (SAMPLE)GET_CK_FLOAT(ARGS);",
27 " RETURN->v_float = (t_CKFLOAT)(d->%var_name%);",
33 typedef struct _variable_t
37 struct _variable_t
*next
;
41 variable_t
*current_v
= &variables
;
43 char dspname
[256] = "mydsp";
45 char outfilename
[2048];
47 void strip(char *to
, char *from
, int quotes
, int replace_spaces
)
50 while (from
[i
] && (from
[i
]==' ' || from
[i
]=='\t'
51 || (from
[i
]=='"' && quotes
)))
54 if (replace_spaces
&& from
[i
]==' ')
61 while (j
>0 && (to
[j
]==' ' || to
[j
]=='\t'
62 || (to
[j
]=='"' && quotes
)
63 || (to
[j
]=='_' && replace_spaces
) ))
67 void on_beg_tag(char *name
)
69 /* TODO: here would be a good place for parsing attributes if we
70 * were interested in doing so. */
72 if (strncmp(name
, "widget", 6)==0) {
73 if (name
[6]!=0 && name
[6]!=' ')
75 current_v
->next
= malloc(sizeof(variable_t
));
76 current_v
= current_v
->next
;
82 void on_end_tag(char *name
, char *value
)
84 if (strcmp(name
, "widget")==0) {
88 else if (strcmp(name
, "varname")==0 && in_widget
) {
89 strip(current_v
->name
, value
, 1, 1);
92 else if (strcmp(name
, "label")==0 && in_widget
) {
93 strip(current_v
->label
, value
, 1, 1);
96 else if (strcmp(name
, "name")==0) {
97 strip(dspname
, value
, 1, 1);
102 * See the technical section of README for an explanation of this insanity.
105 int parseXml(FILE* f
)
123 state_t state
= in_text
;
125 char c
= 0, last_c
= 0;
126 stack_frame stack
[50];
135 if (c
=='\r' || c
=='\n') {
136 if (last_c
!='\r' && last_c
!='\n')
145 on_beg_tag(stack
[level
].buf
);
147 if (stack
[level
].no_text
) {
152 stack
[level
].pos
= 0;
153 stack
[level
].buf
[0] = 0;
156 if (c
== '/' && last_c
== '<') {
157 state
= in_close_tag
;
161 stack
[level
].no_text
= 1; // empty tag
164 stack
[level
].buf
[stack
[level
].pos
++] = c
;
165 stack
[level
].buf
[stack
[level
].pos
] = 0;
170 if (strncmp(stack
[level
].buf
, stack
[level
-2].buf
,
171 strlen(stack
[level
].buf
))!=0) {
172 printf("Error with '%s', line %d\n",
173 stack
[level
].buf
, line
);
177 on_end_tag(stack
[level
+3].buf
, stack
[level
+2].buf
);
180 stack
[level
].buf
[stack
[level
].pos
++] = c
;
181 stack
[level
].buf
[stack
[level
].pos
] = 0;
187 stack
[level
].pos
= 0;
188 stack
[level
].buf
[0] = 0;
189 stack
[level
].no_text
= 0;
192 stack
[level
].buf
[stack
[level
].pos
++] = c
;
193 stack
[level
].buf
[stack
[level
].pos
] = 0;
201 void do_template(char *template[]);
203 int on_replace(char *var
)
205 if (strcmp(var
, "dsp_name")==0) {
206 fprintf(out
, dspname
);
208 else if (strcmp(var
, "var_name")==0) {
209 fprintf(out
, current_v
->name
);
211 else if (strcmp(var
, "var_label")==0) {
212 fprintf(out
, current_v
->label
);
214 else if (strcmp(var
, "ctrl_cget_functions")==0) {
215 variable_t
*v
= variables
.next
;
218 do_template(ctrl_cget_funcs
);
222 else if (strcmp(var
, "ctrl_cget_query")==0) {
223 variable_t
*v
= variables
.next
;
226 do_template(ctrl_cget_query
);
235 void do_template(char *template[])
240 while (template[line
])
242 int len
= strlen(template[line
]);
243 for (pos
=0; pos
< len
; pos
++) {
244 char c
= template[line
][pos
];
249 while (template[line
][pos
] && template[line
][pos
]!='%')
250 str
[k
++] = template[line
][pos
++];
252 if (on_replace(str
)) {
258 fprintf(out
,"%c", template[line
][pos
]);
265 int main(int argc
, char *argv
[])
272 printf("Usage: faust2ck <filename.dsp.xml>\n");
277 fxml
= fopen(argv
[1], "r");
279 printf("Error: Could not open %s.\n", argv
[1]);
284 if (parseXml(fxml
)) {
285 printf("Error parsing XML in %s\n", argv
[1]);
293 // determine output file name
294 strcpy(outfilename
, argv
[1]);
295 i
=strlen(outfilename
)-1;
296 while (i
>0 && outfilename
[i
]!='.')
298 if (i
==0) i
=strlen(outfilename
);
299 strcpy(&outfilename
[i
], "-wrapper.cpp");
301 out
= fopen(outfilename
, "w");
303 printf("Could not open output file %s\n", outfilename
);
308 do_template(chuck_faust_template
);
315 if (out
&& out
!=stdout
)
318 variable_t
*v
= variables
.next
;
320 variable_t
*t
= v
->next
;