Mac OS X: modified Fl_Preferences::Node::search to correctly handle groups

inside the root group and to allow for relative and absolute path names.


git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@2619 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Matthias Melcher 2002-09-05 20:44:36 +00:00
parent 32b9640e1c
commit 6698be1be8
4 changed files with 65 additions and 14 deletions

View File

@ -1,5 +1,5 @@
//
// "$Id: Fl_Preferences.H,v 1.1.2.10 2002/08/27 03:03:34 easysw Exp $"
// "$Id: Fl_Preferences.H,v 1.1.2.11 2002/09/05 20:44:35 matthiaswm Exp $"
//
// Preferences definitions for the Fast Light Tool Kit (FLTK).
//
@ -112,7 +112,7 @@ private:
// node methods
int write( FILE *f );
Node *find( const char *path );
Node *search( const char *path );
Node *search( const char *path, int offset=0 );
Node *addChild( const char *path );
void setParent( Node *parent );
char remove();
@ -157,5 +157,5 @@ private:
#endif // !Fl_Preferences_H
//
// End of "$Id: Fl_Preferences.H,v 1.1.2.10 2002/08/27 03:03:34 easysw Exp $".
// End of "$Id: Fl_Preferences.H,v 1.1.2.11 2002/09/05 20:44:35 matthiaswm Exp $".
//

View File

@ -161,6 +161,9 @@ by <tt>groups()</tt>.
<H4><a name="Fl_Preferences.groupExists">int Fl_Preferences::groupExists(const char *groupname)</a></H4>
<P>Returns non-zero if a group with this name exists.
Groupnames are relative to the Preferences node and can contain a path.
<tt>"."</tt> describes the current node, <tt>"./"</tt> describes the topmost node.
By preceding a groupname with a <tt>"./"</tt>, its path becomes relative to the topmost node.
<H4><a name="Fl_Preferences.groups">int Fl_Preferences::groups()</a></H4>

View File

@ -1,5 +1,5 @@
//
// "$Id: Fl_Preferences.cxx,v 1.1.2.19 2002/08/27 03:03:37 easysw Exp $"
// "$Id: Fl_Preferences.cxx,v 1.1.2.20 2002/09/05 20:44:36 matthiaswm Exp $"
//
// Preferences methods for the Fast Light Tool Kit (FLTK).
//
@ -1013,19 +1013,48 @@ Fl_Preferences::Node *Fl_Preferences::Node::find( const char *path )
}
// find a group somewhere in the tree starting here
// caller must not set 'offset' argument
// - if the node does not exist, 'search' returns NULL
Fl_Preferences::Node *Fl_Preferences::Node::search( const char *path )
// - if the pathname is "." (current node) return this node
// - if the pathname is "./" (root node) return the topmost node
// - if the pathname starts with "./", start the search at the root node instead
Fl_Preferences::Node *Fl_Preferences::Node::search( const char *path, int offset )
{
int len = strlen( path_ );
if ( strncmp( path, path_, len ) == 0 )
if ( offset == 0 )
{
if ( path[ len ] == 0 )
if ( path[0] == '.' )
{
if ( path[1] == 0 )
{
return this; // user was searching for current node
}
else if ( path[1] == '/' )
{
Node *nn = this;
while ( nn->parent_ ) nn = nn->parent_;
if ( path[2]==0 )
{ // user is searching for root ( "./" )
return nn;
}
return nn->search( path+2, 2 ); // do a relative search on the root node
}
}
offset = strlen( path_ ) + 1;
}
int len = strlen( path_ );
if ( len < offset-1 ) return 0;
len -= offset;
if ( ( len <= 0 ) || ( strncmp( path, path_+offset, len ) == 0 ) )
{
if ( len > 0 && path[ len ] == 0 )
return this;
if ( path[ len ] == '/' )
if ( len <= 0 || path[ len ] == '/' )
{
for ( Node *nd = child_; nd; nd = nd->next_ )
{
Node *nn = nd->find( path );
Node *nn = nd->search( path, offset );
if ( nn ) return nn;
}
return 0;
@ -1084,5 +1113,5 @@ char Fl_Preferences::Node::remove()
//
// End of "$Id: Fl_Preferences.cxx,v 1.1.2.19 2002/08/27 03:03:37 easysw Exp $".
// End of "$Id: Fl_Preferences.cxx,v 1.1.2.20 2002/09/05 20:44:36 matthiaswm Exp $".
//

View File

@ -12,6 +12,8 @@ decl {\#include <stdlib.h>} {}
decl {\#include <FL/filename.H>} {}
decl {\#include <FL/fl_ask.H>} {}
decl {void readPrefs();} {public
}
@ -294,9 +296,26 @@ Function {writePrefs()} {open return_type void
eat.set( Fl_Preferences::Name( 3 ), "Test3" );
/** sample code only:
/* sample: create a sub-sub-group */
Fl_Preferences eatMore( eat, "More" );
eatMore.set( "more", "stuff" );
/* all the following searches should return 1 */
int sum = 0;
sum += app.groupExists( "Breakfast" ); /* find 'eat' relative to 'app' */
sum += app.groupExists( "Breakfast/More" ); /* find 'eat.eatMore' relative to 'app' */
sum += app.groupExists( "./Breakfast/More" ); /* find 'eat.eatMore' relative to Preferences */
sum += eat.groupExists( "More" ); /* find 'eatMore' relative to 'eat' */
sum += eat.groupExists( "./Breakfast/More" ); /* find 'eat.eatMore' relative to Preferences */
sum += eat.groupExists( "." ); /* find myself ('eat') */
sum += eat.groupExists( "./" ); /* find the topmost group ('app') */
if ( sum != 7 )
fl_message( "Assertion failed:\\nNot all group entries were found!" );
/* sample code only: */
unsigned int hex = 0x2387efcd;
eat.set( "binFoo", (void*)&hex, sizeof( unsigned int ) );
eat.set( "binFoo2", (void*)&writePrefs, 1024 );
**/} {}
eat.set( "binFoo2", (void*)&writePrefs, 256 );
} {}
}