impl a couple more util.cc functions

This commit is contained in:
fusion32 2025-05-25 22:59:27 -03:00
parent ad8213f355
commit c2f41059c7
3 changed files with 41 additions and 1 deletions

View File

@ -134,6 +134,8 @@ void SetErrorFunction(TErrorFunction *Function);
void SetPrintFunction(TPrintFunction *Function);
void error(const char *Text, ...) ATTR_PRINTF(1, 2);
void print(int Level, const char *Text, ...) ATTR_PRINTF(2, 3);
int random(int Min, int Max);
bool FileExists(const char *FileName);
bool isSpace(int c);
bool isAlpha(int c);
@ -147,6 +149,22 @@ int stricmp(const char *s1, const char *s2, int Max = INT_MAX);
char *findFirst(char *s, char c);
char *findLast(char *s, char c);
template<typename T>
void RandomShuffle(T *Buffer, int Size){
if(Buffer == NULL){
error("RandomShuffle: Buffer ist NULL.\n");
return;
}
int Max = Size - 1;
for(int Min = 0; Min < Max; Min += 1){
int Swap = random(Min, Max);
if(Swap != Min){
std::swap(Buffer[Min], Buffer[Swap]);
}
}
}
struct TReadStream {
// VIRTUAL FUNCTIONS
// =========================================================================

View File

@ -26,7 +26,6 @@ extern void CronExpire(Object Obj, int Delay);
extern uint32 CronInfo(Object Obj, bool Delete);
extern uint32 CronStop(Object Obj);
extern void DeleteDynamicString(uint32 Number);
extern bool FileExists(const char *FileName);
extern TCreature *GetCreature(uint32 CreatureID);
extern const char *GetDynamicString(uint32 Number);
extern TConnection *GetFirstConnection(void);

View File

@ -1,5 +1,7 @@
#include "common.hh"
#include <sys/stat.h>
static void (*ErrorFunction)(const char *Text) = NULL;
static void (*PrintFunction)(int Level, const char *Text) = NULL;
@ -41,6 +43,27 @@ void print(int Level, const char *Text, ...){
}
}
int random(int Min, int Max){
int Range = (Max - Min) + 1;
int Result = Min;
if(Range > 0){
Result += rand() % Range;
}
return Result;
}
bool FileExists(const char *FileName){
struct stat Buffer;
bool Result = true;
if(stat(FileName, &Buffer) != 0){
if(errno != ENOENT){
error("FileExists: Unerwarteter Fehlercode %d.\n", errno);
}
Result = false;
}
return Result;
}
// String Utility
// =============================================================================
bool isSpace(int c){