cwFileSys.h/cpp : Added replaceDirectory/Filename/Extension() functions.

This commit is contained in:
kevin 2021-05-10 08:30:22 -04:00
parent eed27b2e49
commit ee11b004da
2 changed files with 69 additions and 1 deletions

View File

@ -241,6 +241,68 @@ char* cw::filesys::makeVersionedFn( const char* dir, const char* fn_prefix, cons
return fnOut; return fnOut;
} }
char* cw::filesys::replaceDirectory( const char* fn0, const char* dir )
{
pathPart_t* pp = nullptr;
char* fn = nullptr;
if((pp = pathParts( fn0 )) == nullptr )
{
cwLogError(kOpFailRC,"File name parse failed.");
return nullptr;
}
if((fn = makeFn( dir, pp->fnStr, pp->extStr)) == nullptr )
{
cwLogError(kOpFailRC,"Unable to replace directory.");
}
mem::release(pp);
return fn;
}
char* cw::filesys::replaceFilename( const char* fn0, const char* name )
{
pathPart_t* pp = nullptr;
char* fn = nullptr;
if((pp = pathParts( fn0 )) == nullptr )
{
cwLogError(kOpFailRC,"File name parse failed.");
return nullptr;
}
if((fn = makeFn( pp->dirStr, name, pp->extStr)) == nullptr )
{
cwLogError(kOpFailRC,"Unable to replace file name.");
}
mem::release(pp);
return fn;
}
char* cw::filesys::replaceExtension( const char* fn0, const char* ext )
{
pathPart_t* pp = nullptr;
char* fn = nullptr;
if((pp = pathParts( fn0 )) == nullptr )
{
cwLogError(kOpFailRC,"File name parse failed.");
return nullptr;
}
if((fn = makeFn( pp->dirStr, pp->fnStr, ext)) == nullptr )
{
cwLogError(kOpFailRC,"Unable to replace file extension.");
}
mem::release(pp);
return fn;
}
char* cw::filesys::expandPath( const char* dir ) char* cw::filesys::expandPath( const char* dir )
{ {

View File

@ -27,6 +27,12 @@ namespace cw
char* vMakeVersionedFn(const char* dir, const char* fn_prefix, const char* ext, va_list vl ); char* vMakeVersionedFn(const char* dir, const char* fn_prefix, const char* ext, va_list vl );
char* makeVersionedFn( const char* dir, const char* fn_prefix, const char* ext, ... ); char* makeVersionedFn( const char* dir, const char* fn_prefix, const char* ext, ... );
// Replace the directory/name/extension part of a complete path.
// The returned string must be release by a call to mem::release() or mem::free().
char* replaceDirectory( const char* fn, const char* dir );
char* replaceFilename( const char* fn, const char* name );
char* replaceExtension( const char* fn, const char* ext );
// The returned string must be released by a call to mem::release() or mem::free(). // The returned string must be released by a call to mem::release() or mem::free().
char* expandPath( const char* dir ); char* expandPath( const char* dir );
@ -44,7 +50,7 @@ namespace cw
// Given a file name decompose it into a directory string, file name string and file extension string. // Given a file name decompose it into a directory string, file name string and file extension string.
// The returned record and the strings it points to are contained in a single block of // The returned record and the strings it points to are contained in a single block of
// memory which must be released by a call to memRelease() or memFree() // memory which must be released by a call to mem::release() or mem::free()
pathPart_t* pathParts( const char* pathNameStr ); pathPart_t* pathParts( const char* pathNameStr );
// Flags used by dirEntries 'includeFlags' parameter. // Flags used by dirEntries 'includeFlags' parameter.