Skip to content

Commit

Permalink
Rework filename methods of CTString to make them more useful.
Browse files Browse the repository at this point in the history
Now filename methods recognize and process forward slashes in paths as well.
  • Loading branch information
DreamyCecil committed Aug 12, 2024
1 parent 3ba2c46 commit 1f4221e
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 149 deletions.
131 changes: 119 additions & 12 deletions Sources/Engine/Base/CTString.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#endif

#include <Engine/Base/Types.h>
#include <Engine/Base/Assert.h>

/*
* Main string class
Expand All @@ -46,7 +47,10 @@ class ENGINE_API CTString {
inline void Clear(void);

/* Conversion into character string. */
inline operator const char*() const;
inline operator const char*() const {
ASSERT(IsValid());
return str_String;
};

/* Assignment. */
inline CTString &operator=(const char *strCharString);
Expand Down Expand Up @@ -133,6 +137,54 @@ class ENGINE_API CTString {
void LoadVar(const CTString &fnmFile);
void SaveVar(const CTString &fnmFile);

// [Cecil] Split a string using a character delimiter
template<typename TypeContainer>
void CharSplit(const char chDelimiter, TypeContainer &aStrings) const {
size_t iLast = 0;
size_t iPos = Find(chDelimiter);

while (iPos != npos) {
aStrings.push_back(Substr(iLast, iPos - iLast));

// Skip delimiter
iPos += 1;

// Remember position after the delimiter
iLast = iPos;

// Get position of the next one
iPos = Find(chDelimiter, iPos);
}

// Last token
aStrings.push_back(Substr(iLast));
};

// [Cecil] Split a string using a string delimiter
template<typename TypeContainer>
void StringSplit(const CTString &strDelimiter, TypeContainer &aStrings) const {
const size_t iSize = strDelimiter.Length();

size_t iLast = 0;
size_t iPos = Find(strDelimiter);

while (iPos != npos) {
aStrings.push_back(Substr(iLast, iPos - iLast));

// Skip delimiter
iPos += iSize;

// Remember position after the delimiter
iLast = iPos;

// Get position of the next one
iPos = Find(strDelimiter, iPos);
}

// Last token
aStrings.push_back(Substr(iLast));
};

// [Cecil] Extra methods
public:

Expand Down Expand Up @@ -168,12 +220,18 @@ class ENGINE_API CTString {

// Find substring within the string
const char *GetSubstr(const char *strSub, size_t iFrom = 0) const;
inline const char *GetSubstr(const CTString &strSub, size_t iFrom = 0) const {
return GetSubstr(strSub.ConstData(), iFrom);
};

// Find character within the string
char *GetChar(char ch, size_t iFrom = 0) const;

// Find substring position in a string
size_t Find(const char *strSub, size_t iFrom = 0) const;
inline size_t Find(const CTString &strSub, size_t iFrom = 0) const {
return Find(strSub.ConstData(), iFrom);
};

// Find character position in a string
size_t Find(char ch, size_t iFrom = 0) const;
Expand All @@ -185,6 +243,9 @@ class ENGINE_API CTString {

// Find last substring position in a string
size_t RFind(const char *strSub, size_t iFrom = npos) const;
inline size_t RFind(const CTString &strSub, size_t iFrom = npos) const {
return RFind(strSub.ConstData(), iFrom);
};

// Find last character position in a string
size_t RFind(char ch, size_t iFrom = npos) const;
Expand All @@ -201,25 +262,71 @@ class ENGINE_API CTString {
// Find last absence of any of the characters
size_t FindLastNotOf(const char *str, size_t iFrom = npos) const;

// [Cecil] Migrated methods from CTFileName
// [Cecil] New path and filename methods
public:

// Get directory part of a filename
CTString FileDir(void) const;
// Get name part of a filename
// Check if there's a path separator character at some position
bool PathSeparatorAt(size_t i) const;

// Remove directory from the filename
CTString NoDir(void) const;

// Remove extension from the filename
CTString NoExt(void) const;

// Get name of the file
CTString FileName(void) const;
// Get extension part of a filename

// Get path to the file
CTString FileDir(void) const;

// Get file extension with the period
CTString FileExt(void) const;
// Get path and file name without extension
CTString NoExt(void) const;
// Set path to the absolute path, taking \.. and /.. into account

// Go up the path until a certain directory
size_t GoUpUntilDir(CTString strDirName) const;

// Normalize the path taking "backward" and "current" directories into consideration
// E.g. "abc/sub1/../sub2/./qwe" -> "abc/sub2/qwe"
void SetAbsolutePath(void);
// Remove application path from a file name
BOOL RemoveApplicationPath_t(void);

// [Cecil] Convert from a relative path to an absolute path and add missing backslashes
// Get length of the root name, if there's any
size_t RootNameLength() const;

// Check if the path starts with a root name (e.g. "C:" or "//abc")
inline bool HasRootName() const {
return RootNameLength() != 0;
};

// Check if the path starts with a root directory (e.g. "C:/" or "//abc/")
inline bool HasRootDirectory() const {
const size_t ctRoot = RootNameLength();
return Length() > ctRoot && PathSeparatorAt(ctRoot);
};

// Check if it's a full path
inline bool IsAbsolute() const {
#if !SE1_WIN
return HasRootDirectory();
#else
return HasRootName() && HasRootDirectory();
#endif
};

// Check if it's a relative path
inline bool IsRelative() const {
return !IsAbsolute();
};

// Convert from a relative path to an absolute path and add missing backslashes
void SetFullDirectory(void);

// [Cecil] Migrated methods from CTFileName
public:

// Remove application path from a file name
BOOL RemoveApplicationPath_t(void);

// Filename is its own name (used for storing in nametable)
inline const CTString &GetName(void) const {
return *this;
Expand Down
11 changes: 0 additions & 11 deletions Sources/Engine/Base/CTString.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#endif

#include <Engine/Base/Memory.h>
#include <Engine/Base/Assert.h>

/*
* Default constructor.
Expand Down Expand Up @@ -67,16 +66,6 @@ ENGINE_API void CTString::Clear(void)
operator=("");
}

/*
* Conversion into character string.
*/
ENGINE_API CTString::operator const char*() const
{
ASSERT(IsValid());

return str_String;
}

/*
* Assignment.
*/
Expand Down
Loading

0 comments on commit 1f4221e

Please sign in to comment.