Hello all. I made a string library for controlling strings. In DBP people are probably used to stuff like: a$ = "Hello " + b$
Well, in C/C++ this is a bit more difficult to do. I've created a string library (IString) which helps you get past this.
I know there is already std::string, but I decided to build one myself anyways.
Here it is:
struct IString
{
char* szBuff;
// Constructor
IString()
{
szBuff = NULL;
}
// Destructor
virtual ~IString()
{
if (szBuff != NULL)
{
free((void*)szBuff);
szBuff = NULL;
}
}
// Functions
void Set(char* szStr);
char* Get(void);
void Append(char* szStr);
void Lower(void);
void Upper(void);
int Compare(char* szStr);
char* GetToken(char* szDelim, bool firstTime);
void Left(IString* pDest, int amnt);
void Right(IString* pDest, int amnt);
void Mid(IString* pDest, int location, int amnt);
void Release(void);
};
// Set the string
void IString::Set(char* szStr)
{
if (szBuff != NULL)
{
free((void*)szBuff);
}
if (szStr != NULL)
{
if (strlen(szStr) > 0)
{
szBuff = (char*)malloc(strlen(szStr));
strcpy(szBuff,szStr);
}
}
}
// Get the string
char* IString::Get(void)
{
return szBuff;
}
// Append to the string
void IString::Append(char* szStr)
{
int length = strlen(szBuff) + strlen(szStr);
if (length > 0)
{
if (szBuff != NULL)
{
char szTemp[512];
strcpy(szTemp,szBuff);
free((void*)szBuff);
szBuff = (char*)malloc(length);
memset(szBuff,0,length);
strcpy(szBuff, szTemp);
}
else
{
szBuff = (char*)malloc(length);
memset(szBuff,0,length);
}
strcat(szBuff,szStr);
}
}
// Convert the string to lowercase
void IString::Lower(void)
{
if (szBuff != NULL)
{
int length = strlen(szBuff);
if (length > 0)
{
for(int i = 0; i < length; i++)
{
szBuff[i] = (char)tolower((int)szBuff[i]);
}
}
}
}
// Convert the string to uppercase
void IString::Upper(void)
{
if (szBuff != NULL)
{
int length = strlen(szBuff);
if (length > 0)
{
for(int i = 0; i < length; i++)
{
szBuff[i] = (char)toupper((int)szBuff[i]);
}
}
}
}
// Compare the string to another
int IString::Compare(char* szStr)
{
if (szStr != NULL)
{
return strcmp(szBuff,szStr);
}
return -1;
}
// Get the next token occurance
char* IString::GetToken(char* szDelim, bool firstTime)
{
if (firstTime == true)
{
return strtok(szBuff,szDelim);
}
return strtok(NULL,szDelim);
}
// Get the left portion of a string
void IString::Left(IString* pDest, int amnt)
{
int length = strlen(szBuff);
if (amnt > 0)
{
if (length > amnt - 1)
{
pDest->Release();
pDest->szBuff = (char*)malloc(amnt + 1);
memset(pDest->szBuff, 0, amnt + 1);
memcpy(pDest->szBuff, szBuff, amnt);
}
}
}
// Get the right portion of a string
void IString::Right(IString* pDest, int amnt)
{
int length = strlen(szBuff);
if (amnt > 0)
{
if (length > amnt - 1)
{
pDest->Release();
pDest->szBuff = (char*)malloc(amnt + 1);
memset(pDest->szBuff, 0, amnt + 1);
memcpy(pDest->szBuff, (char*)(((unsigned long int)szBuff)+length-amnt), amnt);
}
}
}
// Retrieve a string at a position
void IString::Mid(IString* pDest, int location, int amnt)
{
int length = strlen(szBuff);
if (amnt > 0 && location > -1)
{
if (length > amnt + location - 1)
{
pDest->Release();
pDest->szBuff = (char*)malloc(amnt + 1);
memset(pDest->szBuff, 0, amnt + 1);
memcpy(pDest->szBuff, (char*)(((unsigned long int)szBuff) + location - amnt), amnt);
}
}
}
// Release the string
void IString::Release(void)
{
if (szBuff != NULL)
{
free((void*)szBuff);
szBuff = NULL;
}
}
I don't have DGDK, but I believe this would be workable code:
#include "DarkGDK.h"
void DarkGDK ( void )
{
// Enable syncing
dbSyncOn();
dbSyncRate(60);
// Use the string library
IString StringA, StringB, StringC, StringD;
StringA.Set("Hello ");
StringB.Set("World");
StringC.Set("!");
StringA.Append(StringB.Get());
StringA.Append(StringC.Get());
StringA.Mid(&StringD,8,3);
StringA.Lower();
char szBuff[256];
sprintf(szBuff,"%sn%sn%dn",StringA.Get(),StringD.Get(),StringA.Compare("Hello World!"));
dbPrint(szBuff);
dbSync();
}
Enjoy.
Cheers,
-naota