Work on STD parser for settings loading.

This commit is contained in:
Jonathan Moore Liles 2008-04-24 18:45:50 -05:00
parent f4a35bd519
commit 050a336abb
3 changed files with 108 additions and 1 deletions

View File

@ -119,6 +119,7 @@ Fl_Menu_Settings::dump ( Fl_Menu_ *bar, Fl_Menu_Item *menu, FILE *fp, int depth
return m;
}
/** dump menu to file /name/ starting at /item. */
int
Fl_Menu_Settings::dump ( Fl_Menu_Item *item, const char *name )
{
@ -134,3 +135,99 @@ Fl_Menu_Settings::dump ( Fl_Menu_Item *item, const char *name )
return true;
}
static void
path_push ( char *path, const char *s )
{
strcat( path, s );
strcat( path, "/" );
}
static void
path_pop ( char *path )
{
char *s;
int l = strlen( path );
if ( path[ l - 1 ] == '/' )
path[ l - 1 ] = '\0';
s = rindex( path, '/' );
s = s ? s : path;
*(s + 1) = '\0';
}
void
Fl_Menu_Settings::load ( Fl_Menu_ *bar, Fl_Menu_Item *item, FILE *fp, int depth, char *path, int pmax )
{
char line[256];
/* FIXME: overflow */
while ( ! feof( fp ) )
{
*line = '\0';
fgets( line, sizeof( line ), fp );
if ( *line == '#' )
continue;
line[ strlen( line ) - 1 ] = '\0';
int ld = strspn( line, "\t" );
if ( ld > depth )
{
path_push( path, line + ld );
++depth;
// load( bar, item, fp, depth + 1, path, pmax );
/* */;
}
else if ( ld < depth )
{
/* we should know the path and the value now */
// path_pop( path );
*rindex( path, '/' ) = '\0';
*rindex( path, '/' ) = '\0';
printf( "%s = %s\n", path, path + strlen( path ) + 1 );
while ( ld < depth )
{
path_pop( path );
depth--;
}
path_push( path, line + ld );
/* FIXME: still need to process the current line */
}
else /* d == depth */
{
/* doesn't apply? */
}
}
}
/** load settings from file /name/ into menu starting at /item */
int
Fl_Menu_Settings::load ( Fl_Menu_Item *item, const char *name )
{
FILE *fp = fopen( name, "r" );
if ( ! fp )
return false;
char path[256];
path[0] = '\0';
load( this, item, fp, 0, path, sizeof( path ) );
fclose( fp );
}

View File

@ -27,10 +27,15 @@ class Fl_Menu_Settings : public Fl_Menu_
void remove_ampersands ( char *str, int n );
void indent ( FILE *fp, int n );
Fl_Menu_Item * dump ( Fl_Menu_ *bar, Fl_Menu_Item *menu, FILE *fp, int depth );
void load ( Fl_Menu_ *bar, Fl_Menu_Item *item, FILE *fp, int depth, char *path, int pmax );
public:
int item_pathname_x ( char *path, int n, Fl_Menu_Item *item );
int dump ( Fl_Menu_Item *item, const char *name );
int load ( Fl_Menu_Item *item, const char *name );
};

View File

@ -98,9 +98,14 @@ Loggable::compact();}
}
MenuItem {} {
label Dump
callback {((Fl_Menu_Settings*)menubar)->dump( options_menu, "foo.state" );} selected
callback {((Fl_Menu_Settings*)menubar)->dump( options_menu, "foo.state" );}
xywh {0 0 40 25}
}
MenuItem {} {
label Load
callback {((Fl_Menu_Settings*)menubar)->load( options_menu, "foo.state" );} selected
xywh {10 10 40 25}
}
}
Submenu {} {
label {&Edit} open