Esempio n. 1
0
int main(int argc , char ** argv) {
#ifdef ERT_LINUX
  const char * rel_path = "rel/path";
  const char * rel_true = "rel/path/XXX";
  const char * path_true1 = "rel/path/XXX";
  
  
#endif
  test_work_area_type * work_area = test_work_area_alloc( "config_path_elm" );
  const char * root = test_work_area_get_cwd( work_area );
  char * abs_path = util_alloc_filename( root , "rel/path" , NULL);
  char * abs_true = util_alloc_filename( root , "rel/path/XXX" , NULL);
  char * path_true2 = util_alloc_filename( root , "rel/path/XXX" , NULL);

  util_chdir( test_work_area_get_original_cwd( work_area ));
  config_root_path_type * root_path = config_root_path_alloc( root );
  {
    config_path_elm_type * path_elm = config_path_elm_alloc( root_path , rel_path );
    
    test_assert_string_equal( config_path_elm_get_relpath( path_elm ) , rel_path );
    test_assert_string_equal( config_path_elm_get_abspath( path_elm ) , abs_path );

    test_assert_string_equal( config_path_elm_alloc_relpath( path_elm , "XXX" ) , rel_true);
    test_assert_string_equal( config_path_elm_alloc_abspath( path_elm , "XXX" ) , abs_true);
    test_assert_string_equal( config_path_elm_alloc_path( path_elm , "XXX" ) , path_true2 );

    
    config_path_elm_free( path_elm );
  }
  {
    config_path_elm_type * path_elm = config_path_elm_alloc( root_path , abs_path );
  
    test_assert_string_equal( config_path_elm_get_relpath( path_elm ) , rel_path );
    test_assert_string_equal( config_path_elm_get_abspath( path_elm ) , abs_path );

    test_assert_string_equal( config_path_elm_alloc_relpath( path_elm , "XXX" ) , rel_true);
    test_assert_string_equal( config_path_elm_alloc_abspath( path_elm , "XXX" ) , abs_true);
    test_assert_string_equal( config_path_elm_alloc_path( path_elm , "XXX" ) , path_true2 );
    
    config_path_elm_free( path_elm );
  }
  config_root_path_free( root_path );

  util_chdir( root );
  root_path = config_root_path_alloc( NULL );
  {
    config_path_elm_type * path_elm = config_path_elm_alloc( root_path , rel_path );
    
    test_assert_string_equal( config_path_elm_get_relpath( path_elm ) , rel_path );
    test_assert_string_equal( config_path_elm_get_abspath( path_elm ) , abs_path );

    test_assert_string_equal( config_path_elm_alloc_relpath( path_elm , "XXX" ) , rel_true);
    test_assert_string_equal( config_path_elm_alloc_abspath( path_elm , "XXX" ) , abs_true);
    test_assert_string_equal( config_path_elm_alloc_path( path_elm , "XXX" ) , path_true1 );

    
    config_path_elm_free( path_elm );
  }
  
  exit(0);
}
Esempio n. 2
0
bool config_schema_item_validate_set(const config_schema_item_type * item , stringlist_type * token_list , const char * config_file, const config_path_elm_type * path_elm , config_error_type * error_list) {
  bool OK = true;
  int argc = stringlist_get_size( token_list ) - 1;
  if (item->validate->argc_min >= 0) {
    if (argc < item->validate->argc_min) {
      OK = false;
      {
        char * error_message;
        if (config_file != NULL)
          error_message = util_alloc_sprintf("Error when parsing config_file:\"%s\" Keyword:%s must have at least %d arguments.",config_file , item->kw , item->validate->argc_min);
        else
          error_message = util_alloc_sprintf("Error:: Keyword:%s must have at least %d arguments.",item->kw , item->validate->argc_min);

        config_error_add( error_list , error_message );
      }
    }
  }

  if (item->validate->argc_max >= 0) {
    if (argc > item->validate->argc_max) {
      OK = false;
      {
        char * error_message;

        if (config_file != NULL)
          error_message = util_alloc_sprintf("Error when parsing config_file:\"%s\" Keyword:%s must have maximum %d arguments.",config_file , item->kw , item->validate->argc_max);
        else
          error_message = util_alloc_sprintf("Error:: Keyword:%s must have maximum %d arguments.",item->kw , item->validate->argc_max);

        config_error_add( error_list , error_message );
      }
    }
  }

  /*
     OK - now we have verified that the number of arguments is correct. Then
     we start actually looking at the values.
  */
  if (OK) {
    /* Validating selection set - first common, then indexed */
    if (item->validate->common_selection_set) {
      for (int iarg = 0; iarg < argc; iarg++) {
        if (!set_has_key(item->validate->common_selection_set , stringlist_iget( token_list , iarg + 1))) {
          config_error_add( error_list , util_alloc_sprintf("%s: is not a valid value for: %s.",stringlist_iget( token_list , iarg + 1) , item->kw));
          OK = false;
        }
      }
    } else if (item->validate->indexed_selection_set != NULL) {
      for (int iarg = 0; iarg < argc; iarg++) {
        if ((item->validate->argc_max > 0) || (iarg < item->validate->argc_min)) {  /* Without this test we might go out of range on the indexed selection set. */
          const set_type * selection_set = validate_iget_selection_set( item->validate , iarg);
          if (selection_set) {
            if (!set_has_key( selection_set, stringlist_iget( token_list , iarg + 1))) {
              config_error_add( error_list , util_alloc_sprintf("%s: is not a valid value for item %d of \'%s\'.",stringlist_iget( token_list , iarg + 1) , iarg + 1 , item->kw));
              OK = false;
            }
          }
        }
      }
    }

    /*
      Observe that the following code might rewrite the content of
      argv for arguments referring to path locations.
    */


    /* Validate the TYPE of the various argumnents */
    {
      for (int iarg = 0; iarg < argc; iarg++) {
        const char * value = stringlist_iget(token_list , iarg + 1);
        switch (validate_iget_type( item->validate , iarg)) {
        case(CONFIG_STRING): /* This never fails ... */
          break;
        case(CONFIG_ISODATE):
          if (!util_sscanf_isodate( value , NULL ))
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as an ISO date: YYYY-MM-DD.",value));
          break;
        case(CONFIG_INT):
          if (!util_sscanf_int( value , NULL ))
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as an integer.",value));
          break;
        case(CONFIG_FLOAT):
          if (!util_sscanf_double( value , NULL )) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as a floating point number.", value));
            OK = false;
          }
          break;
        case(CONFIG_PATH):
          // As long as we do not reuqire the path to exist it is just a string.
          break;
        case(CONFIG_EXISTING_PATH):
          {
            char * path = config_path_elm_alloc_abspath( path_elm , value );
            if (!util_entry_exists(path)) {
              config_error_add( error_list , util_alloc_sprintf("Can not find entry %s in %s ",value , config_path_elm_get_relpath( path_elm) ));
              OK = false;
            }
            free( path );
          }
          break;
        case(CONFIG_EXECUTABLE):
          {
            /*
              1. If the supplied value is an abolute path - do nothing.
              2. If the supplied is _not_ an absolute path:

                 a. Try if the relocated exists - then use that.
                 b. Else - try if the util_alloc_PATH_executable() exists.
            */
            if (!util_is_abs_path( value )) {
              char * relocated  = __alloc_relocated__(path_elm , value);
              char * path_exe   = util_alloc_PATH_executable( value );

              if (util_file_exists(relocated)) {
                if (util_is_executable(relocated))
                  stringlist_iset_copy( token_list , iarg , relocated);
              } else if (path_exe != NULL)
                stringlist_iset_copy( token_list , iarg , path_exe);
              else
                config_error_add( error_list , util_alloc_sprintf("Could not locate executable:%s ", value));

              free(relocated);
              util_safe_free(path_exe);
            } else {
              if (!util_is_executable( value ))
                config_error_add( error_list , util_alloc_sprintf("Could not locate executable:%s ", value));
            }
          }
          break;
        case(CONFIG_BOOL):
          if (!util_sscanf_bool( value , NULL )) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as a boolean.", value));
            OK = false;
          }
          break;
        case(CONFIG_BYTESIZE):
          if (!util_sscanf_bytesize( value , NULL)) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:\"%s\" as number of bytes." , value));
            OK = false;
          }
          break;
        default:
          util_abort("%s: config_item_type:%d not recognized \n",__func__ , validate_iget_type(item->validate , iarg));
        }
      }
    }
  }
  return OK;
}