File: uiFindNode.sci

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (148 lines) | stat: -rw-r--r-- 3,953 bytes parent folder | download
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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2009 - DIGITEO - Sylvestre Koumar
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at    
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt

function nodeList = uiFindNode(tree, node, value)

	[lhs,rhs]=argn(0);

	//Input arguments checking
	if rhs < 2 | rhs > 3 then
		error(msprintf(gettext("%s: Wrong number of input arguments: %d or %d expected.\n"), "uiFindNode",2,3));
		return;
	end

	// Check 1st and 2nd inputs : tree & (node or property)
	if rhs >= 2 then
		if (typeof(tree) == 'Tree') then
			myTree = tree;
			isNode = %F;
			isPosition = %F;
			isProperty = %F;
		else
			error(msprintf(gettext("%s: Wrong type for input argument #%d: Tree expected.\n"), "uiFindNode",1));
			return;
		end
		
		if (typeof(node) == 'Tree') then
			myNode = node;
			isNode = %T;
				
		elseif (type(node) == 10) then
			if rhs == 2
				myPosition = node;
				isPosition = %T;
			end
		else
			error(msprintf(gettext("%s: Wrong type for input argument #%d: Tree or String expected.\n"), "uiFindNode",2));
			return;				
		end
					
		// Check 3rd input : property's value
		if rhs == 3 then
			if (type(node) == 10 & type(value) == 10) then
				if (node == 'label' | node == 'icon' | node == 'callback') then
					myProperty = node;
					myValue = value;
					isProperty = %T;
				else
					error(msprintf(gettext("%s: Wrong type for input argument #%d: must be ''label'', ''icon'' or ''callback''.\n"), "uiFindNode",2));
					return;
				end
			else
				error(msprintf(gettext("%s: Wrong type for input argument #%d or #%d: String expected.\n"), "uiFindNode",2,3));
				return;	
			end
		end
	end
	
	// Find matching node(s)
	function r = internalFindNode(myTree, myNode, r)
	
		if uiEqualsTree(myTree, myNode) then
			r($+1) = myTree;
		end

		for index = 3:size(myTree)
			r = internalFindNode(myTree(index), myNode, r)
		end

	endfunction
	
	// Finding node with a given position
	function r = findPos(myTree, myPosition, r, curpos)
	
		if myPosition == curpos then
			r($+1) = myTree;
		end
		
		for index = 3:size(myTree)
			if curpos ~= "root" then
				r = findPos(myTree(index), myPosition, r, curpos+"."+string(index-2))
			else
				r = findPos(myTree(index), myPosition, r, string(index-2))
			end
		end

	endfunction
	
	// Finding node(s) with a given property
	function r = findProperty(myTree, myProperty, myValue, r)
	
		if myTree(2)(myProperty) == myValue then
			r($+1) = myTree;
		end
		
		for index = 3:size(myTree)
			r = findProperty(myTree(index), myProperty, myValue, r)
		end

	endfunction
	
	// List containing result
	r = list();
	
	// Find with a node
	if isNode then
		// List of matching nodes
		nodeList = internalFindNode(myTree, myNode, r);	
		if (size(nodeList) > 1) then
			warning(msprintf(gettext("%s:  #%d matching nodes.\n"), "uiFindNode",size(nodeList)));	
			return;
		elseif (size(nodeList) == 0) then
			warning(msprintf(gettext("%s: No results founded.\n"), "uiFindNode"));
			return;
		end
	end
	
	// Find with a position
	if isPosition then
		// List of matching nodes
		nodeList = findPos(myTree, myPosition, r, "root");
		if size(nodeList) == 0 then 
			error(msprintf(gettext("%s: Invalid position ''%s''.\n"), "uiFindNode",myPosition));
			return;
		end
	end
	
	// Find with a property	
	if isProperty then
		// List of matching nodes
		nodeList = findProperty(myTree, myProperty, myValue, r)
		if size(nodeList) == 0 then 
			warning(msprintf(gettext("%s: No results founded for property ''%s'' and value ''%s''.\n"), "uiFindNode",myProperty, myValue));
			return;
		else	
			if (size(nodeList) > 1) then
				warning(msprintf(gettext("%s:  #%d matching nodes.\n"), "uiFindNode",size(nodeList)));	
				return;
			end
		end
	end
	
endfunction