hbase.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef HV_BASE_H_
  2. #define HV_BASE_H_
  3. #include "hexport.h"
  4. #include "hplatform.h" // for bool
  5. #include "hdef.h" // for printd
  6. BEGIN_EXTERN_C
  7. //--------------------alloc/free---------------------------
  8. HV_EXPORT void* hv_malloc(size_t size);
  9. HV_EXPORT void* hv_realloc(void* oldptr, size_t newsize, size_t oldsize);
  10. HV_EXPORT void* hv_calloc(size_t nmemb, size_t size);
  11. HV_EXPORT void* hv_zalloc(size_t size);
  12. HV_EXPORT void hv_free(void* ptr);
  13. #define HV_ALLOC(ptr, size)\
  14. do {\
  15. *(void**)&(ptr) = hv_zalloc(size);\
  16. printd("alloc(%p, size=%llu)\tat [%s:%d:%s]\n", ptr, (unsigned long long)size, __FILE__, __LINE__, __FUNCTION__);\
  17. } while(0)
  18. #define HV_ALLOC_SIZEOF(ptr) HV_ALLOC(ptr, sizeof(*(ptr)))
  19. #define HV_FREE(ptr)\
  20. do {\
  21. if (ptr) {\
  22. hv_free(ptr);\
  23. printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
  24. ptr = NULL;\
  25. }\
  26. } while(0)
  27. #define STACK_OR_HEAP_ALLOC(ptr, size, stack_size)\
  28. unsigned char _stackbuf_[stack_size] = { 0 };\
  29. if ((size) > (stack_size)) {\
  30. HV_ALLOC(ptr, size);\
  31. } else {\
  32. *(unsigned char**)&(ptr) = _stackbuf_;\
  33. }
  34. #define STACK_OR_HEAP_FREE(ptr)\
  35. if ((unsigned char*)(ptr) != _stackbuf_) {\
  36. HV_FREE(ptr);\
  37. }
  38. #define HV_DEFAULT_STACKBUF_SIZE 1024
  39. #define HV_STACK_ALLOC(ptr, size) STACK_OR_HEAP_ALLOC(ptr, size, HV_DEFAULT_STACKBUF_SIZE)
  40. #define HV_STACK_FREE(ptr) STACK_OR_HEAP_FREE(ptr)
  41. HV_EXPORT long hv_alloc_cnt();
  42. HV_EXPORT long hv_free_cnt();
  43. HV_INLINE void hv_memcheck(void) {
  44. printf("Memcheck => alloc:%ld free:%ld\n", hv_alloc_cnt(), hv_free_cnt());
  45. }
  46. #define HV_MEMCHECK atexit(hv_memcheck);
  47. //--------------------string-------------------------------
  48. HV_EXPORT char* hv_strupper(char* str);
  49. HV_EXPORT char* hv_strlower(char* str);
  50. HV_EXPORT char* hv_strreverse(char* str);
  51. HV_EXPORT bool hv_strstartswith(const char* str, const char* start);
  52. HV_EXPORT bool hv_strendswith(const char* str, const char* end);
  53. HV_EXPORT bool hv_strcontains(const char* str, const char* sub);
  54. HV_EXPORT bool hv_wildcard_match(const char* str, const char* pattern);
  55. // strncpy n = sizeof(dest_buf)-1
  56. // hv_strncpy n = sizeof(dest_buf)
  57. HV_EXPORT char* hv_strncpy(char* dest, const char* src, size_t n);
  58. // strncat n = sizeof(dest_buf)-1-strlen(dest)
  59. // hv_strncpy n = sizeof(dest_buf)
  60. HV_EXPORT char* hv_strncat(char* dest, const char* src, size_t n);
  61. #if !HAVE_STRLCPY
  62. #define strlcpy hv_strncpy
  63. #endif
  64. #if !HAVE_STRLCAT
  65. #define strlcat hv_strncat
  66. #endif
  67. HV_EXPORT char* hv_strnchr(const char* s, char c, size_t n);
  68. #define hv_strrchr_dot(str) strrchr(str, '.')
  69. HV_EXPORT char* hv_strrchr_dir(const char* filepath);
  70. // basename
  71. HV_EXPORT const char* hv_basename(const char* filepath);
  72. HV_EXPORT const char* hv_suffixname(const char* filename);
  73. // mkdir -p
  74. HV_EXPORT int hv_mkdir_p(const char* dir);
  75. // rmdir -p
  76. HV_EXPORT int hv_rmdir_p(const char* dir);
  77. // path
  78. HV_EXPORT bool hv_exists(const char* path);
  79. HV_EXPORT bool hv_isdir(const char* path);
  80. HV_EXPORT bool hv_isfile(const char* path);
  81. HV_EXPORT bool hv_islink(const char* path);
  82. HV_EXPORT size_t hv_filesize(const char* filepath);
  83. HV_EXPORT char* get_executable_path(char* buf, int size);
  84. HV_EXPORT char* get_executable_dir(char* buf, int size);
  85. HV_EXPORT char* get_executable_file(char* buf, int size);
  86. HV_EXPORT char* get_run_dir(char* buf, int size);
  87. // random
  88. HV_EXPORT int hv_rand(int min, int max);
  89. HV_EXPORT char* hv_random_string(char *buf, int len);
  90. // 1 y on yes true enable => true
  91. HV_EXPORT bool hv_getboolean(const char* str);
  92. // 1T2G3M4K5B => ?B
  93. HV_EXPORT size_t hv_parse_size(const char* str);
  94. // 1w2d3h4m5s => ?s
  95. HV_EXPORT time_t hv_parse_time(const char* str);
  96. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  97. typedef enum {
  98. HV_URL_SCHEME,
  99. HV_URL_USERNAME,
  100. HV_URL_PASSWORD,
  101. HV_URL_HOST,
  102. HV_URL_PORT,
  103. HV_URL_PATH,
  104. HV_URL_QUERY,
  105. HV_URL_FRAGMENT,
  106. HV_URL_FIELD_NUM,
  107. } hurl_field_e;
  108. typedef struct hurl_s {
  109. struct {
  110. unsigned short off;
  111. unsigned short len;
  112. } fields[HV_URL_FIELD_NUM];
  113. unsigned short port;
  114. } hurl_t;
  115. HV_EXPORT int hv_parse_url(hurl_t* stURL, const char* strURL);
  116. END_EXTERN_C
  117. #endif // HV_BASE_H_