File: uitree.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (215 lines) | stat: -rw-r--r-- 7,207 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/************************************************************************
 ************************************************************************
    FAUST compiler
	Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/
 
 
 
#include "uitree.hh"



static Tree makeSubFolderChain(Tree path, Tree elem);
static Tree putFolder(Tree folder, Tree item);
static Tree getFolder (Tree folder, Tree ilabel);


static void error(const char * s, Tree t)
{
	fprintf(stderr, "ERROR : %s (%p)\n", s, t);
}

#define ERROR(s,t) error(s,t); exit(1)


//------------------------------------------------------------------------------
// Property list
//------------------------------------------------------------------------------

#if 0
// version normale, qui marche, mais qui ne range pas en ordre alphabetique
static bool findKey (Tree pl, Tree key, Tree& val)
{
	if (isNil(pl)) 				return false;
	if (left(hd(pl)) == key) 	{ val= right(hd(pl)); return true; }
	/*  left(hd(pl)) != key	*/	return findKey (tl(pl), key, val); 
}

static Tree updateKey (Tree pl, Tree key, Tree val)
{
	if (isNil(pl)) 				return cons ( cons(key,val), nil );
	if (left(hd(pl)) == key) 	return cons ( cons(key,val), tl(pl) );
	/*  left(hd(pl)) != key	*/	return cons ( hd(pl), updateKey( tl(pl), key, val ));
}

static Tree removeKey (Tree pl, Tree key)
{
	if (isNil(pl)) 				return nil;
	if (left(hd(pl)) == key) 	return tl(pl);
	/*  left(hd(pl)) != key	*/	return cons (hd(pl), removeKey(tl(pl), key));
}

#else

// verion experimentale qui range en ordre alphabetique

static bool isBefore(Tree k1, Tree k2) 
{ 
	// before comparing replace (type . label) by label
	if (isList(k1)) { k1 = tl(k1); }
	if (isList(k2)) { k2 = tl(k2); }
	
	//fprintf(stderr, "isBefore("); print(k1, stderr); fprintf(stderr,", "); print(k2, stderr); fprintf(stderr,")\n"); 
	Sym s1, s2;
	if (!isSym(k1->node(), &s1)) {
		ERROR("the node of the tree is not a symbol", k1);
	}
	if (!isSym(k2->node(), &s2)) {
		ERROR("the node of the tree is not a symbol", k2);
	}
	
	//fprintf (stderr, "strcmp(\"%s\", \"%s\") = %d\n", name(s1), name(s2), strcmp(name(s1), name(s2)));
	return strcmp(name(s1), name(s2)) < 0;
}


static bool findKey (Tree pl, Tree key, Tree& val)
{
	if (isNil(pl)) 					return false;
	if (left(hd(pl)) == key) 		{ val = right(hd(pl)); return true; }
	if (isBefore(left(hd(pl)),key))	return findKey (tl(pl), key, val); 
	return false;
}

static Tree updateKey (Tree pl, Tree key, Tree val)
{
    if (isNil(pl))                  return cons ( cons(key,val), nil );
    if (left(hd(pl)) == key)        return cons ( cons(key,val), tl(pl) );
    if (isBefore(left(hd(pl)),key)) return cons ( hd(pl), updateKey( tl(pl), key, val ));
    return cons(cons(key,val), pl);
}

/**
 * Like updateKey but allow multiple items with same key
 */
static Tree addKey (Tree pl, Tree key, Tree val)
{
    if (isNil(pl))                  return cons ( cons(key,val), nil );
    if (isBefore(key, left(hd(pl)))) return cons(cons(key,val), pl);
    return cons ( hd(pl), addKey( tl(pl), key, val ));
}


#if 0
static Tree removeKey (Tree pl, Tree key)
{
	if (isNil(pl)) 					return nil;
	if (left(hd(pl)) == key) 		return tl(pl);
	if (isBefore(left(hd(pl)),key))	return cons (hd(pl), removeKey(tl(pl), key));
	return pl;
}
#endif
#endif

//------------------------------------------------------------------------------
// gestion de la construction de l'arbre d'interface utilisateur
//------------------------------------------------------------------------------

Sym 	UIFOLDER = symbol ("uiFolder");
Tree  	uiFolder(Tree label, Tree elements)				{ return tree(UIFOLDER, label, elements); 		}
bool  	isUiFolder(Tree t)								{ return isTree(t, UIFOLDER); 					}
bool  	isUiFolder(Tree t, Tree& label, Tree& elements)	{ return isTree(t, UIFOLDER, label, elements); 	}

Sym 	UIWIDGET = symbol ("uiWidget");
Tree 	uiWidget(Tree label, Tree varname, Tree sig) 					{ return tree(UIWIDGET, label, varname, sig); }
bool 	isUiWidget(Tree t, Tree& label, Tree& varname, Tree& sig)		{ return isTree(t, UIWIDGET, label, varname, sig); }



// place un item dans un folder. Remplace eventuellement l'lment de mme nom.
Tree putFolder(Tree folder, Tree item)
{
    Tree    label, content;
    
    if ( ! isUiFolder(folder, label, content)) { fprintf(stderr, "ERROR in addFolder : not a folder\n"); }
    return uiFolder(label, updateKey(content, uiLabel(item), item));
}

// place un item dans un folder. Sans Remplacement
Tree addToFolder(Tree folder, Tree item)
{
    Tree    label, content;
    
    if ( ! isUiFolder(folder, label, content)) { fprintf(stderr, "ERROR in addFolder : not a folder\n"); }
    return uiFolder(label, addKey(content, uiLabel(item), item));
}

// get an item from a folder (or return NIL)
Tree getFolder (Tree folder, Tree ilabel)
{
	Tree	flabel, content, item;
	if (!isUiFolder(folder, flabel, content)) 	{ fprintf(stderr, "ERROR in getFolder : not a folder\n"); }
	if (findKey(content, ilabel, item)) {
		return item;
	} else {
		return nil;
	}
}
	
// cre une chaine de dossiers correspondant  path et contenant in fine elem
Tree makeSubFolderChain(Tree path, Tree elem)
{
	if (isNil(path)) {
		return elem;
	} else {
		return putFolder(uiFolder(hd(path)), makeSubFolderChain(tl(path),elem));
	}
} 

	
Tree putSubFolder(Tree folder, Tree path, Tree item) 
{
	if (isNil(path)) {
        //return putFolder(folder, item);
        return addToFolder(folder, item);
	} else {
		Tree subfolder = getFolder(folder, hd(path));
		if (isUiFolder(subfolder)) {
			return putFolder(folder, putSubFolder(subfolder, tl(path), item));
		} else {
			return putFolder(folder, makeSubFolderChain(path, item));
		}
	}
}

	
/*
Fonctionnement des dossiers. 
Dossier  1 niveau : Un dossier contient une liste de choses reperes par un nom  :
	Dossier[(l1,d1)...(ln,dn)] 
ou (lx,dx) est une chose dx repre par un nom lx. On suppose les lx tous diffrents

On peut ajouter une chose  un dossier : Ajouter(Dossier, Chose) -> Dossier

Si le dossier contient deja qq chose de meme nom, cette chose est remplace par la nouvelle.

AJOUTER (Dossier[(l1,d1)...(ln,dn)], (lx,dx)) -> Dossier[(l1,d1)...(lx,dx)...(ln,dn)]

AJOUTER (Dossier[(l1,d1)...(lx,dx)...(ln,dn)], (lx,dx')) -> Dossier[(l1,d1)...(lx,dx')...(ln,dn)]
*/