Exemplo n.º 1
0
config_root_path_type * config_root_path_alloc( const char * input_path ) {
  if (input_path == NULL || util_is_directory( input_path )) {
    config_root_path_type * root_path = util_malloc( sizeof * root_path );
    {
      char * cwd = util_alloc_cwd();
      
      root_path->input_path = util_alloc_string_copy( input_path );
      if (input_path == NULL) {
        root_path->rel_path = NULL;
        root_path->abs_path = util_alloc_string_copy( cwd );
      } else {
        if (util_is_abs_path( input_path )) {
          root_path->abs_path = util_alloc_string_copy( input_path );
          root_path->rel_path = util_alloc_rel_path( cwd , root_path->abs_path);
        } else {
          root_path->rel_path = util_alloc_string_copy( input_path );
          {
            char * abs_path = util_alloc_filename( cwd , input_path , NULL );
            root_path->abs_path = util_alloc_realpath( abs_path );
            free( abs_path );
          }
        }
      }
      free( cwd );
    }
    return root_path;
  } else 
    return NULL;
}
Exemplo n.º 2
0
config_path_elm_type * config_content_add_path_elm( config_content_type * content , const char * path ) {
  const config_path_elm_type * current_path_elm;

  if (vector_get_size( content->path_elm_stack ) == 0)
    current_path_elm = NULL;
  else
    current_path_elm = vector_get_last_const(content->path_elm_stack);

  {
    config_path_elm_type * new_path_elm;

    {
      char * rel_path = NULL;
      config_root_path_type * invoke_path = config_content_get_invoke_path( content );
      if (path != NULL) {
        if (current_path_elm == NULL)
          rel_path = util_alloc_rel_path( config_root_path_get_abs_path(invoke_path) , path);
        else
          rel_path = config_path_elm_alloc_relpath( current_path_elm , path );
      }
      new_path_elm = config_path_elm_alloc( invoke_path , rel_path );
      util_safe_free( rel_path );
    }
    vector_append_owned_ref( content->path_elm_storage , new_path_elm , config_path_elm_free__);
    vector_append_ref( content->path_elm_stack , new_path_elm );
    return new_path_elm;
  }
}
Exemplo n.º 3
0
void test_path(int nr , const char * root , const char * path , const char * true_path) {
  char * rel_path = util_alloc_rel_path( root  , path);

  if (!test_string_equal( rel_path , true_path))
    test_error_exit("Case:%d  rel_path(%s,%s) -> %s failed - expected: %s\n" , nr , root , path , rel_path , true_path);
  else
    printf("Case:%d OK \n",nr);
  
  util_safe_free( rel_path );
}
Exemplo n.º 4
0
char * util_alloc_parent_path( const char * path) {
  int     path_ncomp;
  char ** path_component_list;
  char *  parent_path = NULL;

  if (path) {
    bool is_abs = util_is_abs_path( path );
    char * work_path;
    
    if (strstr(path , "..")) {
      if (is_abs) 
        work_path = util_alloc_realpath__( path );
      else {
        char * abs_path = util_alloc_realpath__( path );
        char * cwd = util_alloc_cwd();
        work_path = util_alloc_rel_path( cwd , abs_path );
        free( abs_path );
        free( cwd );
      }
    } else
      work_path = util_alloc_string_copy( path );
    
    util_path_split( work_path , &path_ncomp , &path_component_list );
    if (path_ncomp > 0) {
      int current_length = 4;
      int ip;

      parent_path = util_realloc( parent_path , current_length * sizeof * parent_path);
      parent_path[0] = '\0';
  
      for (ip=0; ip < path_ncomp - 1; ip++) {
        const char * ipath = path_component_list[ip];
        int min_length = strlen(parent_path) + strlen(ipath) + 1;
    
        if (min_length >= current_length) {
          current_length = 2 * min_length;
          parent_path = util_realloc( parent_path , current_length * sizeof * parent_path);
        }

        if (is_abs || (ip > 0))
          strcat( parent_path , UTIL_PATH_SEP_STRING );
        strcat( parent_path , ipath );
      }
    }
    util_free_stringlist( path_component_list , path_ncomp );
    free( work_path );
  }
  return parent_path;
}
Exemplo n.º 5
0
int main(int argc , char ** argv) {
  char * cwd = util_alloc_cwd();

  {
    config_root_path_type * root_path = config_root_path_alloc( NULL );
    
    if (!test_check_string_equal( config_root_path_get_abs_path( root_path ) , cwd ))
      test_error_exit("abs:path:%s   expeceted:%s \n",config_root_path_get_abs_path( root_path ) , cwd );

    if (!test_check_string_equal( config_root_path_get_input_path( root_path ) , NULL ))
      test_error_exit("input:path:%s   expeceted:%s \n",config_root_path_get_input_path( root_path ) , NULL );
    
    if (!test_check_string_equal( config_root_path_get_rel_path( root_path ) , NULL ))
      test_error_exit("rel:path:%s   expeceted:%s \n",config_root_path_get_rel_path( root_path ) , NULL );

    
    config_root_path_free( root_path );
  }


  {
    config_root_path_type * root_path = config_root_path_alloc( "/does/not/exist" );
    if (root_path != NULL)
      test_error_exit("Created root_path instance for not-existing input \n");
  }
  

  
  {
    const char * input_path = argv[1];
    char * cwd      = util_alloc_cwd();
    char * rel_path = util_alloc_rel_path( cwd , input_path );

    config_root_path_type * root_path1 = config_root_path_alloc( input_path );
    config_root_path_type * root_path2 = config_root_path_alloc( rel_path );

    if (!test_check_string_equal( config_root_path_get_rel_path( root_path1 ) , config_root_path_get_rel_path( root_path2 )))
      test_error_exit("Rel: %s != %s \n",config_root_path_get_rel_path( root_path1 ) , config_root_path_get_rel_path( root_path2));
    
    if (!test_check_string_equal( config_root_path_get_abs_path( root_path1 ) , config_root_path_get_abs_path( root_path2 )))
      test_error_exit("Abs: %s != %s \n",config_root_path_get_abs_path( root_path1 ) , config_root_path_get_abs_path( root_path2 ));
    
    config_root_path_free( root_path1 );
    config_root_path_free( root_path2 );
  }


  exit(0);
}