📄 params.c
字号:
/* lil-gp Genetic Programming System, version 1.0, 11 July 1995 * Copyright (C) 1995 Michigan State University * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Douglas Zongker (zongker@isl.cps.msu.edu) * Dr. Bill Punch (punch@isl.cps.msu.edu) * * Computer Science Department * A-714 Wells Hall * Michigan State University * East Lansing, Michigan 48824 * USA * */#include <lilgp.h>/** array of parameters, along with the size of the array and the number of parameters used. **/parameter *param;int param_size = 0, param_alloc = 0;/* read_parameter_file() * * reads and parses a parameter file. */void read_parameter_file ( char *filename ){ FILE *f; char buffer[MAXPARAMLINELENGTH+1]; char *buf2; int buf2size; int line = 0; int errors = 0; int including = 1; int flag; int buflen, buf2len; int cont; /** open it. **/ f = fopen ( filename, "r" ); if ( f == NULL ) error ( E_FATAL_ERROR, "can't open parameter file \"%s\".", filename ); /** lines are read into buffer. they are appended to buf2 until a line which is not continued is hit, then buf2 is parsed and the parameter added. **/ /* initial allocation of buf2. */ buf2 = (char *)MALLOC ( MAXPARAMLINELENGTH * sizeof ( char ) ); buf2size = MAXPARAMLINELENGTH; buf2[0] = 0; buf2len = 0; cont = 0; while ( fgets ( buffer, MAXPARAMLINELENGTH, f ) != NULL ) { ++line; /* remove comments, skip line if it's blank after comment removal. */ if ( delete_comment ( buffer ) ) continue; /** if this is a %ifdef, %ifndef, or %endif directive, then set the including variable accordingly. **/ if ( buffer[0] == '%' ) { flag = 0; if ( strncmp ( buffer+1, "ifdef", 5 ) == 0 ) including = test_directive ( buffer+6 ); else if ( strncmp ( buffer+1, "ifndef", 6 ) == 0 ) including = !test_directive ( buffer+7 ); else if ( strncmp ( buffer+1, "endif", 5 ) == 0 ) including = 1; else flag = 1; if ( !flag ) continue; } /* skip this line unless including is set. */ if ( !including ) continue; /** if this is a %define or %undefine directive, then process it. **/ if ( buffer[0] == '%' ) { if ( strncmp ( buffer+1, "define", 6 ) == 0 ) define_directive ( buffer+7 ); else if ( strncmp ( buffer+1, "undefine", 8 ) == 0 ) undefine_directive ( buffer+9 ); else error ( E_ERROR, "%s line %d: unknown directive.", filename, line ); continue; } /* is this line continued? (is the last nonwhitespace character a backslash?) */ cont = check_continuation ( buffer ); /** make buf2 bigger if necessary. **/ buflen = strlen ( buffer ); while ( buf2len + buflen > buf2size + 10 ) { buf2size += MAXPARAMLINELENGTH; buf2 = (char *)REALLOC ( buf2, buf2size * sizeof ( char ) ); } /** tack the current line onto buf2. **/ strcat ( buf2, buffer ); buf2len += buflen; /** if this line is not continued further, then parse buf2. and add the parameter. **/ if ( !cont ) { if ( parse_one_parameter ( buf2 ) ) { errors = 1; error ( E_ERROR, "%s line %d: syntax error.", filename, line ); } /** reset buf2 as empty. **/ buf2len = 0; buf2[0] = 0; } } /* close file. */ fclose ( f ); /** if the last line was continued, and nothing followed it, that's an error. **/ if ( buf2len != 0 ) { errors = 1; error ( E_ERROR, "unexpected EOF." ); } /** if any errors occurred during parsing, stop now. **/ if ( errors ) error ( E_FATAL_ERROR, "some syntax errors occurred parsing \"%s\".", filename ); FREE ( buf2 );}/* check_continuation() * * returns true/false depending on whether the given string is continued * (the last nonwhitespace character is a backslash). If so, the backslash * and everything after it is chopped. */int check_continuation ( char *buffer ){ int i, l; l = strlen ( buffer ); for ( i = l-1; i >= 0; --i ) if ( !isspace(buffer[i]) ) { if ( buffer[i] == '\\' ) { buffer[i] = '\n'; buffer[i+1] = 0; return 1; } else return 0; } /* a blank line was passed. */ return 0;}/* delete_comment() * * this searches the string for a '#' or ';' and chops everything * following, if one is found. returns 1 if the resulting line is blank * (all whitespace), 0 otherwise. */int delete_comment ( char *buffer ){ int i, l; l = strlen ( buffer ); /* zero-length lines are considered blank. */ if ( l == 0 ) return 1; /* if the last character is a newline, chop it. */ if ( buffer[--l] == '\n' ) buffer[l] = 0; for ( i = 0; i < l; ++i ) if ( buffer[i] == '#' || buffer[i] == ';' ) { /* chop the line at a '#' or ';'. */ buffer[i] = 0; break; } /* look for a nonwhitespace character. */ l = strlen ( buffer ); for ( i = 0; i < l; ++i ) if ( !isspace(buffer[i]) ) /* found one, return 0. */ return 0; /* blank line, return 1. */ return 1;}/* translate_binary() * * this translates all of the valid strings representing binary values * to the corresponding integer. */int translate_binary ( char *string ){ if ( strcmp ( string, "true" ) == 0 || strcmp ( string, "t" ) == 0 || strcmp ( string, "on" ) == 0 || strcmp ( string, "yes" ) == 0 || strcmp ( string, "y" ) == 0 || strcmp ( string, "1" ) == 0 ) return 1; else if ( strcmp ( string, "false" ) == 0 || strcmp ( string, "f" ) == 0 || strcmp ( string, "off" ) == 0 || strcmp ( string, "no" ) == 0 || strcmp ( string, "n" ) == 0 || strcmp ( string, "0" ) == 0 ) return 0; else return -1;}/* read_parameter_database() * * this reads parameters from a checkpoint file. */void read_parameter_database ( FILE *f ){ int i, j, k; int count; char *name, *value; char *buf1, *buf2; int buf2len, buf2alloc; /* how many parameters are we supposed to find? */ fscanf ( f, "%*s %d\n", &count ); if ( fgetc ( f ) != '#' ) error ( E_FATAL_ERROR, "error in parameter section of checkpoint file." ); buf1 = (char *)MALLOC ( MAXPARAMLINELENGTH ); buf2 = (char *)MALLOC ( MAXPARAMLINELENGTH ); buf2alloc = MAXPARAMLINELENGTH; buf2[0] = 0; buf2len = 0; for ( i = 0; i < count; ) { /* get a line in buf1. */ fgets ( buf1, MAXPARAMLINELENGTH, f ); /** lengthen buf2 if necessary. */ while ( buf2len + strlen ( buf1 ) >= buf2alloc ) { buf2 = (char *)REALLOC ( buf2, buf2alloc + MAXPARAMLINELENGTH ); buf2alloc += MAXPARAMLINELENGTH; } /** tack line onto buf2. **/ strcat ( buf2, buf1 ); buf2len += strlen ( buf1 ); /* get the first character of the next line. */ k = fgetc ( f ); if ( k != '+' ) { /** not a '+', so the line is not continued. **/ /* chop the final newline. */ buf2[buf2len-1] = 0; for ( j = 0; j < buf2len; ++j ) /* look for a " = " substring, and break it into name/value there. */ if ( buf2[j] == ' ' && buf2[j+1] == '=' && buf2[j+2] == ' ' ) { /** add the parameter. **/ buf2[j] = 0; add_parameter ( buf2, buf2+j+3, PARAM_COPY_NAME|PARAM_COPY_VALUE );#ifdef DEBUG fprintf ( stderr, "name = [%s]\nvalue = [%s]\n", buf2, buf2+j+3 );#endif } /** reset buf2. **/ buf2[0] = 0; buf2len = 0; /* count of how many we've found. */ ++i; } } /* put the extra character we read back. */ ungetc ( k, f ); FREE ( buf1 ); FREE ( buf2 );}/* write_parameter_database() * * this writes all the parameters to a checkpoint file, as "name = value\n". * since parameters can have embedded newlines, we begin each line of the * file with a "#" to indicate the start of a new name/value pair or a "+" * to indicate a continuation of the previous line. */void write_parameter_database ( FILE *f ){ int i, j; /* write the total count. */ fprintf ( f, "parameter-count: %d\n", param_size ); for ( i = 0; i < param_size; ++i ) { /* start the pair with a '#'. */ fputc ( '#', f ); /** write the name, adding '+' after newlines. */ for ( j = 0; j < strlen ( param[i].n ); ++j ) { fputc ( param[i].n[j], f ); if ( param[i].n[j] == '\n' ) fputc ( '+', f ); } /* write " = ". */ fputs ( " = ", f ); /** write the value, adding '+' after newlines. */ for ( j = 0; j < strlen ( param[i].v ); ++j ) { fputc ( param[i].v[j], f ); if ( param[i].v[j] == '\n' ) fputc ( '+', f ); } /* end the pair. */ fputc ( '\n', f ); }}/* initialize_parameters() * * initializes the parameter database. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -