⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 params.c

📁 Genetic Programing of music
💻 C
📖 第 1 页 / 共 2 页
字号:
void initialize_parameters ( void ){     oputs ( OUT_SYS, 30, "    parameter database.\n" );          param = (parameter *)MALLOC ( PARAMETER_MINSIZE * sizeof ( parameter ) );     param_alloc = PARAMETER_MINSIZE;     param_size = 0;}/* free_parameters() * * frees all the parameters. */void free_parameters ( void ){     int i;     for ( i = 0; i < param_size; ++i )     {	  /* if add_parameter made a copy of the name, then free it. */          if ( param[i].copyflags & PARAM_COPY_NAME )               FREE ( param[i].n );	  /* if add_parameter make a copy of the value, then free it. */          if ( param[i].copyflags & PARAM_COPY_VALUE )               FREE ( param[i].v );     }          FREE ( param );     param = NULL;     param_alloc = 0;     param_size = 0;}/* add_parameter() * * adds the given name/value pair to the database.  the flags indicate * which if any of the strings need to be copied. */void add_parameter ( char *name, char *value, int copyflags ){     /* erase any existing parameter of the same name. */     delete_parameter ( name );     /** if the database is full, make it bigger. **/     while ( param_alloc < param_size+1 )     {          param_alloc += PARAMETER_CHUNKSIZE;          param = (parameter *)REALLOC ( param,                                       param_alloc * sizeof ( parameter ) );     }     /** add the name. **/     if ( copyflags & PARAM_COPY_NAME )     {	  /* make a copy of the string if requested. */          param[param_size].n = (char *)MALLOC ( strlen(name)+1 );          strcpy ( param[param_size].n, name );     }     else	  /* just store the pointer passed to us. */          param[param_size].n = name;     /** add the value. **/     if ( copyflags & PARAM_COPY_VALUE )     {	  /* make a copy of the string if requested. */          param[param_size].v = (char *)MALLOC ( strlen(value)+1 );          strcpy ( param[param_size].v, value );     }     else	  /* just store the pointer passed to us. */          param[param_size].v = value;     /* record whether our values are copies or not. */     param[param_size].copyflags = copyflags;          ++param_size;}/* delete_parameter() * * deletes a parameter from the database. */int delete_parameter ( char *name ){     int i;     for ( i = 0; i < param_size; ++i )          if ( strcmp ( name, param[i].n ) == 0 )          {	       /** free any copies make by add_parameter. **/               if ( param[i].copyflags & PARAM_COPY_NAME )                    FREE ( param[i].n );               if ( param[i].copyflags & PARAM_COPY_VALUE )                    FREE ( param[i].v );	       /** move the last value in the database to the position		 of the deleted one. **/               if ( param_size-1 != i )               {                    param[i].n = param[param_size-1].n;                    param[i].v = param[param_size-1].v;                    param[i].copyflags = param[param_size-1].copyflags;               }               --param_size;               return 1;          }     return 0;}/* get_parameter() * * looks up a parameter in the database. */char *get_parameter ( char *name ){     int i;     for ( i = 0; i < param_size; ++i )          if ( strcmp ( name, param[i].n ) == 0 )               return param[i].v;     return NULL;}/* print_parameters() * * dumps parameter database to stdout. */void print_parameters ( void ){     int i;     for ( i = 0; i < param_size; ++i )          printf ( "name: \"%s\"  value: \"%s\"  copy: %d\n",                  param[i].n, param[i].v, param[i].copyflags );     }/* parse_one_parameter() * * breaks a string at the first equals sign into name and value parts. * removes leading and trailing whitespace from both parts. inserts the * resulting pair into the parameter database. */int parse_one_parameter ( char *buffer ){     char name[MAXPARAMLINELENGTH+1];     char data[MAXPARAMLINELENGTH+1];     int i, j, k, l;     int n, d;     k = -1;     j = 0;     l = strlen ( buffer );     /** scan for a equals sign. **/      for ( i = 0; i < l; ++i )     {	  /* j records whether or not we have found a nonwhitespace	    character. */          j += (buffer[i] != ' ' && buffer[i] != '\t' && buffer[i] != '\n');          if ( buffer[i] == '=' )          {               k = i;	       /* copy the name part. */               strncpy ( name, buffer, k );               name[k] = 0;	       /* copy the value part. */               strcpy ( data, buffer+k+1 );               break;          }     }     /* if we found no '=', return an error unless the line was	completely blank. */     if ( k == -1 )          return !!j;     /* trim leading and trailing whitespace. */     n = trim_string ( name );     d = trim_string ( data );     /** if either section is blank, return an error, otherwise add       the pair as a parameter. **/     if ( n == 0 || d == 0 )          return 1;     else          add_parameter ( name, data, PARAM_COPY_NAME|PARAM_COPY_VALUE );     return 0;}/* trim_string() * * trims leading and trailing whitespace from a string, overwriting the * argument with the result.  returns number of characters in result. */int trim_string ( char *string ){     int i, j, l;     j = -1;     l = strlen ( string );     for ( i = 0; i < l; ++i )     {          if ( j == -1 )          {               if ( string[i] != ' ' && string[i] != '\t' &&                    string[i] != '\n' )               {                    j = i;                    --i;               }          }          else               string[i-j] = string[i];     }     if ( j == -1 )     {          string[0] = 0;          return 0;     }          string[i-j] = 0;     l = i-j;     j = -1;     for ( i = 0; i < l; ++i )     {          if ( string[i] != ' ' && string[i] != '\t' && string[i] != '\n' )               j = i;     }     string[j+1] = 0;     return j+1;}/* define_directive() * * defines a directive "SYMBOL", which is just a parameter called * "__define:SYMBOL".  trims leading and trailing whitespace from SYMBOL. */void define_directive ( char *string ){     char *buffer;     int i;     for ( i = 0; i < strlen(string) && isspace(string[i]); ++i );          buffer = (char *)MALLOC ( (20 + strlen(string)) * sizeof ( char ) );     strcpy ( buffer, "__define:" );     strcat ( buffer, string+i );     for ( i = strlen(buffer)-1; i >= 0 && isspace(buffer[i]); --i )          buffer[i] = 0;          add_parameter ( buffer, "1", PARAM_COPY_NAME );     FREE ( buffer );}/* undefine_directive() * * undefines a directive "SYMBOL". */void undefine_directive ( char *string ){     char *buffer;     int i;     for ( i = 0; i < strlen(string) && isspace(string[i]); ++i );     buffer = (char *)MALLOC ( (20 + strlen(string)) * sizeof ( char ) );     strcpy ( buffer, "__define:" );     strcat ( buffer, string+i );     for ( i = strlen(buffer)-1; i >= 0 && isspace(buffer[i]); --i )          buffer[i] = 0;          delete_parameter ( buffer );     FREE ( buffer );}/* test_directive() * * returns 1 iff a given directive is defined. */int test_directive ( char *string ){     char *buffer;     int ret;     int i;     for ( i = 0; i < strlen(string) && isspace(string[i]); ++i );     buffer = (char *)MALLOC ( (20 + strlen(string)) * sizeof ( char ) );     strcpy ( buffer, "__define:" );     strcat ( buffer, string+i );     for ( i = strlen(buffer)-1; i >= 0 && isspace(buffer[i]); --i )          buffer[i] = 0;          if ( get_parameter ( buffer )  )          ret = 1;     else          ret = 0;     FREE ( buffer );     return ret;}/* binary_parameter() * * checks for the existence of a parameter.  if it exists, then it is changed * to the string "0" or "1" using lilgp's list of strings representing * binary values.  if the value is not on the list, or the parameter is * not found, then the parameter is set according to the value argument (it * acts as a default). */void binary_parameter ( char *name, int value ){     char *param = get_parameter ( name );     char string[2];     char *i, *is;     int v;     if ( param != NULL )     {	  /* copy the value and lowercase it. */          v = strlen ( param );          i = (char *)MALLOC ( (v+1)*sizeof ( char ) );          strcpy ( i, param );          for ( is = i; *is; ++is )               *is = tolower(*is);	  /* translate to a binary integer. */          v = translate_binary ( i );                    if ( v == -1 )          {	       /* translation failed, use the value argument. */               error ( E_ERROR,                      "\"%s\" is not a legal value for \"%s\"; assuming default.",                      i, name );               v = value;          }          FREE ( i );                    }     else	  /* parameter not found, use the value argument. */          v = value;     /** print the value to a string and put it in the parameter database. */     sprintf ( string, "%d", !!v );     add_parameter ( name, string, PARAM_COPY_VALUE|PARAM_COPY_NAME );}               

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -