From 8242682fae3102a9170d41e97b2a845431288278 Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Sun, 21 Apr 2024 00:37:57 -0700 Subject: [PATCH] add string util extract_filepath_file() --- src/core/chuck_lang.cpp | 3 +- src/core/util_string.cpp | 59 ++++++++++++++++++++++++++++------------ src/core/util_string.h | 7 +++-- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/core/chuck_lang.cpp b/src/core/chuck_lang.cpp index dadf53289..8ce42741f 100644 --- a/src/core/chuck_lang.cpp +++ b/src/core/chuck_lang.cpp @@ -2473,8 +2473,9 @@ CK_DLL_MFUN( shred_sourceDir ) // added 1.3.0.0 { Chuck_VM_Shred * derhs = (Chuck_VM_Shred *)SELF; + // new chuck string Chuck_String * str = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED ); - + // set the content str->set( extract_filepath_dir(derhs->code->filename) ); RETURN->v_string = str; diff --git a/src/core/util_string.cpp b/src/core/util_string.cpp index d77272101..24ab6056d 100644 --- a/src/core/util_string.cpp +++ b/src/core/util_string.cpp @@ -840,32 +840,51 @@ std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) // name: extract_filepath_dir() // desc: return the directory portion of a file path, excluding the filename //----------------------------------------------------------------------------- -std::string extract_filepath_dir(std::string &filepath) +std::string extract_filepath_dir( const std::string & filepath ) { + // normalized internal file path separator char path_separator = '/'; - -//#ifdef __PLATFORM_WINDOWS__ -// path_separator = '\\'; -//#else -// path_separator = '/'; -//#endif - - // if the last character is a slash, skip it - t_CKINT i = filepath.rfind(path_separator); + + // normalize for searching, e.g., \ replaced with / | 1.5.2.5 (ge) added + string normalize = normalize_directory_separator( trim(filepath) ); + // look for separator from the right + t_CKINT i = normalize.rfind( path_separator ); // if not separator found, return empty string - if(i == std::string::npos) - return ""; + if( i == std::string::npos ) return ""; // skip any/all extra trailing slashes - while( i > 0 && filepath[i-1] == path_separator ) - i--; + while( i > 0 && normalize[i-1] == path_separator ) i--; // change spencer 2014-7-17: include trailing slash - return std::string(filepath, 0, i+1); + return std::string( filepath, 0, i+1 ); +} + + + + +//----------------------------------------------------------------------------- +// name: extract_filepath_file() | 1.5.2.5 (ge) added +// desc: return the file portion of a file path, excluding the directory portion +//----------------------------------------------------------------------------- +std::string extract_filepath_file( const std::string & filepath ) +{ + // normalized internal file path separator + char path_separator = '/'; + + // normalize for searching, e.g., \ replaced with / + string normalize = normalize_directory_separator( trim(filepath) ); + // look for separator from the right + long i = normalize.rfind( path_separator ); + // if not separator found, return the path unchanged + if( i == std::string::npos ) return filepath; + + // substring after the last / + return std::string( filepath, i+1, filepath.length()-i ); } + //----------------------------------------------------------------------------- // file: dir_go_up() // desc: return directory path 'numUp' levels up the chain @@ -952,14 +971,20 @@ void parse_path_list( std::string & str, std::list & lst ) std::string normalize_directory_separator( const std::string & filepath ) { #ifdef __PLATFORM_WINDOWS__ + // make a copy std::string new_filepath = filepath; - t_CKINT len = new_filepath.size(); - for(int i = 0; i < len; i++) + // string length + long len = new_filepath.size(); + // iterate over characters + for( long i = 0; i < len; i++ ) { + // replace \ with / if( new_filepath[i] == '\\' ) new_filepath[i] = '/'; } + // return potentially modified copy return new_filepath; #else + // return unchanged path return filepath; #endif // __PLATFORM_WINDOWS__ } diff --git a/src/core/util_string.h b/src/core/util_string.h index a434989e0..d00f1db7e 100644 --- a/src/core/util_string.h +++ b/src/core/util_string.h @@ -98,10 +98,13 @@ std::string get_full_path( const std::string & fp ); std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists = FALSE ); // get directory portion of a filepath (minus the file itself) -std::string extract_filepath_dir( std::string & filepath ); +std::string extract_filepath_dir( const std::string & filepath ); + +// get filename portion of a filepath (minus the directory portion) | 1.5.2.5 (ge) added +std::string extract_filepath_file( const std::string & filepath ); // convert \ to / (on Windows) -std::string normalize_directory_separator(const std::string &filepath); +std::string normalize_directory_separator( const std::string & filepath ); // check if path is absolute on the underlying platform t_CKBOOL is_absolute_path( const std::string & path );