summaryrefslogtreecommitdiff
path: root/src/common/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/utils.c')
-rw-r--r--src/common/utils.c53
1 files changed, 46 insertions, 7 deletions
diff --git a/src/common/utils.c b/src/common/utils.c
index 73df3aae1..0d76a885e 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -2,7 +2,7 @@
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
- * Copyright (C) 2012-2015 Hercules Dev Team
+ * Copyright (C) 2012-2016 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
@@ -54,6 +54,9 @@ void WriteDump(FILE* fp, const void* buffer, size_t length)
size_t i;
char hex[48+1], ascii[16+1];
+ nullpo_retv(fp);
+ nullpo_retv(buffer);
+
fprintf(fp, "--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
ascii[16] = 0;
@@ -78,10 +81,12 @@ void WriteDump(FILE* fp, const void* buffer, size_t length)
}
/// Dumps given buffer on the console.
-void ShowDump(const void *buffer, size_t length) {
+void ShowDump(const void *buffer, size_t length)
+{
size_t i;
char hex[48+1], ascii[16+1];
+ nullpo_retv(buffer);
ShowDebug("--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n");
ascii[16] = 0;
@@ -108,6 +113,7 @@ static char* checkpath(char *path, const char *srcpath)
{
// just make sure the char*path is not const
char *p = path;
+
if (NULL == path || NULL == srcpath)
return path;
while(*srcpath) {
@@ -353,6 +359,28 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B)
return (unsigned int)floor(result);
}
+/// calculates the value of A / B, in percent (rounded down)
+uint64 get_percentage64(const uint64 A, const uint64 B)
+{
+ double result;
+
+ if( B == 0 )
+ {
+ ShowError("get_percentage(): division by zero! (A=%"PRIu64",B=%"PRIu64")\n", A, B);
+ return ~0U;
+ }
+
+ result = 100 * ((double)A / (double)B);
+
+ if( result > UINT_MAX )
+ {
+ ShowError("get_percentage(): result percentage too high! (A=%"PRIu64",B=%"PRIu64",result=%g)\n", A, B, result);
+ return UINT_MAX;
+ }
+
+ return (uint64)floor(result);
+}
+
/**
* Applies a percentual rate modifier.
*
@@ -400,7 +428,9 @@ int apply_percentrate(int value, int rate, int maxrate)
//-----------------------------------------------------
const char* timestamp2string(char* str, size_t size, time_t timestamp, const char* format)
{
- size_t len = strftime(str, size, format, localtime(&timestamp));
+ size_t len;
+ nullpo_retr(NULL, str);
+ len = strftime(str, size, format, localtime(&timestamp));
memset(str + len, '\0', size - len);
return str;
}
@@ -413,6 +443,7 @@ bool HCache_check(const char *file)
char s_path[255], dT[1];
time_t rtime;
+ nullpo_retr(false, file);
if (!(first = fopen(file,"rb")))
return false;
@@ -456,10 +487,14 @@ bool HCache_check(const char *file)
return true;
}
-FILE *HCache_open(const char *file, const char *opt) {
+FILE *HCache_open(const char *file, const char *opt)
+{
FILE *first;
char s_path[255];
+ nullpo_retr(NULL, file);
+ nullpo_retr(NULL, opt);
+
if( file[0] == '.' && file[1] == '/' )
file += 2;
else if( file[0] == '.' )
@@ -498,15 +533,19 @@ void HCache_init(void)
}
/* transit to fread, shields vs warn_unused_result */
-size_t hread(void * ptr, size_t size, size_t count, FILE * stream) {
+size_t hread(void *ptr, size_t size, size_t count, FILE *stream)
+{
return fread(ptr, size, count, stream);
}
+
/* transit to fwrite, shields vs warn_unused_result */
-size_t hwrite(const void * ptr, size_t size, size_t count, FILE * stream) {
+size_t hwrite(const void *ptr, size_t size, size_t count, FILE *stream)
+{
return fwrite(ptr, size, count, stream);
}
-void HCache_defaults(void) {
+void HCache_defaults(void)
+{
HCache = &HCache_s;
HCache->init = HCache_init;