sized_string (can we fix libc headers yet?)

This commit is contained in:
robojumper
2024-05-29 20:02:57 +02:00
parent be163e6de7
commit e6429777e5
6 changed files with 88 additions and 93 deletions
+6 -3
View File
@@ -3,6 +3,9 @@
#include <common.h>
#include <m/m_dvd.h>
// clang-format off
#include <sized_string.h>
// clang-format on
// TODO: loading status could be an enum (-2/-1/0/+1)
@@ -28,8 +31,8 @@ public:
return mRefCount != 0;
}
inline const char *name() {
return mArcName;
inline const char *name() const {
return &mArcName;
}
inline bool isNotLoaded() {
@@ -62,7 +65,7 @@ private:
static void searchCallback2(void *, void *, const ARCDirEntry *, const char *);
private:
/* 0x00 */ char mArcName[0x20];
/* 0x00 */ SizedString<32> mArcName;
/* 0x20 */ u16 mRefCount;
/* 0x24 */ mDvd_mountMemArchive_c *mpDvdReq;
/* 0x28 */ EGG::Archive *mpArc;
-31
View File
@@ -1,31 +0,0 @@
#ifndef INLINE_STRING_H
#define INLINE_STRING_H
#include <MSL_C/string.h>
inline void inline_strncat(char *dest, const char *src, size_t destSize) {
if (src != nullptr) {
size_t destLen = strlen(dest);
size_t copyLen = strlen(src);
// Make sure copy length isnt more than destination length
if (destLen + copyLen + 1 >= destSize) {
copyLen = destSize - destLen - 1;
}
strncpy(dest + destLen, src, copyLen);
// make sure string is null terminated
size_t offset = destLen + copyLen;
dest[offset] = '\0';
}
}
inline void inline_strncpy(char *dest, const char *src, size_t destSize) {
if (src != dest) {
dest[0] = '\0';
inline_strncat(dest, src, destSize);
}
}
#endif
+69
View File
@@ -0,0 +1,69 @@
#ifndef SIZED_STRING_H
#define SIZED_STRING_H
#include <MSL_C/string.h>
/**
* A statically sized string buffer used for resource
* identification where strings are guaranteed to be short.
*
* Note: We aren't aware of any other projects that use a similar
* class and given that SS has no debugging info anywhere it's hard
* to be certain about anything.
*/
template <size_t Size>
struct SizedString {
SizedString() {
mChars[0] = '\0';
}
char mChars[Size];
char *operator&() {
return mChars;
}
const char *operator&() const {
return mChars;
}
void operator=(const char *src) {
if (src != mChars) {
mChars[0] = '\0';
operator+=(src);
}
}
void operator+=(const char *src) {
if (src != nullptr) {
size_t destLen = strlen(mChars);
size_t copyLen = strlen(src);
// Make sure copy length isnt more than destination length
if (destLen + copyLen + 1 >= Size) {
size_t tmpLen = Size - destLen;
copyLen = tmpLen - 1;
}
strncpy(mChars + destLen, src, copyLen);
// make sure string is null terminated
size_t offset = destLen + copyLen;
mChars[offset] = '\0';
}
}
int sprintf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int printed = vsnprintf(this->mChars, Size, fmt, args);
if (printed != strlen(this->mChars)) {
this->mChars[0] = '\0';
}
va_end(list);
return printed;
}
};
#endif