Example #1
0
void test_filter_file1() {
  subst_list_type * subst_list = subst_list_alloc( NULL );
  test_work_area_type * work_area = test_work_area_alloc("subst_list/filter1");
  {
    FILE * stream = util_fopen("template" , "w");
    fprintf(stream , "<KEY1>\n<KEY2>\n<KEY3>\n<KEY4>\n");
    fclose(stream);
  }
  subst_list_append_copy( subst_list , "<KEY1>" , "Value1" , NULL);
  subst_list_append_copy( subst_list , "<KEY2>" , "Value2" , NULL);
  subst_list_append_copy( subst_list , "<KEY3>" , "Value3" , NULL);
  subst_list_append_copy( subst_list , "<KEY4>" , "Value4" , NULL);

  subst_list_filter_file( subst_list , "template" , "target");

  {
    FILE * stream = util_fopen("target" , "r");
    char s1[128],s2[128],s3[128],s4[128];

    test_assert_int_equal( 4 , fscanf( stream , "%s %s %s %s" , s1,s2,s3,s4));
    fclose(stream);

    test_assert_string_equal( s1 , "Value1");
    test_assert_string_equal( s2 , "Value2");
    test_assert_string_equal( s3 , "Value3");
    test_assert_string_equal( s4 , "Value4");
  }
  test_work_area_free( work_area );
  subst_list_free( subst_list );
}
Example #2
0
/** Will loose tagging .... */
int subst_list_add_from_string( subst_list_type * subst_list , const char * arg_string, bool append) {
  int     error_count = 0;
  if (arg_string != NULL) {
    char ** key_value_list;
    int     num_arg, iarg;
    
    util_split_string(arg_string , "," , &num_arg , &key_value_list);
    for (iarg = 0; iarg < num_arg; iarg++) {
      if (strchr(key_value_list[iarg] , '=') == NULL)
        //util_abort("%s: could not find \'=\' in argument string:%s \n",__func__ , key_value_list[iarg]);
        /*
          Could not find '=' in the argument string, this argument will
          be ignored, and the error_count will be increased by one.
        */
        error_count += 1;
      else {
        char * key , * value;
        char * tmp     = key_value_list[iarg];
        int arg_length , value_length;
        while (isspace(*tmp))  /* Skipping initial space */
          tmp++;
        
        arg_length = strcspn(tmp , " =");
        key  = util_alloc_substring_copy(tmp , 0 , arg_length);
        tmp += arg_length;
        while ((*tmp == ' ') || (*tmp == '='))
          tmp++;
        
        value_length = strcspn(tmp , " ");
        value = util_alloc_substring_copy( tmp , 0 , value_length);
        
        /* Setting the argument */
        if (append)
          subst_list_append_copy( subst_list , key , value , NULL);
        else
          subst_list_prepend_copy( subst_list , key , value , NULL);
        
        free(key);
        free(value);
        tmp += value_length;
        
        
        /* Accept only trailing space - any other character indicates a failed parsing. */
        while (*tmp != '\0') {
          if (!isspace(*tmp))
            util_abort("%s: something wrong with:%s  - spaces are not allowed in key or value part.\n",__func__ , key_value_list[iarg]);
          tmp++;
        }
      }
    }
    util_free_stringlist(key_value_list , num_arg);
  }
  return error_count;
}
Example #3
0
void test_filter_file2() {
  subst_list_type * subst_list = subst_list_alloc( NULL );
  test_work_area_type * work_area = test_work_area_alloc("subst_list/filter2");
  {
    FILE * stream = util_fopen("template" , "w");
    fprintf(stream , "MAGIC_PRINT  magic-list.txt  <ERTCASE>  __MAGIC__");
    fclose(stream);
  }

  subst_list_append_copy( subst_list , "<QC_PATH>" , "QC" , NULL);
  subst_list_append_copy( subst_list , "__MAGIC__" , "MagicAllTheWayToWorkFlow" , NULL);
  subst_list_append_copy( subst_list , "<CASE>" , "SUPERcase" , NULL);
  subst_list_append_copy( subst_list , "<ERTCASE>" , "default" , NULL);
  subst_list_filter_file( subst_list , "template" , "target");

  {
    char * target_string = util_fread_alloc_file_content( "target" , NULL );
    test_assert_string_equal( target_string , "MAGIC_PRINT  magic-list.txt  default  MagicAllTheWayToWorkFlow");
    free( target_string );
  }
  test_work_area_free( work_area );
  subst_list_free( subst_list );
}
Example #4
0
void config_content_add_define( config_content_type * content , const char * key , const char * value ) {
  subst_list_append_copy( content->define_list , key , value , NULL );
}