📄 params.c
字号:
* successfully, else False. * * Notes: This function is in two parts. The first loop scans the * parameter name. Internal whitespace is compressed, and an * equal sign (=) terminates the token. Leading and trailing * whitespace is discarded. The second loop scans the parameter * value. When both have been successfully identified, they are * passed to pfunc() for processing. * * ------------------------------------------------------------------------ ** */ { int i = 0; /* Position within bufr. */ int end = 0; /* bufr[end] is current end-of-string. */ int vstart = 0; /* Starting position of the parameter value. */ char *func = "params.c:Parameter() -"; /* Read the parameter name. */ while( 0 == vstart ) /* Loop until we've found the start of the value. */ { if( i > (bSize - 2) ) /* Ensure there's space for next char. */ { bSize += BUFR_INC; bufr = realloc_array( bufr, char, bSize ); if( NULL == bufr ) { rprintf(FLOG, "%s Memory re-allocation failure.", func) ; return( False ); } } switch( c ) { case '=': /* Equal sign marks end of param name. */ if( 0 == end ) /* Don't allow an empty name. */ { rprintf(FLOG, "%s Invalid parameter name in config. file.\n", func ); return( False ); } bufr[end++] = '\0'; /* Mark end of string & advance. */ i = end; /* New string starts here. */ vstart = end; /* New string is parameter value. */ bufr[i] = '\0'; /* New string is nul, for now. */ break; case '\n': /* Find continuation char, else error. */ i = Continuation( bufr, i ); if( i < 0 ) { bufr[end] = '\0'; rprintf(FLOG, "%s Ignoring badly formed line in configuration file: %s\n", func, bufr ); return( True ); } end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); c = getc( InFile ); /* Read past eoln. */ break; case '\0': /* Shouldn't have EOF within param name. */ case EOF: bufr[i] = '\0'; rprintf(FLOG, "%s Unexpected end-of-file at: %s\n", func, bufr ); return( True ); default: if( isspace( c ) ) /* One ' ' per whitespace region. */ { bufr[end] = ' '; i = end + 1; c = EatWhitespace( InFile ); } else /* All others verbatim. */ { bufr[i++] = c; end = i; c = getc( InFile ); } } } /* Now parse the value. */ c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */ while( (EOF !=c) && (c > 0) ) { if( i > (bSize - 2) ) /* Make sure there's enough room. */ { bSize += BUFR_INC; bufr = realloc_array( bufr, char, bSize ); if( NULL == bufr ) { rprintf(FLOG, "%s Memory re-allocation failure.", func) ; return( False ); } } switch( c ) { case '\r': /* Explicitly remove '\r' because the older */ c = getc( InFile ); /* version called fgets_slash() which also */ break; /* removes them. */ case '\n': /* Marks end of value unless there's a '\'. */ i = Continuation( bufr, i ); if( i < 0 ) c = 0; else { for( end = i; end >= 0 && isSpace(bufr + end); end-- ) ; c = getc( InFile ); } break; default: /* All others verbatim. Note that spaces do */ bufr[i++] = c; /* not advance <end>. This allows trimming */ if( !isspace( c ) ) /* of whitespace at the end of the line. */ end = i; c = getc( InFile ); break; } } bufr[end] = '\0'; /* End of value. */ return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ } /* Parameter */static BOOL Parse( FILE *InFile, BOOL (*sfunc)(char *), BOOL (*pfunc)(char *, char *) ) /* ------------------------------------------------------------------------ ** * Scan & parse the input. * * Input: InFile - Input source. * sfunc - Function to be called when a section name is scanned. * See Section(). * pfunc - Function to be called when a parameter is scanned. * See Parameter(). * * Output: True if the file was successfully scanned, else False. * * Notes: The input can be viewed in terms of 'lines'. There are four * types of lines: * Blank - May contain whitespace, otherwise empty. * Comment - First non-whitespace character is a ';' or '#'. * The remainder of the line is ignored. * Section - First non-whitespace character is a '['. * Parameter - The default case. * * ------------------------------------------------------------------------ ** */ { int c; c = EatWhitespace( InFile ); while( (EOF != c) && (c > 0) ) { switch( c ) { case '\n': /* Blank line. */ c = EatWhitespace( InFile ); break; case ';': /* Comment line. */ case '#': c = EatComment( InFile ); break; case '[': /* Section Header. */ if (!sfunc) return True; if( !Section( InFile, sfunc ) ) return( False ); c = EatWhitespace( InFile ); break; case '\\': /* Bogus backslash. */ c = EatWhitespace( InFile ); break; default: /* Parameter line. */ if( !Parameter( InFile, pfunc, c ) ) return( False ); c = EatWhitespace( InFile ); break; } } return( True ); } /* Parse */static FILE *OpenConfFile( char *FileName ) /* ------------------------------------------------------------------------ ** * Open a configuration file. * * Input: FileName - The pathname of the config file to be opened. * * Output: A pointer of type (FILE *) to the opened file, or NULL if the * file could not be opened. * * ------------------------------------------------------------------------ ** */ { FILE *OpenedFile; char *func = "params.c:OpenConfFile() -"; if( NULL == FileName || 0 == *FileName ) { rprintf(FLOG, "%s No configuration filename specified.\n", func); return( NULL ); } OpenedFile = fopen( FileName, "r" ); if( NULL == OpenedFile ) { rsyserr(FLOG, errno, "unable to open configuration file \"%s\"", FileName); } return( OpenedFile ); } /* OpenConfFile */BOOL pm_process( char *FileName, BOOL (*sfunc)(char *), BOOL (*pfunc)(char *, char *) ) /* ------------------------------------------------------------------------ ** * Process the named parameter file. * * Input: FileName - The pathname of the parameter file to be opened. * sfunc - A pointer to a function that will be called when * a section name is discovered. * pfunc - A pointer to a function that will be called when * a parameter name and value are discovered. * * Output: TRUE if the file was successfully parsed, else FALSE. * * ------------------------------------------------------------------------ ** */ { int result; FILE *InFile; char *func = "params.c:pm_process() -"; InFile = OpenConfFile( FileName ); /* Open the config file. */ if( NULL == InFile ) return( False ); if( NULL != bufr ) /* If we already have a buffer */ result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ /* use it. */ else /* If we don't have a buffer */ { /* allocate one, then parse, */ bSize = BUFR_INC; /* then free. */ bufr = new_array( char, bSize ); if( NULL == bufr ) { rprintf(FLOG, "%s memory allocation failure.\n", func); fclose(InFile); return( False ); } result = Parse( InFile, sfunc, pfunc ); free( bufr ); bufr = NULL; bSize = 0; } fclose(InFile); if( !result ) /* Generic failure. */ { rprintf(FLOG, "%s Failed. Error returned from params.c:parse().\n", func); return( False ); } return( True ); /* Generic success. */ } /* pm_process *//* -------------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -