From 01e25e5d8da5740610ac8da786cbd12566a83f26 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 5 Jan 2013 23:26:51 -0800 Subject: [PATCH 1/4] Add plFileName string specialization for filename manipulation --- Sources/Plasma/CoreLib/hsStream.h | 2 +- Sources/Plasma/CoreLib/plString.cpp | 68 +++++++++++++++++++++++++++++ Sources/Plasma/CoreLib/plString.h | 18 ++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/Sources/Plasma/CoreLib/hsStream.h b/Sources/Plasma/CoreLib/hsStream.h index 34fac020..09dc7c79 100644 --- a/Sources/Plasma/CoreLib/hsStream.h +++ b/Sources/Plasma/CoreLib/hsStream.h @@ -84,7 +84,7 @@ public: virtual ~hsStream() { } // Pre-filename-stringification shortcut: - bool Open_TEMP(const plString & filename, const char * mode = "rb") { return Open(filename.c_str(), mode); } + bool Open_TEMP(const plFileName & filename, const char * mode = "rb") { return Open(filename.c_str(), mode); } virtual bool Open(const char *, const char * = "rb")=0; virtual bool Open(const wchar_t *, const wchar_t * = L"rb")=0; diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index cd24209b..833cd11b 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -878,3 +878,71 @@ size_t ustrlen(const UniChar *ustr, size_t max) ; return length; } + + +/* plFileName */ +plString plFileName::GetFileName() const +{ + int end = FindLast('/'); + if (end < 0) + end = FindLast('\\'); + if (end < 0) + return *this; + + return Substr(end + 1); +} + +plString plFileName::GetFileExt() const +{ + int dot = FindLast('.'); + + // Be sure not to get a dot in the directory! + int end = FindLast('/'); + if (end < 0) + end = FindLast('\\'); + + if (dot > end) + return Substr(dot + 1); + + return plString::Null; +} + +plString plFileName::GetFileNameNoExt() const +{ + int dot = FindLast('.'); + + int end = FindLast('/'); + if (end < 0) + end = FindLast('\\'); + + // Be sure not to get a dot in the directory! + if (dot > end) + return Substr(end + 1, dot - end - 1); + return Substr(end + 1); +} + +plFileName plFileName::StripFileName() const +{ + int end = FindLast('/'); + if (end < 0) + end = FindLast('\\'); + if (end < 0) + return *this; + + return Left(end + 1); +} + +plFileName plFileName::StripFileExt() const +{ + int dot = FindLast('.'); + + // Be sure not to get a dot in the directory! + int end = FindLast('/'); + if (end < 0) + end = FindLast('\\'); + + if (dot > end) + return Left(dot); + + return *this; +} diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 4a60b427..85b20710 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -656,4 +656,22 @@ private: /** \p strlen implementation for UniChar based C-style string buffers. */ size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); + +/* plFileName - custom extension of plString for manipulating filenames */ +class plFileName : public plString +{ +public: + plFileName() { } + plFileName(const char *cstr) : plString(cstr) { } + plFileName(const plString ©) : plString(copy) { } + plFileName(const plFileName ©) : plString(copy) { } + + plString GetFileName() const; + plString GetFileExt() const; + plString GetFileNameNoExt() const; + + plFileName StripFileName() const; + plFileName StripFileExt() const; +}; + #endif //plString_Defined From c52a1f10378e9efce43d30d8b84f93af9106cdc4 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 13 Jan 2013 13:25:54 -0800 Subject: [PATCH 2/4] Add plFileName::Join static method to join paths together correctly --- Sources/Plasma/CoreLib/plString.cpp | 20 +++++++++++++++++++ Sources/Plasma/CoreLib/plString.h | 23 ++++++++++++++++++++++ Sources/Plasma/PubUtilLib/plFile/hsFiles.h | 13 ------------ 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index 833cd11b..54719892 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -946,3 +946,23 @@ plFileName plFileName::StripFileExt() const return *this; } + +plFileName plFileName::Join(const plFileName &base, const plFileName &path) +{ + if (base.IsEmpty()) + return path; + if (path.IsEmpty()) + return base; + + char last = base.CharAt(base.GetSize() - 1); + char first = path.CharAt(0); + if (last != '/' && last != '\\') { + if (first != '/' && first != '\\') + return plString::Format("%s" PATH_SEPARATOR_STR "%s", base.c_str(), path.c_str()); + return base + path; + } else if (first != '/' && first != '\\') { + return base + path; + } + // Both have a slash, but we only need one + return base + path.Substr(1); +} diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 85b20710..8d8d8d08 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -657,6 +657,18 @@ private: size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); +#if HS_BUILD_FOR_WIN32 +# define PATH_SEPARATOR '\\' +# define WPATH_SEPARATOR L'\\' +# define PATH_SEPARATOR_STR "\\" +# define WPATH_SEPARATOR_STR L"\\" +#else +# define PATH_SEPARATOR '/' +# define WPATH_SEPARATOR L'/' +# define PATH_SEPARATOR_STR "/" +# define WPATH_SEPARATOR_STR L"/" +#endif + /* plFileName - custom extension of plString for manipulating filenames */ class plFileName : public plString { @@ -672,6 +684,17 @@ public: plFileName StripFileName() const; plFileName StripFileExt() const; + + static plFileName Join(const plFileName &base, const plFileName &path); + + // VS doesn't do variadic templates, and we don't want to use const char * + static plFileName Join(const plFileName &base, const plFileName &path, + const plFileName& path2) + { return Join(Join(base, path), path2); } + + static plFileName Join(const plFileName &base, const plFileName &path, + const plFileName& path2, const plFileName &path3) + { return Join(Join(Join(base, path), path2), path3); } }; #endif //plString_Defined diff --git a/Sources/Plasma/PubUtilLib/plFile/hsFiles.h b/Sources/Plasma/PubUtilLib/plFile/hsFiles.h index d9c2ed32..2217e734 100644 --- a/Sources/Plasma/PubUtilLib/plFile/hsFiles.h +++ b/Sources/Plasma/PubUtilLib/plFile/hsFiles.h @@ -54,19 +54,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #endif -#if HS_BUILD_FOR_WIN32 -# define PATH_SEPARATOR '\\' -# define WPATH_SEPARATOR L'\\' -# define PATH_SEPARATOR_STR "\\" -# define WPATH_SEPARATOR_STR L"\\" -#elif HS_BUILD_FOR_UNIX -# define PATH_SEPARATOR '/' -# define WPATH_SEPARATOR L'/' -# define PATH_SEPARATOR_STR "/" -# define WPATH_SEPARATOR_STR L"/" -#endif - - /////////////////////////////////////////////////////////////////////// class hsFile { From 5fa72a086f9b3e2574fb1627be1f9a4e0b9ca277 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 8 Jan 2013 20:15:50 -0800 Subject: [PATCH 3/4] Clean up some miscellaneous non-optimal string uses --- Sources/Plasma/CoreLib/plString.h | 14 +++++--------- .../PubUtilLib/plAudioCore/plSoundBuffer.cpp | 2 +- .../Plasma/PubUtilLib/plAvatar/plArmatureMod.cpp | 2 +- .../Plasma/PubUtilLib/plContainer/plConfigInfo.cpp | 2 +- .../PubUtilLib/plNetClientComm/plNetClientComm.cpp | 2 +- .../Plasma/PubUtilLib/plPipeline/plDXPipeline.cpp | 2 +- .../Plasma/PubUtilLib/plPipeline/plDXPipeline.h | 2 +- Sources/Plasma/PubUtilLib/plSDL/plSDLParser.cpp | 2 +- .../Plasma/PubUtilLib/plSDL/plStateVariable.cpp | 8 ++++---- .../Plasma/PubUtilLib/plStatusLog/plStatusLog.cpp | 7 +++---- .../PubUtilLib/plVault/plVaultNodeAccess.cpp | 2 +- 11 files changed, 20 insertions(+), 25 deletions(-) diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 8d8d8d08..8aa8e0c5 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -658,15 +658,11 @@ size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); #if HS_BUILD_FOR_WIN32 -# define PATH_SEPARATOR '\\' -# define WPATH_SEPARATOR L'\\' -# define PATH_SEPARATOR_STR "\\" -# define WPATH_SEPARATOR_STR L"\\" +# define PATH_SEPARATOR '\\' +# define PATH_SEPARATOR_STR "\\" #else -# define PATH_SEPARATOR '/' -# define WPATH_SEPARATOR L'/' -# define PATH_SEPARATOR_STR "/" -# define WPATH_SEPARATOR_STR L"/" +# define PATH_SEPARATOR '/' +# define PATH_SEPARATOR_STR "/" #endif /* plFileName - custom extension of plString for manipulating filenames */ @@ -687,7 +683,7 @@ public: static plFileName Join(const plFileName &base, const plFileName &path); - // VS doesn't do variadic templates, and we don't want to use const char * + // TODO: Make this more efficient static plFileName Join(const plFileName &base, const plFileName &path, const plFileName& path2) { return Join(Join(base, path), path2); } diff --git a/Sources/Plasma/PubUtilLib/plAudioCore/plSoundBuffer.cpp b/Sources/Plasma/PubUtilLib/plAudioCore/plSoundBuffer.cpp index 6b37058d..9bc4c3b6 100644 --- a/Sources/Plasma/PubUtilLib/plAudioCore/plSoundBuffer.cpp +++ b/Sources/Plasma/PubUtilLib/plAudioCore/plSoundBuffer.cpp @@ -275,7 +275,7 @@ void plSoundBuffer::Write( hsStream *s, hsResMgr *mgr ) s->WriteSafeString( fFileName ); } else - s->WriteSafeString( nil ); + s->WriteSafeString( "" ); s->WriteLE( fHeader.fFormatTag ); s->WriteLE( fHeader.fNumChannels ); diff --git a/Sources/Plasma/PubUtilLib/plAvatar/plArmatureMod.cpp b/Sources/Plasma/PubUtilLib/plAvatar/plArmatureMod.cpp index f449d274..43472589 100644 --- a/Sources/Plasma/PubUtilLib/plAvatar/plArmatureMod.cpp +++ b/Sources/Plasma/PubUtilLib/plAvatar/plArmatureMod.cpp @@ -1735,7 +1735,7 @@ void plArmatureMod::Write(hsStream *stream, hsResMgr *mgr) stream->WriteLEFloat(fPhysHeight); stream->WriteLEFloat(fPhysWidth); - stream->WriteSafeString(fAnimationPrefix.c_str()); + stream->WriteSafeString(fAnimationPrefix); stream->WriteSafeString(fBodyAgeName.c_str()); stream->WriteSafeString(fBodyFootstepSoundPage.c_str()); } diff --git a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp index b7097eb0..bb2137bd 100644 --- a/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp +++ b/Sources/Plasma/PubUtilLib/plContainer/plConfigInfo.cpp @@ -1116,7 +1116,7 @@ bool plWWWAuthenticateConfigSource::ReadInto(plConfigInfo & configInfo, KAddValu end = i; plString buf = fAuth.Substr(begin, end-begin); - if(!ReadString(buf.c_str())) + if (!ReadString(buf)) { // TODO log warning here } diff --git a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp index 1e70ac2b..e522275a 100644 --- a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp @@ -601,7 +601,7 @@ static void INetCliAuthAgeRequestCallback ( LogMsg( kLogPerf, - L"Connecting to game server %s, ageInstId %s", + L"Connecting to game server %S, ageInstId %S", gameAddrStr.c_str(), ageInstIdStr.c_str() ); diff --git a/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.cpp b/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.cpp index a938bac1..02435f9c 100644 --- a/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.cpp +++ b/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.cpp @@ -472,7 +472,7 @@ void D3DSURF_MEMDEL(IDirect3DCubeTexture9* cTex) {} #endif // PLASMA_EXTERNAL_RELEASE #ifndef PLASMA_EXTERNAL_RELEASE -void plDXPipeline::ProfilePoolMem(D3DPOOL poolType, uint32_t size, bool add, char *id) +void plDXPipeline::ProfilePoolMem(D3DPOOL poolType, uint32_t size, bool add, const char *id) { switch( poolType ) { diff --git a/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.h b/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.h index 75aa9347..99713f8f 100644 --- a/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.h +++ b/Sources/Plasma/PubUtilLib/plPipeline/plDXPipeline.h @@ -709,7 +709,7 @@ public: static void AllocManagedVertex(uint32_t sz) { fVtxManaged += sz; } #ifndef PLASMA_EXTERNAL_RELEASE - static void ProfilePoolMem(D3DPOOL poolType, uint32_t size, bool add, char *id); + static void ProfilePoolMem(D3DPOOL poolType, uint32_t size, bool add, const char *id); #endif // PLASMA_EXTERNAL_RELEASE // From a D3DFORMAT enumeration, return the bit depth associated with it. diff --git a/Sources/Plasma/PubUtilLib/plSDL/plSDLParser.cpp b/Sources/Plasma/PubUtilLib/plSDL/plSDLParser.cpp index a3ad5f61..7b0a9a0e 100644 --- a/Sources/Plasma/PubUtilLib/plSDL/plSDLParser.cpp +++ b/Sources/Plasma/PubUtilLib/plSDL/plSDLParser.cpp @@ -310,7 +310,7 @@ bool plSDLParser::IParseVarDesc(const char* fileName, hsStream* stream, char tok } } - DebugMsg((char*)dbgStr.c_str()); + DebugMsg(dbgStr.c_str()); return skipNext; } diff --git a/Sources/Plasma/PubUtilLib/plSDL/plStateVariable.cpp b/Sources/Plasma/PubUtilLib/plSDL/plStateVariable.cpp index ddaa182e..2dffb00d 100644 --- a/Sources/Plasma/PubUtilLib/plSDL/plStateVariable.cpp +++ b/Sources/Plasma/PubUtilLib/plSDL/plStateVariable.cpp @@ -116,7 +116,7 @@ void plStateVarNotificationInfo::Write(hsStream* s, uint32_t writeOptions) const { uint8_t saveFlags=0; // unused s->WriteLE(saveFlags); - s->WriteSafeString(fHintString.c_str()); + s->WriteSafeString(fHintString); } ///////////////////////////////////////////////////// @@ -2325,7 +2325,7 @@ void plSimpleStateVariable::DumpToStream(hsStream* stream, bool dirtyOnly, int l plString logMsg = plString::Format( "%sSimpleVar, name:%s[%d]", pad.c_str(), GetName().c_str(), GetCount()); if (GetCount()>1) { - stream->WriteString(logMsg.c_str()); // it's going to be a long msg, so print it on its own line + stream->WriteString(logMsg); // it's going to be a long msg, so print it on its own line logMsg = ""; } @@ -2348,7 +2348,7 @@ void plSimpleStateVariable::DumpToStream(hsStream* stream, bool dirtyOnly, int l if ( !dirtyOnly ) logMsg += plString::Format( " dirty:%d", IsDirty() ); - stream->WriteString(logMsg.c_str()); + stream->WriteString(logMsg); logMsg = ""; } } @@ -2704,7 +2704,7 @@ void plSDStateVariable::DumpToStream(hsStream* stream, bool dirtyOnly, int level int cnt = dirtyOnly ? GetDirtyCount() : GetUsedCount(); stream->WriteString(plString::Format( "%sSDVar, name:%s dirtyOnly:%d count:%d", - pad.c_str(), GetName().c_str(), dirtyOnly, cnt).c_str()); + pad.c_str(), GetName().c_str(), dirtyOnly, cnt)); for(i=0;i= 1) - needsSeparator = (base[baseLen - 1] != WPATH_SEPARATOR); + needsSeparator = (base[baseLen - 1] != PATH_SEPARATOR); if (needsSeparator) { if ((baseLen + 1 + 1) >= maxLen) return; // abort, buffer isn't big enough - base[baseLen] = WPATH_SEPARATOR; + base[baseLen] = PATH_SEPARATOR; ++baseLen; base[baseLen] = '\0'; } @@ -533,12 +533,11 @@ void plStatusLog::IFini( void ) delete [] fColors; } - void plStatusLog::IParseFileName(wchar_t* file, size_t fnsize, wchar_t* fileNoExt, wchar_t** ext) const { const wchar_t *base = plStatusLogMgr::IGetBasePath(); if( wcslen( base ) != nil ) - hsSnwprintf( file, fnsize, L"%s%s%s", base, WPATH_SEPARATOR_STR, fFilename.c_str() ); + hsSnwprintf( file, fnsize, L"%s%S%s", base, PATH_SEPARATOR_STR, fFilename.c_str() ); else wcscpy( file, fFilename.c_str() ); diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp index 2fccccea..f9529765 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultNodeAccess.cpp @@ -455,7 +455,7 @@ void VaultAgeLinkNode::AddSpawnPoint (const plSpawnPointInfo & point) { plSpawnPointVec points; GetSpawnPoints( &points ); - if ( std::find_if( points.begin(), points.end(), MatchesSpawnPointTitle( point.fTitle.c_str() ) )!=points.end() ) + if ( std::find_if( points.begin(), points.end(), MatchesSpawnPointTitle( point.fTitle ) )!=points.end() ) return; // only check to see if the titles are the same... From 2b1e05fcc7ebf0264799bff8fb1b11ff8a5bf2af Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sun, 13 Jan 2013 16:13:33 -0800 Subject: [PATCH 4/4] Add doxygen docs to plFileName --- Doxyfile | 2 +- Sources/Plasma/CoreLib/plString.cpp | 2 +- Sources/Plasma/CoreLib/plString.h | 45 +++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Doxyfile b/Doxyfile index d836a3e3..c1aa43e8 100644 --- a/Doxyfile +++ b/Doxyfile @@ -353,7 +353,7 @@ TYPEDEF_HIDES_STRUCT = NO # 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, # corresponding to a cache size of 2^16 = 65536 symbols. -SYMBOL_CACHE_SIZE = 0 +SYMBOL_CACHE_SIZE = 4 # Similar to the SYMBOL_CACHE_SIZE the size of the symbol lookup cache can be # set using LOOKUP_CACHE_SIZE. This cache is used to resolve symbols given diff --git a/Sources/Plasma/CoreLib/plString.cpp b/Sources/Plasma/CoreLib/plString.cpp index 54719892..fd74326e 100644 --- a/Sources/Plasma/CoreLib/plString.cpp +++ b/Sources/Plasma/CoreLib/plString.cpp @@ -929,7 +929,7 @@ plFileName plFileName::StripFileName() const if (end < 0) return *this; - return Left(end + 1); + return Left(end); } plFileName plFileName::StripFileExt() const diff --git a/Sources/Plasma/CoreLib/plString.h b/Sources/Plasma/CoreLib/plString.h index 8aa8e0c5..c1adbc45 100644 --- a/Sources/Plasma/CoreLib/plString.h +++ b/Sources/Plasma/CoreLib/plString.h @@ -665,29 +665,70 @@ size_t ustrlen(const UniChar *ustr, size_t max = plString::kSizeAuto); # define PATH_SEPARATOR_STR "/" #endif -/* plFileName - custom extension of plString for manipulating filenames */ +/** Subclass of plString with specific methods to help deal with common + * filename manipulation tasks. + */ class plFileName : public plString { public: + /** Construct an empty filename. */ plFileName() { } + + /** Construct a filename from the UTF-8 character data in \a cstr. */ plFileName(const char *cstr) : plString(cstr) { } + + /** Construct a filename from the plString argument \a copy. */ plFileName(const plString ©) : plString(copy) { } + + /** Copy constructor. */ plFileName(const plFileName ©) : plString(copy) { } + /** Return the name portion of the path (including extension). + * For example: + *
plFileName("C:\\Path\\Filename.ext") => "Filename.ext"
+ */ plString GetFileName() const; + + /** Return the file extension from the filename. + * For example: + *
plFileName("C:\\Path\\Filename.ext") => "ext"
+ */ plString GetFileExt() const; + + /** Return the name portion of the path, excluding its extension. + * For example: + *
plFileName("C:\\Path\\Filename.ext") => "Filename"
+ */ plString GetFileNameNoExt() const; + /** Return the path with the filename portion stripped off. + * For example: + *
plFileName("C:\\Path\\Filename.ext") => "C:\\Path"
+ */ plFileName StripFileName() const; + + /** Return the filename with the extension stripped off. + * For example: + *
plFileName("C:\\Path\\Filename.ext") => "C:\\Path\\Filename"
+ */ plFileName StripFileExt() const; + /** Join two path components together with the correct path separator. + * For example: + *
plFileName::Join("C:\\Path", "Filename.ext") => "C:\\Path\\Filename.ext"
+ */ static plFileName Join(const plFileName &base, const plFileName &path); - // TODO: Make this more efficient + /** Join three path components together with the correct path separator. + * \todo Make this more efficient. + */ static plFileName Join(const plFileName &base, const plFileName &path, const plFileName& path2) { return Join(Join(base, path), path2); } + /** Join four path components together with the correct path separator. + * \todo Make this more efficient. + */ static plFileName Join(const plFileName &base, const plFileName &path, const plFileName& path2, const plFileName &path3) { return Join(Join(Join(base, path), path2), path3); }