コード例 #1
0
ファイル: ert_util_work_area.cpp プロジェクト: OPM/ResInsight
void test_update_store() {
  {
    test_work_area_type * work_area = test_work_area_alloc( "update-store1" );
    char * work_cwd = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
    test_work_area_set_store( work_area , true );
    test_work_area_free( work_area );
    test_assert_true( util_entry_exists( work_cwd ));
  }

  {
    test_work_area_type * work_area = test_work_area_alloc( "update-store2" );
    char * work_cwd = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
    test_work_area_free( work_area );
    test_assert_false( util_entry_exists( work_cwd ));
  }

  {
    test_work_area_type * work_area = test_work_area_alloc( "update-store3" );
    char * work_cwd = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
    test_work_area_set_store( work_area , false );
    test_work_area_free( work_area );
    test_assert_false( util_entry_exists( work_cwd ));
  }

  {
    test_work_area_type * work_area = test_work_area_alloc( "update-store4" );
    char * work_cwd = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
    test_work_area_set_store( work_area , true);
    test_work_area_free( work_area );
    test_assert_true( util_entry_exists( work_cwd ));
  }
}
コード例 #2
0
ファイル: ert_util_work_area.cpp プロジェクト: OPM/ResInsight
void test_with_prefix() {
  test_work_area_type * work_area = test_work_area_alloc( "with-prefix" );

  util_make_path( "PREFIX" );
  {
    test_work_area_type * sub_area = test_work_area_alloc_relative("PREFIX" , "sub-work" );
    test_assert_true( test_work_area_is_instance( sub_area ));
    test_work_area_free( sub_area );
    test_assert_true( util_entry_exists("PREFIX/sub-work"));
  }
  {
    test_work_area_type * sub_area = test_work_area_alloc_relative("DoesNotExist" , "sub-work" );
    test_assert_NULL( sub_area );
  }
  test_work_area_free( work_area );
}
コード例 #3
0
ファイル: ert_util_work_area.c プロジェクト: JacobStoren/ert
void create_test_area(const char * test_name , bool store) {
  char * pre_cwd = util_alloc_cwd();
  test_work_area_type * work_area = test_work_area_alloc( test_name , store);
  char * work_path = util_alloc_string_copy( test_work_area_get_cwd( work_area ));
  
  test_assert_true( util_is_directory( work_path ));
  test_work_area_free( work_area );
  test_assert_bool_equal( store , util_entry_exists( work_path ));
  
  {
    char * post_cwd = util_alloc_cwd();
    test_assert_string_equal( pre_cwd , post_cwd );
    free( post_cwd );
  }
  free( pre_cwd );
  free( work_path );
}
コード例 #4
0
ファイル: ert_util_work_area.cpp プロジェクト: OPM/ResInsight
void test_copy_parent_directory( const char * path ) {
  test_work_area_type * work_area = test_work_area_alloc( "copy-parent-directory" );
  char * parent_path;

  {
    char * full_path = util_alloc_abs_path( path );
    util_alloc_file_components( path , &parent_path , NULL , NULL);
    free( full_path );
  }

  test_assert_false( test_work_area_copy_parent_directory( work_area , "Does/not/exist") );
  test_assert_true( test_work_area_copy_parent_directory( work_area , path ) );

  test_assert_true( util_entry_exists( parent_path ));
  test_assert_true( util_is_directory( parent_path ));

  test_work_area_free( work_area );
  free( parent_path );
}
コード例 #5
0
ファイル: EclipseIO.cpp プロジェクト: OPM/opm-output
EclipseIO::EclipseIO( const EclipseState& es,
                      EclipseGrid grid,
                      const Schedule& schedule,
                      const SummaryConfig& summary_config)
    : impl( new Impl( es, std::move( grid ), schedule , summary_config) )
{
    if( !this->impl->output_enabled )
        return;
    {
        const auto& outputDir = this->impl->outputDir;

        // make sure that the output directory exists, if not try to create it
        if ( !util_entry_exists( outputDir.c_str() ) ) {
            util_make_path( outputDir.c_str() );
        }

        if( !util_is_directory( outputDir.c_str() ) ) {
            throw std::runtime_error( "The path specified as output directory '"
                                      + outputDir + "' is not a directory");
        }
    }
}
コード例 #6
0
ファイル: config_schema_item.c プロジェクト: agchitu/ert
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;
}
コード例 #7
0
ファイル: enkf_tui_export.cpp プロジェクト: berland/ert
void enkf_tui_export_scalar2csv(void * arg) {
  enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
  const ensemble_config_type * ensemble_config = enkf_main_get_ensemble_config(enkf_main);
  const enkf_config_node_type * config_node;
  char * user_key, *key_index;

  util_printf_prompt("Scalar to export (KEY:INDEX)" , PROMPT_LEN , '=' , "=> "); user_key = util_alloc_stdin_line();
  config_node = ensemble_config_user_get_node( ensemble_config , user_key , &key_index);
  if (config_node != NULL) {
    int    report_step , first_report, last_report;
    int    iens1 , iens2, iens;
    char * csv_file;

    iens2        = enkf_main_get_ensemble_size( enkf_main ) - 1;
    iens1        = 0;
    first_report = 0;
    last_report  = enkf_main_get_history_length( enkf_main );
    {
      char * path;
      char * prompt = util_alloc_sprintf("File to store \'%s\'", user_key);
      util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> ");
      csv_file = util_alloc_stdin_line();

      util_alloc_file_components( csv_file , &path , NULL , NULL);
      if (path != NULL) {
        if (util_entry_exists( path )) {
          if (!util_is_directory( path )) {
            /* The path component already exists in the filesystem - and it is not a directory - we leave the building. */
            fprintf(stderr,"Sorry: %s already exists - and is not a directory.\n",path);
            free(path);
            free(csv_file);
            free(user_key);
            return ;
          }
        } else {
          /* The path does not exist - we make it. */
          enkf_tui_util_msg("Creating new directory: %s\n" , path);
          util_make_path( path );
        }
      }
      free(prompt);
    }
    {
      /* Seriously manual creation of csv file. */
      enkf_fs_type * fs     = enkf_main_tui_get_fs(enkf_main);
      enkf_node_type * node = enkf_node_alloc( config_node );
      FILE * stream         = util_fopen( csv_file , "w");
      node_id_type node_id;


      /* Header line */
      fprintf(stream , "\"Report step\"");
      for (iens = iens1; iens <= iens2; iens++)
        fprintf(stream , "%s\"%s(%d)\"" , CSV_SEP , user_key , iens);
      fprintf(stream , CSV_NEWLINE);

      for (report_step = first_report; report_step <= last_report; report_step++) {
        fprintf(stream , "%6d" , report_step);
        node_id.report_step = report_step;
        for (iens = iens1; iens <= iens2; iens++) {
          double value;
          char label[32];
          /*
             Have not implemented a choice on forecast/analyzed. Tries
             analyzed first, then forecast.
          */
          node_id.iens = iens;
          sprintf(label , "%03d/%03d" , report_step , iens);

          if (enkf_node_user_get( node , fs , key_index , node_id ,  &value))
            fprintf(stream , "%s%g" , CSV_SEP , value);
          else
            fprintf(stream , "%s%s" , CSV_SEP , CSV_MISSING_VALUE);

        }
        fprintf(stream , CSV_NEWLINE);
      }

      enkf_node_free( node );
      fclose(stream);
    }
  } else
    fprintf(stderr,"Sorry - could not find any nodes with key:%s\n",user_key);

  free(user_key);
}