#include #include #include "debug.h" #include "document.h" #include "clean_spaces.h" #include "parse_config.h" config_types resolve_keyword( const char *kw ) { int c; for( c = 0; c < TOTAL_KEYWORDS; c++ ) { dmsg( 5, "Checking keyword match, '%s' == '%s'", kw, conflist[ c ].keyword ); if( strcmp( kw, conflist[ c ].keyword ) == 0 ) { dmsg( 3, "Keyword resolved: '%s' = %i", kw, conflist[ c ].val ); return( conflist[ c ].val ); } } dmsg( 2, "Unknown keyword: '%s'", kw ); return( C_UNKNOWN ); } int parse_config( document *d, const char *config_file ) { /* Parse a config file and put options in *d */ FILE *in; char line[ LINE_LENGTH ], *keyword, *value; int c, foundspaces, l; dmsg( 3, "Trying to parse config: '%s'", config_file ); in = fopen( config_file, "r" ); if( ! in ) { dmsg( 2, "Could not open config: '%s'", config_file ); return( 1 ); } while( ! feof( in ) ) { if( fgets( line, LINE_LENGTH, in ) == NULL ) { break; } dmsg( 4, "Read line: '%s'", line ); /* Strip comments */ l = strlen( line ); for( c = 0; c < l; c++ ) { if( line[ c ] == '#' ) { line[ c ] = 0; } if( line[ c ] == '\n' ) { line[ c ] = 0; } } dmsg( 4, "Line after cleaned: '%s'", line ); /* Proc line, keyword value */ foundspaces = 0; keyword = line; value = NULL; l = strlen( line ); for( c = 0; c < l; c++ ) { dmsg( 4, "Loop %i, (%c)", c, line[ c ] ); if( ( line[ c ] == ' ' ) || ( line[ c ] == '\t' ) ) { dmsg( 5, "Space character" ); if( ! foundspaces ) { /* Cut the string here */ line[ c ] = 0; } foundspaces = 1; }else if( ( foundspaces ) && ( value == NULL ) ) { /* If we already foundspaces and we're here and our value is NULL, then this is the value half ptr */ dmsg( 3, "Value at %i", c ); value = &line[ c ]; } } /* Now switch on the values */ dmsg( 4, "Keyword: '%s'", keyword ); if( value ) { clean_spaces( value, CL_BOTH ); dmsg( 2, "keyword: '%s', value: '%s'", keyword, value ); switch( resolve_keyword( keyword ) ) { case C_DEBUG: sscanf( value, "%i", &c ); debug_level( c ); break; case C_CPL: sscanf( value, "%i", &d -> chars_per_line ); break; case C_LPP: sscanf( value, "%i", &d -> lines_per_page ); break; case C_TOP_MARGIN: sscanf( value, "%f", &d -> top_margin ); break; case C_LEFT_MARGIN: sscanf( value, "%f", &d -> left_margin ); break; case C_FONT_SIZE: sscanf( value, "%f", &d -> font_size ); break; case C_FONT_FACE: d -> font_face = strdup( value ); break; case C_PAGE_MODE: document_set_page( d, value ); break; case C_AUTHOR: d -> author = strdup( value ); break; case C_SUBJECT: d -> author = strdup( value ); break; case C_TITLE: d -> author = strdup( value ); break; case C_EXTRA_KEY: d -> extra_key = strdup( value ); break; case C_EXTRA_DATA: d -> extra_data = strdup( value ); break; case C_UNKNOWN: dmsg( 0, "Unknown keyword: '%s'", keyword ); break; } } } fclose( in ); return( 0 ); }