📄 documentation-5.html
字号:
Tree class assists you in generating this string in this way, that it will gothrough the whole tree and call several functions on every stage trough theway. It will be your task to change the functions, so that a nice layout willbe generated.<P><P><H3>Instance variables</H3><P><P><CENTER><TABLE BORDER><TR><TD><BR>classname</TD><TD>Serialization helper: The name of this class.</TD></TR><TR><TD>delimiter</TD><TD>a char for truncating the "path"</TD></TR><TR><TD>tree</TD><TD>an array of an array of an array</TD></TR><TR><TD>outp</TD><TD>the "output"</TD></TR><TR><TD>prfx, sufx, flag</TD><TD>internal - some helpers to create outp<CAPTION>Accessible instance variables.</CAPTION></TD></TR></TABLE></CENTER><P><H3>Instance methods</H3><P><P><H3>Accessible instance methods</H3><P><P><DL><DT><B>build_tree()</B><DD><P>This function is completely user driven! You have to create anarray with the structure described below. See the example for details.<P>Don't be shy to create some own functions which are called by<CODE>build_tree()</CODE> - e.g. for recursive calls.<P><DT><B>go_trough_tree($key="",$path="",$depth=0,$lcount=0,$pcount=0)</B><DD><P><P>This is the most important function of this class. It will call the outputfunctions in the right order with the correct parameters.<P>All variables are optional. The parameters are perhaps useful, if youwant to display only partial trees, but this is not supported now.<P><DT><B>path_to_index (&$path,$key="")</B><DD><P>This function is mostly used internally, but could be usefulfor you to generate <CODE>$this->tree</CODE>. This function generatesa PHP3 associate array-index string from a path, which is alsoa string but truncated by <CODE>$this->delimiter</CODE>. If <CODE>$key</CODE>is given, it will be added to <CODE>$path</CODE> (minds empty path andso on).<P><P>Example:<P><BLOCKQUOTE><CODE><HR><PRE> $t->delimiter="/"; $path= "usr/local/lib"; ## $path must be given as a var, because it is called by reference! $bla = $t->path_to_index($path,"etc"); ## $path is now "usr/local/lib/etc" ## $bla is now ["usr"]["local"]["lib"]["etc"]</PRE><HR></CODE></BLOCKQUOTE><P><DT><B>path_to_parent (&$path)</B><DD><P>This function isn't used internally, but could be usefulfor you during generating the output tree. It will removeone from the depth of the path.<P>Example:<BLOCKQUOTE><CODE><HR><PRE> $t->delimiter="/"; $path= "usr/local/lib"; $bla = $t->path_to_parent($path); ## $path is now "usr/local" ## $bla is now ["usr"]["local"]</PRE><HR></CODE></BLOCKQUOTE><P><P><DT><B>path_add ($path,$key)</B><DD><P>This function is the 'non-call-by-reference-version' of<CODE>path_to_index</CODE>. It will add the $key tothe path and return it.<P><DT><B>path_sub ($path)</B><DD><P>This function is the 'non-call-by-reference-version' of<CODE>path_to_parent</CODE>. It will find theparent of path and return it.<P><DT><B>path_index ($path)</B><DD><P>This function is the 'non-call-by-reference-version' of<CODE>path_to_index()</CODE>. It will return theassociate key to the tree described by path.<P><P><P><DT><B>starttree ()</B><DD><P>This function is called by <CODE>go_trough_tree()</CODE>at the beginning of the output of a tree.<P>All <CODE>*tree</CODE>-functions are called by <CODE>go_trough_tree()</CODE>,but it's your turn, to give them a nice layout. I think it is possibleto generate nearly every kind of tree-layout with this. Have a look atthe variables: E.g. <CODE>$depth</CODE> makes it possible to handle every"level" in another manner.<P><DT><B>growtree ($key,$value,$path,$depth,$count,$pcount)</B><DD><P>This function is called by <CODE>go_trough_tree()</CODE>at the beginning of the output of a tree.<P>It is called every time, when <CODE>go_trough_tree()</CODE>will call itself recursively. You could also say it is called, whenthe current item has a successor.<P><P><DT><B>leaftree ($key,$value,$path,$depth,$count,$pcount)</B><DD><P>This function is called, when the current item has <EM>no</EM> successor.<P><DT><B>shrinktree ($key,$depth)</B><DD><P>This function is the "opposite" of <CODE>growtree()</CODE>. It iscalled every time, when the current item was the last item in thissub-list.<P><DT><B>endtree()</B><DD><P>Called when leaving tree.<P><P></DL><P><H3>The Tree Array </H3><P>As said above, before you call <CODE>go_trough_tree()</CODE>,first <CODE>$tree</CODE> must be generated.<P><CODE>$tree</CODE> consists of nested arrays of arbitrary depth. An example:<P><BLOCKQUOTE><CODE><HR><PRE>$t= new Tree;$t->tree = array( "usr" => array( 0 => "allowed", "lib" => "forbidden", "local" => "allowed", "bin" => "forbidden", "etc" => array( 0 => "allowed", "hosts" => "forbidden", "mailcap"=> "allowed" ), "var" => "allowed", "tmp" => "allowed" ), "root" =>"forbidden" );$t->go_through_tree();print $t->outp;</PRE><HR></CODE></BLOCKQUOTE><P>This is a completely recursive structure and I think, it is clear, howto create it with a recursive call of a function. If not, see theexample below.<P><P>One little quirk has to be explained, because it is a littlebit confusing: the array name 0 (zero) is used for the value of theparent element. As shown in the example, an element with children(for example "etc") cannot have attributes (such as "allowed").Instead the value of this element is stored in a pseudo-childnamed 0. If this element is not present, it will have the value"Array" (perhaps something that should be changed).<P><P>The output of this example if you don't change the output-functions willlook like this:<P><BLOCKQUOTE><CODE><HR><PRE>/^---- usr->'allowed' : 'usr' (1) [1/2]| ^---- lib->'forbidden' : 'usr^lib' (2) [2/7]| O---- local->'allowed' : 'usr^local' (2) [3/7]| O---- bin->'forbidden' : 'usr^bin' (2) [4/7]| O---- etc->'allowed' : 'usr^etc' (2) [5/7]| | ^---- hosts->'forbidden' : 'usr^etc^hosts' (3) [2/3]| | \--- mailcap->'allowed' : 'usr^etc^mailcap' (3) [3/3]| O---- var->'allowed' : 'usr^var' (2) [6/7]| \--- tmp->'allowed' : 'usr^tmp' (2) [7/7] \--- root->'forbidden' : 'root' (1) [2/2]</PRE><HR></CODE></BLOCKQUOTE><P>Looks a bit confusing. From left to right the fields are<UL><LI>The <EM>index-name</EM> of the current field</LI><LI>The <EM>value</EM> of this field</LI><LI>The <EM>full path</EM> to this field (see<CODE>path_to_*</CODE>-functions)</LI><LI>The current <EM>depth</EM> or <EM>level</EM></LI><LI>The current <EM>element number</EM>. See below to understand, whyit will begin sometimes with "2" in this example!</LI><LI>The <EM>number of elements</EM> in the subtree at this depth</LI></UL><P><P><H3>Example</H3><P><P>My example is just going trough the directory structure of your hard disk.<P><P>The following code could read it:<P><BLOCKQUOTE><CODE><HR><PRE>class dir_Tree extends Tree { var $classname = "dir_Tree"; var $delimiter="/"; var $tdat; function build_tree ($path=".") { $this->tree=$this->recurs_dir($path,0); } ## This example code can read and output 1000 directory entries with ## many subdirs in about 20 seconds on my system (P200, 64 MB); ## 220 dir entries with a maximum depth of 4 are read in 2 seconds. ## This is ok. :) function recurs_dir ($path,$depth) { GLOBAL $flap_out; $d=opendir($path); while ( $name=readdir($d) ) { $pathname=$path . $this->delimiter . $name; if (is_dir($pathname) && !ereg("\.\.?",$pathname)) { if (isset($flap_out[$pathname])) { $array[$name]=$this->recurs_dir($pathname,$depth+1); } # ATTENTION: It is IMPORTANT fill the [0] array # *after* filling the rest of the array! $array[$name][0]=$pathname; } else { $array[$name]=$pathname; } } closedir($d); return($array); } ################################################# ## FLAPPING IN and OUT ## This is used to create an array which includes ## all sub-paths which should be showed ## function flapping ($path) { GLOBAL $flap_out; if ($path) { if (is_dir($path)) { if (isset($flap_out[$path])) { unset($flap_out[$path]); } else { $flap_out[$path]=$name; } } } }}$t= new dir_Tree;$t->flapping($val); ## $val is given by GET-method, see *tree()-functions $t->build_tree();$t->go_through_tree();print $t->outp;</PRE><HR></CODE></BLOCKQUOTE><P>With this code it is very easy to flap in and out wholeparts of the tree. Send the path via GET-method and putthis path in <CODE>flapping()</CODE>. The whole <CODE>$flap_out</CODE>-array must bepersistent (e.g. via <EM>session</EM>). Perhaps you can program a garbagecollection, which will look into <CODE>$flap_out</CODE> and check for paths thatalready exist?<P><H3>Known Bugs / Tips</H3><P>There is one known bug: If a name of a subpath contains the<CODE>$delimiter</CODE>-string. This cannot be solved correctly and you haveto look for it when you create the tree.<P>The same thing is with the value [0] (zero) of a sub-array. This elementis always used as the attribute of the parent element.<P>A good tip: when youbuild your tree recursively then the [0]-index must be filled <EM>after</EM>the subtree is returned from recursive call. See in the example above what Imean. I think it's a PHP3 specialty.<P>Also it is possible that not every name could be inserted into theassociate index-field (Control-chars etc.), but this is untested.<P><H2><A NAME="ss5.7">5.7 STRINGS2 function set</A></H2><P>This is a set of functions, which are used very often by me.<P>They are so easy, that I now stop describing and simply insert the code.Perhaps the next revision of this set I will replace it with a betterdescription:<P><BLOCKQUOTE><CODE><HR><PRE><?php#### Strings2-Functions#### Copyright (c) 1998-2000 Alex 'SSilk' Aulbach#### These Functions are very practical and if I could program## C a little bit better it will be placed directly in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -