FAUST compiler  0.9.9.6b8
description.cpp
Go to the documentation of this file.
1 //------------------------------------
2 // generation of an xml description
3 //------------------------------------
4 #include <map>
5 #include <set>
6 #include <string>
7 
8 #include "description.hh"
9 #include "Text.hh"
10 
14 void extractMetadata(const string& fulllabel, string& label, map<string, set<string> >& metadata)
15 {
16  enum {kLabel, kEscape1, kEscape2, kEscape3, kKey, kValue};
17  int state = kLabel; int deep = 0;
18  string key, value;
19 
20  for (size_t i=0; i < fulllabel.size(); i++) {
21  char c = fulllabel[i];
22  switch (state) {
23  case kLabel :
24  assert (deep == 0);
25  switch (c) {
26  case '\\' : state = kEscape1; break;
27  case '[' : state = kKey; deep++; break;
28  default : label += c;
29  }
30  break;
31 
32  case kEscape1 :
33  label += c;
34  state = kLabel;
35  break;
36 
37  case kEscape2 :
38  key += c;
39  state = kKey;
40  break;
41 
42  case kEscape3 :
43  value += c;
44  state = kValue;
45  break;
46 
47  case kKey :
48  assert (deep > 0);
49  switch (c) {
50  case '\\' : state = kEscape2;
51  break;
52 
53  case '[' : deep++;
54  key += c;
55  break;
56 
57  case ':' : if (deep == 1) {
58  state = kValue;
59  } else {
60  key += c;
61  }
62  break;
63  case ']' : deep--;
64  if (deep < 1) {
65  metadata[rmWhiteSpaces(key)].insert("");
66  state = kLabel;
67  key="";
68  value="";
69  } else {
70  key += c;
71  }
72  break;
73  default : key += c;
74  }
75  break;
76 
77  case kValue :
78  assert (deep > 0);
79  switch (c) {
80  case '\\' : state = kEscape3;
81  break;
82 
83  case '[' : deep++;
84  value += c;
85  break;
86 
87  case ']' : deep--;
88  if (deep < 1) {
89  metadata[rmWhiteSpaces(key)].insert(rmWhiteSpaces(value));
90  state = kLabel;
91  key="";
92  value="";
93  } else {
94  value += c;
95  }
96  break;
97  default : value += c;
98  }
99  break;
100 
101  default :
102  cerr << "ERROR unrecognized state " << state << endl;
103  }
104  }
105  label = rmWhiteSpaces(label);
106 }
107 
108 //------------------------ specific schema -------------------------
109 
110 string extractName(Tree fulllabel)
111 {
112  string name;
113  map<string, set<string> > metadata;
114 
115  extractMetadata(tree2str(fulllabel), name, metadata);
116  return name;
117 }
118 
119 
123 static string xmlize(const string& fullsrc)
124 {
125  map<string, set<string> > metadata;
126  string dst;
127  string src;
128 
129  extractMetadata(fullsrc, src, metadata);
130 
131  for (size_t i=0; i<src.size(); i++) {
132  if (src[i] == '"' & (i==0 | i==src.size()-1)) {
133  // nothing to do just skip the quotes
134  } else {
135  switch (src[i]) {
136  case '<' : dst += "&lt;"; break;
137  case '>' : dst += "&gt;"; break;
138  case '&' : dst += "&amp;"; break;
139  default : dst += src[i];
140  }
141  }
142  }
143  return dst;
144 }
145 
146 void Description::print(int n, ostream& fout)
147 {
148  list<string>::iterator s;
149  list<int>::iterator t;
150 
151  tab(n,fout); fout << "<faust>";
152 
153  tab(n+1,fout); fout << "<name>" << xmlize(fName) << "</name>";
154  tab(n+1,fout); fout << "<author>" << xmlize(fAuthor) << "</author>";
155  tab(n+1,fout); fout << "<copyright>" << xmlize(fCopyright) << "</copyright>";
156  tab(n+1,fout); fout << "<license>" << xmlize(fLicense) << "</license>";
157  tab(n+1,fout); fout << "<version>" << xmlize(fVersion) << "</version>";
158  tab(n+1,fout); fout << "<classname>" << xmlize(fClassName) << "</classname>";
159  tab(n+1,fout); fout << "<inputs>" << fInputs << "</inputs>";
160  tab(n+1,fout); fout << "<outputs>" << fOutputs << "</outputs>";
161 
162  tab(n+1,fout); fout << "<ui>";
163 
164  // active widget list
165  tab(n+2,fout); fout << "<activewidgets>";
166  tab(n+3,fout); fout << "<count>" << fActiveWidgetCount << "</count>";
167  for (s = fActiveLines.begin(); s != fActiveLines.end(); s++) {
168  tab(n+3, fout); fout << *s;
169  }
170  tab(n+2,fout); fout << "</activewidgets>";
171 
172  tab(n+2,fout);
173 
174  // passive widget list
175  tab(n+2,fout); fout << "<passivewidgets>";
176  tab(n+3,fout); fout << "<count>" << fPassiveWidgetCount << "</count>";
177  for (s = fPassiveLines.begin(); s != fPassiveLines.end(); s++) {
178  tab(n+3, fout); fout << *s;
179  }
180  tab(n+2,fout); fout << "</passivewidgets>";
181 
182 
183  tab(n+2,fout);
184 
185  // widget layout
186  tab(n+2,fout); fout << "<layout>";
187  for ( t = fLayoutTabs.begin(), s = fLayoutLines.begin();
188  s != fLayoutLines.end(); t++, s++) {
189  tab(n+3+*t, fout); fout << *s;
190  }
191  tab(n+2,fout); fout << "</layout>";
192 
193  tab(n+1,fout); fout << "</ui>";
194 
195 
196  tab(n,fout); fout << "</faust>" << endl;
197 
198 }
199 
201 {
202  addGroup(0,t);
203 }
204 
205 
206 void Description::addGroup(int level, Tree t)
207 {
208  Tree label, elements, varname, sig;
209  const char* groupnames[] = {"vgroup", "hgroup", "tgroup"};
210 
211  if (isUiFolder(t, label, elements)) {
212 
213  const int orient = tree2int(left(label));
214  const char * str = tree2str(right(label));
215 
216  addLayoutLine(level, subst("<group type=\"$0\">", groupnames[orient]));
217  addLayoutLine(level+1, subst("<label>$0</label>", xmlize(str)));
218  while (!isNil(elements)) {
219  addGroup(level+1, right(hd(elements)));
220  elements = tl(elements);
221  }
222  addLayoutLine(level, "</group>");
223 
224  } else if (isUiWidget(t, label, varname, sig)) {
225 
226  int w = addWidget(label, varname, sig);
227  addLayoutLine(level, subst("<widgetref id=\"$0\" />", T(w)));
228 
229  } else {
230 
231  fprintf(stderr, "error in user interface generation 2\n");
232  exit(1);
233 
234  }
235 }
236 
237 void Description::tab (int n, ostream& fout)
238 {
239  fout << '\n';
240  while (n--) fout << '\t';
241 }
242 
243 int Description::addWidget(Tree label, Tree varname, Tree sig)
244 {
245  Tree path, c, x, y, z;
246 
247  // add an active widget description
248 
249  if ( isSigButton(sig, path) ) {
250 
251  fWidgetID++;
253  addActiveLine(subst("<widget type=\"button\" id=\"$0\">", T(fWidgetID)));
254  addActiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
255  addActiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
256  addActiveLine("</widget>");
257 
258  } else if ( isSigCheckbox(sig, path) ) {
259 
260  fWidgetID++;
262  addActiveLine(subst("<widget type=\"checkbox\" id=\"$0\">", T(fWidgetID)));
263  addActiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
264  addActiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
265  addActiveLine("</widget>");
266 
267  } else if ( isSigVSlider(sig, path,c,x,y,z) ) {
268 
269  fWidgetID++;
271  addActiveLine(subst("<widget type=\"vslider\" id=\"$0\">", T(fWidgetID)));
272  addActiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
273  addActiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
274  addActiveLine(subst("\t<init>$0</init>", T(tree2double(c))));
275  addActiveLine(subst("\t<min>$0</min>", T(tree2double(x))));
276  addActiveLine(subst("\t<max>$0</max>", T(tree2double(y))));
277  addActiveLine(subst("\t<step>$0</step>", T(tree2double(z))));
278  addActiveLine("</widget>");
279 
280  } else if ( isSigHSlider(sig, path,c,x,y,z) ) {
281 
282  fWidgetID++;
284  addActiveLine(subst("<widget type=\"hslider\" id=\"$0\">", T(fWidgetID)));
285  addActiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
286  addActiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
287  addActiveLine(subst("\t<init>$0</init>", T(tree2double(c))));
288  addActiveLine(subst("\t<min>$0</min>", T(tree2double(x))));
289  addActiveLine(subst("\t<max>$0</max>", T(tree2double(y))));
290  addActiveLine(subst("\t<step>$0</step>", T(tree2double(z))));
291  addActiveLine("</widget>");
292 
293  } else if ( isSigNumEntry(sig, path,c,x,y,z) ) {
294 
295  fWidgetID++;
297  addActiveLine(subst("<widget type=\"nentry\" id=\"$0\">", T(fWidgetID)));
298  addActiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
299  addActiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
300  addActiveLine(subst("\t<init>$0</init>", T(tree2double(c))));
301  addActiveLine(subst("\t<min>$0</min>", T(tree2double(x))));
302  addActiveLine(subst("\t<max>$0</max>", T(tree2double(y))));
303  addActiveLine(subst("\t<step>$0</step>", T(tree2double(z))));
304  addActiveLine("</widget>");
305 
306 
307  // add a passive widget description
308 
309  } else if ( isSigVBargraph(sig,path,x,y,z) ) {
310 
311  fWidgetID++;
313  addPassiveLine(subst("<widget type=\"vbargraph\" id=\"$0\">", T(fWidgetID)));
314  addPassiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
315  addPassiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
316  addPassiveLine(subst("\t<min>$0</min>", T(tree2double(x))));
317  addPassiveLine(subst("\t<max>$0</max>", T(tree2double(y))));
318  addPassiveLine("</widget>");
319 
320  } else if ( isSigHBargraph(sig,path,x,y,z) ) {
321 
322  fWidgetID++;
324  addPassiveLine(subst("<widget type=\"hbargraph\" id=\"$0\">", T(fWidgetID)));
325  addPassiveLine(subst("\t<label>$0</label>", xmlize(tree2str(label))));
326  addPassiveLine(subst("\t<varname>$0</varname>", tree2str(varname)));
327  addPassiveLine(subst("\t<min>$0</min>", T(tree2double(x))));
328  addPassiveLine(subst("\t<max>$0</max>", T(tree2double(y))));
329  addPassiveLine("</widget>");
330 
331  } else {
332  fprintf(stderr, "Error describing widget : unrecognized expression\n");
333  exit(1);
334  }
335 
336  return fWidgetID;
337 }
338 
339 
340 
bool isSigHSlider(Tree s)
Definition: signals.cpp:217
void tab(int n, ostream &fout)
bool isSigCheckbox(Tree s)
Definition: signals.cpp:205
bool isSigNumEntry(Tree s)
Definition: signals.cpp:257
list< string > fLayoutLines
Definition: description.hh:39
Tree left(Tree t)
Definition: list.hh:170
list< string > fActiveLines
Definition: description.hh:37
int tree2int(Tree t)
if t has a node of type int, return it otherwise error
Definition: tree.cpp:230
void ui(Tree t)
int addWidget(Tree label, Tree varname, Tree sig)
void addActiveLine(const string &l)
Definition: description.hh:77
A CTree = (Node x [CTree]) is a Node associated with a list of subtrees called branches.
Definition: tree.hh:109
string fClassName
Definition: description.hh:31
void addGroup(int level, Tree t)
Tree hd(Tree l)
Definition: list.hh:133
string fVersion
Definition: description.hh:29
Tree right(Tree t)
Definition: list.hh:171
bool isSigVBargraph(Tree s)
Definition: signals.cpp:285
list< int > fLayoutTabs
Definition: description.hh:40
bool isSigVSlider(Tree s)
Definition: signals.cpp:237
string fCopyright
Definition: description.hh:27
bool isNil(Tree l)
Definition: list.hh:137
void print(int n, ostream &fout)
string subst(const string &model, const vector< string > &args)
Text substitution.
Definition: Text.cpp:47
bool isSigHBargraph(Tree s)
Definition: signals.cpp:279
string T(char *c)
Definition: Text.cpp:158
int fActiveWidgetCount
Definition: description.hh:35
void extractMetadata(const string &fulllabel, string &label, map< string, set< string > > &metadata)
Extracts metdata from a label : 'vol [unit: dB]' -> 'vol' + metadata.
Definition: description.cpp:14
bool isUiWidget(Tree t, Tree &label, Tree &varname, Tree &sig)
Definition: uitree.cpp:141
string rmWhiteSpaces(const string &s)
Definition: Text.cpp:222
string fName
Definition: description.hh:25
string extractName(Tree fulllabel)
const char * name(Symbol *sym)
Returns the name of a symbol.
Definition: symbol.hh:98
int fPassiveWidgetCount
Definition: description.hh:36
list< string > fPassiveLines
Definition: description.hh:38
string fLicense
Definition: description.hh:28
double tree2double(Tree t)
if t has a node of type float, return it otherwise error
Definition: tree.cpp:262
string fAuthor
Definition: description.hh:26
bool isUiFolder(Tree t)
Definition: uitree.cpp:136
void addPassiveLine(const string &l)
Definition: description.hh:78
static string xmlize(const string &fullsrc)
removes enclosing quotes and transforms '<', '>' and '&' characters
Tree tl(Tree l)
Definition: list.hh:134
const char * tree2str(Tree t)
if t has a node of type symbol, return its name otherwise error
Definition: tree.cpp:278
void addLayoutLine(int n, const string &l)
Definition: description.hh:79
bool isSigButton(Tree s)
Definition: signals.cpp:199