Skip to content

Commit

Permalink
use prefix hv
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Mar 27, 2022
1 parent 14008bd commit e4090c1
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 102 deletions.
2 changes: 1 addition & 1 deletion base/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static inline void atype##_cleanup(atype* p) {\
\
static inline void atype##_resize(atype* p, int maxsize) {\
if (maxsize == 0) maxsize = ARRAY_INIT_SIZE;\
p->ptr = (type*)safe_realloc(p->ptr, sizeof(type) * maxsize, sizeof(type) * p->maxsize);\
p->ptr = (type*)hv_realloc(p->ptr, sizeof(type) * maxsize, sizeof(type) * p->maxsize);\
p->maxsize = maxsize;\
}\
\
Expand Down
68 changes: 34 additions & 34 deletions base/hbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ long hv_free_cnt() {
return s_free_cnt;
}

void* safe_malloc(size_t size) {
void* hv_malloc(size_t size) {
hatomic_inc(&s_alloc_cnt);
void* ptr = malloc(size);
if (!ptr) {
Expand All @@ -31,7 +31,7 @@ void* safe_malloc(size_t size) {
return ptr;
}

void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize) {
void* hv_realloc(void* oldptr, size_t newsize, size_t oldsize) {
hatomic_inc(&s_alloc_cnt);
hatomic_inc(&s_free_cnt);
void* ptr = realloc(oldptr, newsize);
Expand All @@ -45,7 +45,7 @@ void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize) {
return ptr;
}

void* safe_calloc(size_t nmemb, size_t size) {
void* hv_calloc(size_t nmemb, size_t size) {
hatomic_inc(&s_alloc_cnt);
void* ptr = calloc(nmemb, size);
if (!ptr) {
Expand All @@ -55,7 +55,7 @@ void* safe_calloc(size_t nmemb, size_t size) {
return ptr;
}

void* safe_zalloc(size_t size) {
void* hv_zalloc(size_t size) {
hatomic_inc(&s_alloc_cnt);
void* ptr = malloc(size);
if (!ptr) {
Expand All @@ -66,15 +66,15 @@ void* safe_zalloc(size_t size) {
return ptr;
}

void safe_free(void* ptr) {
void hv_free(void* ptr) {
if (ptr) {
free(ptr);
ptr = NULL;
hatomic_inc(&s_free_cnt);
}
}

char* strupper(char* str) {
char* hv_strupper(char* str) {
char* p = str;
while (*p != '\0') {
if (*p >= 'a' && *p <= 'z') {
Expand All @@ -85,7 +85,7 @@ char* strupper(char* str) {
return str;
}

char* strlower(char* str) {
char* hv_strlower(char* str) {
char* p = str;
while (*p != '\0') {
if (*p >= 'A' && *p <= 'Z') {
Expand All @@ -96,7 +96,7 @@ char* strlower(char* str) {
return str;
}

char* strreverse(char* str) {
char* hv_strreverse(char* str) {
if (str == NULL) return NULL;
char* b = str;
char* e = str;
Expand All @@ -114,7 +114,7 @@ char* strreverse(char* str) {
}

// n = sizeof(dest_buf)
char* safe_strncpy(char* dest, const char* src, size_t n) {
char* hv_strncpy(char* dest, const char* src, size_t n) {
assert(dest != NULL && src != NULL);
char* ret = dest;
while (*src != '\0' && --n > 0) {
Expand All @@ -125,7 +125,7 @@ char* safe_strncpy(char* dest, const char* src, size_t n) {
}

// n = sizeof(dest_buf)
char* safe_strncat(char* dest, const char* src, size_t n) {
char* hv_strncat(char* dest, const char* src, size_t n) {
assert(dest != NULL && src != NULL);
char* ret = dest;
while (*dest) {++dest;--n;}
Expand All @@ -136,7 +136,7 @@ char* safe_strncat(char* dest, const char* src, size_t n) {
return ret;
}

bool strstartswith(const char* str, const char* start) {
bool hv_strstartswith(const char* str, const char* start) {
assert(str != NULL && start != NULL);
while (*str && *start && *str == *start) {
++str;
Expand All @@ -145,7 +145,7 @@ bool strstartswith(const char* str, const char* start) {
return *start == '\0';
}

bool strendswith(const char* str, const char* end) {
bool hv_strendswith(const char* str, const char* end) {
assert(str != NULL && end != NULL);
int len1 = 0;
int len2 = 0;
Expand All @@ -162,12 +162,12 @@ bool strendswith(const char* str, const char* end) {
return true;
}

bool strcontains(const char* str, const char* sub) {
bool hv_strcontains(const char* str, const char* sub) {
assert(str != NULL && sub != NULL);
return strstr(str, sub) != NULL;
}

char* strrchr_dir(const char* filepath) {
char* hv_strrchr_dir(const char* filepath) {
char* p = (char*)filepath;
while (*p) ++p;
while (--p >= filepath) {
Expand All @@ -182,12 +182,12 @@ char* strrchr_dir(const char* filepath) {
}

const char* hv_basename(const char* filepath) {
const char* pos = strrchr_dir(filepath);
const char* pos = hv_strrchr_dir(filepath);
return pos ? pos+1 : filepath;
}

const char* hv_suffixname(const char* filename) {
const char* pos = strrchr_dot(filename);
const char* pos = hv_strrchr_dot(filename);
return pos ? pos+1 : "";
}

Expand All @@ -196,7 +196,7 @@ int hv_mkdir_p(const char* dir) {
return EEXIST;
}
char tmp[MAX_PATH] = {0};
safe_strncpy(tmp, dir, sizeof(tmp));
hv_strncpy(tmp, dir, sizeof(tmp));
char* p = tmp;
char delim = '/';
while (*p) {
Expand Down Expand Up @@ -226,7 +226,7 @@ int hv_rmdir_p(const char* dir) {
return EPERM;
}
char tmp[MAX_PATH] = {0};
safe_strncpy(tmp, dir, sizeof(tmp));
hv_strncpy(tmp, dir, sizeof(tmp));
char* p = tmp;
while (*p) ++p;
while (--p >= tmp) {
Expand Down Expand Up @@ -283,20 +283,6 @@ size_t hv_filesize(const char* filepath) {
return st.st_size;
}

bool getboolean(const char* str) {
if (str == NULL) return false;
int len = strlen(str);
if (len == 0) return false;
switch (len) {
case 1: return *str == '1' || *str == 'y' || *str == 'Y';
case 2: return stricmp(str, "on") == 0;
case 3: return stricmp(str, "yes") == 0;
case 4: return stricmp(str, "true") == 0;
case 6: return stricmp(str, "enable") == 0;
default: return false;
}
}

char* get_executable_path(char* buf, int size) {
#ifdef OS_WIN
GetModuleFileName(NULL, buf, size);
Expand All @@ -313,7 +299,7 @@ char* get_executable_path(char* buf, int size) {
char* get_executable_dir(char* buf, int size) {
char filepath[MAX_PATH] = {0};
get_executable_path(filepath, sizeof(filepath));
char* pos = strrchr_dir(filepath);
char* pos = hv_strrchr_dir(filepath);
if (pos) {
*pos = '\0';
strncpy(buf, filepath, size);
Expand All @@ -324,7 +310,7 @@ char* get_executable_dir(char* buf, int size) {
char* get_executable_file(char* buf, int size) {
char filepath[MAX_PATH] = {0};
get_executable_path(filepath, sizeof(filepath));
char* pos = strrchr_dir(filepath);
char* pos = hv_strrchr_dir(filepath);
if (pos) {
strncpy(buf, pos+1, size);
}
Expand Down Expand Up @@ -361,3 +347,17 @@ void hv_random_string(char *buf, int len) {
}
buf[i] = '\0';
}

bool hv_getboolean(const char* str) {
if (str == NULL) return false;
int len = strlen(str);
if (len == 0) return false;
switch (len) {
case 1: return *str == '1' || *str == 'y' || *str == 'Y';
case 2: return stricmp(str, "on") == 0;
case 3: return stricmp(str, "yes") == 0;
case 4: return stricmp(str, "true") == 0;
case 6: return stricmp(str, "enable") == 0;
default: return false;
}
}
56 changes: 28 additions & 28 deletions base/hbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

BEGIN_EXTERN_C

//--------------------safe alloc/free---------------------------
HV_EXPORT void* safe_malloc(size_t size);
HV_EXPORT void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
HV_EXPORT void* safe_calloc(size_t nmemb, size_t size);
HV_EXPORT void* safe_zalloc(size_t size);
HV_EXPORT void safe_free(void* ptr);
//--------------------alloc/free---------------------------
HV_EXPORT void* hv_malloc(size_t size);
HV_EXPORT void* hv_realloc(void* oldptr, size_t newsize, size_t oldsize);
HV_EXPORT void* hv_calloc(size_t nmemb, size_t size);
HV_EXPORT void* hv_zalloc(size_t size);
HV_EXPORT void hv_free(void* ptr);

#define HV_ALLOC(ptr, size)\
do {\
*(void**)&(ptr) = safe_zalloc(size);\
*(void**)&(ptr) = hv_zalloc(size);\
printd("alloc(%p, size=%llu)\tat [%s:%d:%s]\n", ptr, (unsigned long long)size, __FILE__, __LINE__, __FUNCTION__);\
} while(0)

Expand All @@ -25,7 +25,7 @@ HV_EXPORT void safe_free(void* ptr);
#define HV_FREE(ptr)\
do {\
if (ptr) {\
safe_free(ptr);\
hv_free(ptr);\
printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
ptr = NULL;\
}\
Expand Down Expand Up @@ -55,33 +55,33 @@ HV_INLINE void hv_memcheck() {
}
#define HV_MEMCHECK atexit(hv_memcheck);

//--------------------safe string-------------------------------
HV_EXPORT char* strupper(char* str);
HV_EXPORT char* strlower(char* str);
HV_EXPORT char* strreverse(char* str);
//--------------------string-------------------------------
HV_EXPORT char* hv_strupper(char* str);
HV_EXPORT char* hv_strlower(char* str);
HV_EXPORT char* hv_strreverse(char* str);

HV_EXPORT bool strstartswith(const char* str, const char* start);
HV_EXPORT bool strendswith(const char* str, const char* end);
HV_EXPORT bool strcontains(const char* str, const char* sub);
HV_EXPORT bool hv_strstartswith(const char* str, const char* start);
HV_EXPORT bool hv_strendswith(const char* str, const char* end);
HV_EXPORT bool hv_strcontains(const char* str, const char* sub);

// strncpy n = sizeof(dest_buf)-1
// safe_strncpy n = sizeof(dest_buf)
HV_EXPORT char* safe_strncpy(char* dest, const char* src, size_t n);
// hv_strncpy n = sizeof(dest_buf)
HV_EXPORT char* hv_strncpy(char* dest, const char* src, size_t n);

// strncat n = sizeof(dest_buf)-1-strlen(dest)
// safe_strncpy n = sizeof(dest_buf)
HV_EXPORT char* safe_strncat(char* dest, const char* src, size_t n);
// hv_strncpy n = sizeof(dest_buf)
HV_EXPORT char* hv_strncat(char* dest, const char* src, size_t n);

#if !HAVE_STRLCPY
#define strlcpy safe_strncpy
#define strlcpy hv_strncpy
#endif

#if !HAVE_STRLCAT
#define strlcat safe_strncat
#define strlcat hv_strncat
#endif

#define strrchr_dot(str) strrchr(str, '.')
HV_EXPORT char* strrchr_dir(const char* filepath);
#define hv_strrchr_dot(str) strrchr(str, '.')
HV_EXPORT char* hv_strrchr_dir(const char* filepath);

// basename
HV_EXPORT const char* hv_basename(const char* filepath);
Expand All @@ -97,17 +97,17 @@ HV_EXPORT bool hv_isfile(const char* path);
HV_EXPORT bool hv_islink(const char* path);
HV_EXPORT size_t hv_filesize(const char* filepath);

// 1 y on yes true enable
HV_EXPORT bool getboolean(const char* str);

HV_EXPORT char* get_executable_path(char* buf, int size);
HV_EXPORT char* get_executable_dir(char* buf, int size);
HV_EXPORT char* get_executable_file(char* buf, int size);
HV_EXPORT char* get_run_dir(char* buf, int size);

// random
HV_EXPORT int hv_rand(int min, int max);
HV_EXPORT void hv_random_string(char *buf, int len);
HV_EXPORT int hv_rand(int min, int max);
HV_EXPORT void hv_random_string(char *buf, int len);

// 1 y on yes true enable
HV_EXPORT bool hv_getboolean(const char* str);

END_EXTERN_C

Expand Down
6 changes: 3 additions & 3 deletions base/hbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class HBuf : public hbuf_t {
HV_ALLOC(base, cap);
}
else {
base = (char*)safe_realloc(base, cap, len);
base = (char*)hv_realloc(base, cap, len);
}
len = cap;
cleanup_ = true;
Expand Down Expand Up @@ -128,7 +128,7 @@ class HVLBuf : public HBuf {
void push_front(void* ptr, size_t len) {
if (len > this->len - _size) {
size_t newsize = MAX(this->len, len)*2;
base = (char*)safe_realloc(base, newsize, this->len);
base = (char*)hv_realloc(base, newsize, this->len);
this->len = newsize;
}

Expand All @@ -146,7 +146,7 @@ class HVLBuf : public HBuf {
void push_back(void* ptr, size_t len) {
if (len > this->len - _size) {
size_t newsize = MAX(this->len, len)*2;
base = (char*)safe_realloc(base, newsize, this->len);
base = (char*)hv_realloc(base, newsize, this->len);
this->len = newsize;
}
else if (len > this->len - _offset - _size) {
Expand Down
2 changes: 1 addition & 1 deletion base/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static inline void qtype##_cleanup(qtype* p) {\
\
static inline void qtype##_resize(qtype* p, int maxsize) {\
if (maxsize == 0) maxsize = QUEUE_INIT_SIZE;\
p->ptr = (type*)safe_realloc(p->ptr, sizeof(type) * maxsize, sizeof(type) * p->maxsize);\
p->ptr = (type*)hv_realloc(p->ptr, sizeof(type) * maxsize, sizeof(type) * p->maxsize);\
p->maxsize = maxsize;\
}\
\
Expand Down
2 changes: 1 addition & 1 deletion cpputil/iniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void IniParser::SetValue(const std::string& key, const std::string& value, const
template<>
HV_EXPORT bool IniParser::Get(const std::string& key, const std::string& section, bool defvalue) {
std::string str = GetValue(key, section);
return str.empty() ? defvalue : getboolean(str.c_str());
return str.empty() ? defvalue : hv_getboolean(str.c_str());
}

template<>
Expand Down

0 comments on commit e4090c1

Please sign in to comment.