Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove last uses of hsStrcpy and hsStrncpy #1657

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/MaxPlugin/MaxComponent/plAudioComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ bool plBaseSoundEmitterComponent::LookupLatestAsset( const char *waveName, ch
}

// Copy the string over and go
hsStrcpy( retPath, assetPath );
strcpy(retPath, assetPath);
return true;
}

Expand Down
31 changes: 9 additions & 22 deletions Sources/MaxPlugin/MaxMain/plPythonMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,20 @@ bool ICallIntFunc(PyObject *dict, const char *funcName, int& val)
return false;
}

bool ICallStrFunc(PyObject *dict, const char *funcName, char*& val)
bool ICallStrFunc(PyObject *dict, const char *funcName, ST::string& val)
{
PyObject *obj;
if (ICallVoidFunc(dict, funcName, obj))
{
if (PyUnicode_Check(obj))
{
val = hsStrcpy(PyUnicode_AsUTF8(obj));
Py_DECREF(obj);
return true;
}
}

return false;
}

bool ICallStrFunc(PyObject* dict, const char* funcName, wchar_t*& val)
{
PyObject* obj;
if (ICallVoidFunc(dict, funcName, obj)) {
if (PyUnicode_Check(obj)) {
Py_ssize_t size;
wchar_t* pyUtf16Str = PyUnicode_AsWideCharString(obj, &size);
val = new wchar_t[size + 1];
wcsncpy(val, pyUtf16Str, size + 1);
PyMem_Free(pyUtf16Str);
const char* utf8 = PyUnicode_AsUTF8AndSize(obj, &size);
if (utf8 == nullptr) {
Py_DECREF(obj);
return false;
}
val = ST::string::from_utf8(utf8, size);
Py_DECREF(obj);
return true;
}
Expand Down Expand Up @@ -290,7 +278,7 @@ bool plPythonMgr::IQueryPythonFile(const ST::string& fileName)
}

// Get the class name
MCHAR* className = nullptr;
ST::string className;
if (!ICallStrFunc(dict, kGetClassName, className))
{
Py_DECREF(module);
Expand Down Expand Up @@ -326,7 +314,7 @@ bool plPythonMgr::IQueryPythonFile(const ST::string& fileName)

if (PyCallable_Check(getParamFunc))
{
plAutoUIBlock *autoUI = new plAutoUIBlock(PythonFile::GetClassDesc(), blockID, className, version);
plAutoUIBlock *autoUI = new plAutoUIBlock(PythonFile::GetClassDesc(), blockID, std::move(className), version);
// test to see if it is a multi-modifier type class
if (isMulti)
autoUI->SetMultiModifierFlag(true);
Expand Down Expand Up @@ -466,7 +454,6 @@ bool plPythonMgr::IQueryPythonFile(const ST::string& fileName)
PythonFile::AddAutoUIBlock(autoUI);
}

delete [] className;
Py_DECREF(module);
}
else
Expand Down
23 changes: 0 additions & 23 deletions Sources/Plasma/CoreLib/HeadSpin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,3 @@ void hsStatusMessageF(const char * fmt, ...)
}

#endif

/**************************************/
char* hsStrcpy(char* dst, const char* src)
{
if (src)
{
if (dst == nullptr)
{
size_t count = strlen(src);
dst = new char[count + 1];
memcpy(dst, src, count);
dst[count] = 0;
return dst;
}

int32_t i;
for (i = 0; src[i] != 0; i++)
dst[i] = src[i];
dst[i] = 0;
}

return dst;
}
14 changes: 0 additions & 14 deletions Sources/Plasma/CoreLib/HeadSpin.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,6 @@ template <> inline double hsToLE(double value) { return hsToLEDouble(value); }
void hsStatusMessageF(const char * fmt, ...);
#endif // PLASMA_EXTERNAL_RELEASE

char* hsStrcpy(char* dstOrNil, const char* src);

inline char* hsStrcpy(const char* src)
{
return hsStrcpy(nullptr, src);
}

inline char *hsStrncpy(char *strDest, const char *strSource, size_t count)
{
char *temp = strncpy(strDest, strSource, count-1);
strDest[count-1] = 0;
return temp;
}

// Use "correct" non-standard string functions based on the
// selected compiler / library
#if HS_BUILD_FOR_WIN32
Expand Down
28 changes: 7 additions & 21 deletions Sources/Plasma/PubUtilLib/plAnimation/plAGAnimInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
/////////////////////////////////////////////////////////////////////////////////////////

#include <algorithm>
#include <string_theory/format>

// singular
#include "plAGAnimInstance.h"
Expand Down Expand Up @@ -621,35 +622,20 @@ void plAGAnimInstance::ISetupFade(float goal, float rate, bool detach, uint8_t t
}
}

class agAlloc
struct agAlloc
{
public:
agAlloc(plAGChannel *object, const char *chanName, const char *animName, uint16_t classIndex)
: fObject(object),
fClassIndex(classIndex)
{
fChannelName = hsStrcpy(chanName);
fAnimName = hsStrcpy(animName);
}

~agAlloc()
{
delete[] fChannelName;
delete[] fAnimName;
}

plAGChannel *fObject;
char *fChannelName;
char *fAnimName;
ST::string fChannelName;
ST::string fAnimName;
uint16_t fClassIndex;
};

typedef std::map<plAGChannel *, agAlloc *> agAllocMap;
static agAllocMap gAGAllocs;

void RegisterAGAlloc(plAGChannel *object, const char *chanName, const char *animName, uint16_t classIndex)
void RegisterAGAlloc(plAGChannel *object, ST::string chanName, ST::string animName, uint16_t classIndex)
{
gAGAllocs[object] = new agAlloc(object, chanName, animName, classIndex);
gAGAllocs[object] = new agAlloc {object, std::move(chanName), std::move(animName), classIndex};
}

void DumpAGAllocs()
Expand All @@ -665,7 +651,7 @@ void DumpAGAllocs()

uint16_t realClassIndex = al->fObject->ClassIndex();

hsStatusMessageF("agAlloc: an: %s ch: %s, cl: %s", al->fAnimName, al->fChannelName, plFactory::GetNameOfClass(realClassIndex));
hsStatusMessage(ST::format("agAlloc: an: {} ch: {}, cl: {}", al->fAnimName, al->fChannelName, plFactory::GetNameOfClass(realClassIndex)).c_str());

}
// it's not fast but it's safe and simple..
Expand Down
2 changes: 1 addition & 1 deletion Sources/Plasma/PubUtilLib/plAnimation/plAGAnimInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class plAGAnimInstance {
extern ST::string gGlobalAnimName;
extern ST::string gGlobalChannelName;

void RegisterAGAlloc(plAGChannel *object, const char *chanName, const char *animName, uint16_t classIndex);
void RegisterAGAlloc(plAGChannel *object, ST::string chanName, ST::string animName, uint16_t classIndex);
void UnRegisterAGAlloc(plAGChannel *object);
void DumpAGAllocs();

Expand Down
3 changes: 1 addition & 2 deletions Sources/Plasma/PubUtilLib/plAnimation/plAGChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ plAGChannel::plAGChannel()
{
#ifdef TRACK_AG_ALLOCS
fName = gGlobalAnimName;
RegisterAGAlloc(this, gGlobalChannelName.c_str(), gGlobalAnimName.c_str(), this->ClassIndex());
RegisterAGAlloc(this, gGlobalChannelName, gGlobalAnimName, this->ClassIndex());
#endif // TRACK_AG_ALLOCS
}

// DTOR
plAGChannel::~plAGChannel()
{
// we do not own the "fName" string, so don't delete it!
#ifdef TRACK_AG_ALLOCS
UnRegisterAGAlloc(this);
#endif // TRACK_AG_ALLOCS
Expand Down
9 changes: 8 additions & 1 deletion Sources/Plasma/PubUtilLib/plNetMessage/plNetMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,14 @@ class plNetMsgGetSharedState : public plNetMsgObject
CLASSNAME_REGISTER( plNetMsgGetSharedState );
GETINTERFACE_ANY( plNetMsgGetSharedState, plNetMsgObject );

void SetSharedStateName(char* n) { if (n) hsStrncpy(fSharedStateName, n, kMaxNameLen); }
void SetSharedStateName(char* n)
{
if (n) {
strncpy(fSharedStateName, n, kMaxNameLen - 1);
fSharedStateName[kMaxNameLen - 1] = 0;
}
}

char* GetSharedStateName() { return fSharedStateName; }
};

Expand Down
7 changes: 5 additions & 2 deletions Sources/Plasma/PubUtilLib/plSDL/plStateVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ bool plSimpleStateVariable::SetFromString(const ST::string& value, int idx, bool
int i=idx*fVar.GetAtomicCount();
for (std::vector<ST::string>::iterator ptr = bits.begin(); ptr != bits.end(); ++ptr)
{
hsStrncpy(fS32[i++], ptr->c_str(), 32);
plVarDescriptor::String32& dest = fS32[i];
strncpy(dest, ptr->c_str(), std::size(dest) - 1);
dest[std::size(dest) - 1] = 0;
i++;
}
}
break;
Expand Down Expand Up @@ -1730,7 +1733,7 @@ bool plSimpleStateVariable::Get(char value[], int idx) const

if (fVar.GetType()==plVarDescriptor::kString32)
{
hsStrcpy(value, fS32[idx]);
strcpy(value, fS32[idx]);
return true;
}
hsAssert(false, "passing wrong value type to SDL variable");
Expand Down
Loading