forked from metin2/server
153 lines
5.5 KiB
C
153 lines
5.5 KiB
C
|
#ifndef __INC_LIBTHECORE_UTILS_H__
|
|||
|
#define __INC_LIBTHECORE_UTILS_H__
|
|||
|
|
|||
|
#ifdef __cplusplus
|
|||
|
extern "C"
|
|||
|
{
|
|||
|
#endif /* __cplusplus */
|
|||
|
|
|||
|
#define SAFE_FREE(p) { if (p) { free( (void *) p); (p) = NULL; } }
|
|||
|
#define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }
|
|||
|
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p) = NULL; } }
|
|||
|
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = NULL; } }
|
|||
|
|
|||
|
#define LOWER(c) (((c)>='A' && (c) <= 'Z') ? ((c)+('a'-'A')) : (c))
|
|||
|
#define UPPER(c) (((c)>='a' && (c) <= 'z') ? ((c)+('A'-'a')) : (c))
|
|||
|
|
|||
|
#define str_cmp strcasecmp
|
|||
|
#define STRNCPY(dst, src, len) do {strncpy(dst, src, len); dst[len] = '\0'; } while(0)
|
|||
|
|
|||
|
extern char * str_dup(const char * source); // <20><EFBFBD><DEB8><EFBFBD> <20>Ҵ<EFBFBD> <20>ؼ<EFBFBD> source <20><><EFBFBD><EFBFBD> <20>Ѱ<EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern void printdata(const unsigned char * data, int bytes); // data<74><61> hex<65><78> ascii<69><69> <20><><EFBFBD><EFBFBD> (<28><>Ŷ <20>м<EFBFBD> <20> <20><><EFBFBD><EFBFBD>)
|
|||
|
extern int filesize(FILE * fp); // <20><><EFBFBD><EFBFBD> ũ<><C5A9> <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
#define core_dump() core_dump_unix(__FILE__, __LINE__)
|
|||
|
extern void core_dump_unix(const char *who, WORD line); // <20>ھ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
#define TOKEN(string) if (!str_cmp(token_string, string))
|
|||
|
// src = <20><>ū : <20><>
|
|||
|
extern void parse_token(char * src, char * token, char * value);
|
|||
|
|
|||
|
extern void trim_and_lower(const char * src, char * dest, size_t dest_size);
|
|||
|
|
|||
|
// <20><><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD> <20>ҹ<EFBFBD><D2B9>ڷ<EFBFBD>
|
|||
|
extern void lower_string(const char * src, char * dest, size_t dest_len);
|
|||
|
|
|||
|
// arg1<67><31> arg2<67><32> <20><><EFBFBD><EFBFBD><EFBFBD>ϴ°<CFB4>? (<28><><EFBFBD>ҹ<EFBFBD><D2B9><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>)
|
|||
|
extern int is_abbrev(char *arg1, char *arg2);
|
|||
|
|
|||
|
// a<><61> b<><62> <20>ð<EFBFBD><C3B0><EFBFBD> <20><EFBFBD> <20><><EFBFBD>̳<EFBFBD><CCB3><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern struct timeval * timediff(const struct timeval *a, const struct timeval *b);
|
|||
|
|
|||
|
// a<><61> <20>ð<EFBFBD><C3B0><EFBFBD> b<><62> <20>ð<EFBFBD><C3B0><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern struct timeval * timeadd(struct timeval *a, struct timeval *b);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD> <20>ð<EFBFBD> curr_tm<74><6D><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> days<79><73> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern struct tm * tm_calc(const struct tm *curr_tm, int days);
|
|||
|
|
|||
|
extern int MAX(int a, int b); // <20><><EFBFBD>߿<EFBFBD> ū <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern int MIN(int a, int b); // <20><><EFBFBD>߿<EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern int MINMAX(int min, int value, int max); // <20>ּ<EFBFBD> <20>ִ<EFBFBD> <20><><EFBFBD><EFBFBD> <20>Բ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
extern int number_ex(int from, int to, const char *file, int line); // from<6F><6D><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> to<74><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD>
|
|||
|
#define number(from, to) number_ex(from, to, __FILE__, __LINE__)
|
|||
|
|
|||
|
float fnumber(float from, float to);
|
|||
|
|
|||
|
extern void thecore_sleep(struct timeval * timeout); // timeout<75><74>ŭ <20><><EFBFBD>μ<EFBFBD><CEBC><EFBFBD> <20><><EFBFBD><EFBFBD>
|
|||
|
extern DWORD thecore_random(); // <20><><EFBFBD><EFBFBD> <20>Լ<EFBFBD>
|
|||
|
|
|||
|
extern float get_float_time();
|
|||
|
extern DWORD get_dword_time();
|
|||
|
|
|||
|
extern char * time_str(time_t ct);
|
|||
|
|
|||
|
#define CREATE(result, type, number) do { \
|
|||
|
if (!((result) = (type *) calloc ((number), sizeof(type)))) { \
|
|||
|
sys_err("calloc failed [%d] %s", errno, strerror(errno)); \
|
|||
|
abort(); } } while(0)
|
|||
|
|
|||
|
#define RECREATE(result,type,number) do { \
|
|||
|
if (!((result) = (type *) realloc ((result), sizeof(type) * (number)))) { \
|
|||
|
sys_err("realloc failed [%d] %s", errno, strerror(errno)); \
|
|||
|
abort(); } } while(0)
|
|||
|
|
|||
|
// Next <20><> Prev <20><> <20>ִ<EFBFBD> <20><><EFBFBD><EFBFBD>Ʈ<EFBFBD><C6AE> <20>߰<EFBFBD>
|
|||
|
#define INSERT_TO_TW_LIST(item, head, prev, next) \
|
|||
|
if (!(head)) \
|
|||
|
{ \
|
|||
|
head = item; \
|
|||
|
(head)->prev = (head)->next = NULL; \
|
|||
|
} \
|
|||
|
else \
|
|||
|
{ \
|
|||
|
(head)->prev = item; \
|
|||
|
(item)->next = head; \
|
|||
|
(item)->prev = NULL; \
|
|||
|
head = item; \
|
|||
|
}
|
|||
|
|
|||
|
#define REMOVE_FROM_TW_LIST(item, head, prev, next) \
|
|||
|
if ((item) == (head)) \
|
|||
|
{ \
|
|||
|
if (((head) = (item)->next)) \
|
|||
|
(head)->prev = NULL; \
|
|||
|
} \
|
|||
|
else \
|
|||
|
{ \
|
|||
|
if ((item)->next) \
|
|||
|
(item)->next->prev = (item)->prev; \
|
|||
|
\
|
|||
|
if ((item)->prev) \
|
|||
|
(item)->prev->next = (item)->next; \
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#define INSERT_TO_LIST(item, head, next) \
|
|||
|
(item)->next = (head); \
|
|||
|
(head) = (item); \
|
|||
|
|
|||
|
#define REMOVE_FROM_LIST(item, head, next) \
|
|||
|
if ((item) == (head)) \
|
|||
|
head = (item)->next; \
|
|||
|
else \
|
|||
|
{ \
|
|||
|
temp = head; \
|
|||
|
\
|
|||
|
while (temp && (temp->next != (item))) \
|
|||
|
temp = temp->next; \
|
|||
|
\
|
|||
|
if (temp) \
|
|||
|
temp->next = (item)->next; \
|
|||
|
} \
|
|||
|
|
|||
|
#ifndef MAKEFOURCC
|
|||
|
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
|||
|
((DWORD)(BYTE) (ch0 ) | ((DWORD)(BYTE) (ch1) << 8) | \
|
|||
|
((DWORD)(BYTE) (ch2) << 16) | ((DWORD)(BYTE) (ch3) << 24))
|
|||
|
#endif // defined(MAKEFOURCC)
|
|||
|
|
|||
|
#ifdef __cplusplus
|
|||
|
}
|
|||
|
#endif // __cplusplus
|
|||
|
|
|||
|
// _countof for gcc/g++
|
|||
|
#if !defined(_countof)
|
|||
|
#if !defined(__cplusplus)
|
|||
|
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
|
|||
|
#else
|
|||
|
extern "C++"
|
|||
|
{
|
|||
|
template <typename _CountofType, size_t _SizeOfArray>
|
|||
|
char (*__countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
|
|||
|
#define _countof(_Array) sizeof(*__countof_helper(_Array))
|
|||
|
}
|
|||
|
#endif
|
|||
|
#endif
|
|||
|
|
|||
|
#ifdef __WIN32__
|
|||
|
extern void gettimeofday(struct timeval* t, struct timezone* dummy);
|
|||
|
#endif
|
|||
|
|
|||
|
#endif // __INC_UTILS_H__
|