Skip to content

Commit

Permalink
add string util extract_filepath_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Apr 21, 2024
1 parent 58165cc commit 8242682
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/core/chuck_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 42 additions & 17 deletions src/core/util_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -952,14 +971,20 @@ void parse_path_list( std::string & str, std::list<std::string> & 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__
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/util_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 8242682

Please sign in to comment.