| 1 |
一些基础函数 |
| 2 |
|
| 3 |
```c |
| 4 |
|
| 5 |
/* hv内存分配/释放函数 */ |
| 6 |
void* hv_malloc(size_t size); |
| 7 |
void* hv_realloc(void* oldptr, size_t newsize, size_t oldsize); |
| 8 |
void* hv_calloc(size_t nmemb, size_t size); |
| 9 |
void* hv_zalloc(size_t size); |
| 10 |
void hv_free(void* ptr); |
| 11 |
|
| 12 |
// 使用hv分配内存次数 |
| 13 |
long hv_alloc_cnt(); |
| 14 |
|
| 15 |
// 使用hv释放内存次数 |
| 16 |
long hv_free_cnt(); |
| 17 |
|
| 18 |
/* 字符串操作 */ |
| 19 |
// 字符串转大写 |
| 20 |
char* hv_strupper(char* str); |
| 21 |
// 字符串转小写 |
| 22 |
char* hv_strlower(char* str); |
| 23 |
// 字符串翻转 |
| 24 |
char* hv_strreverse(char* str); |
| 25 |
|
| 26 |
// 判断字符串是否以xxx开头 |
| 27 |
bool hv_strstartswith(const char* str, const char* start); |
| 28 |
|
| 29 |
// 判断字符串是否以xxx结尾 |
| 30 |
bool hv_strendswith(const char* str, const char* end); |
| 31 |
|
| 32 |
// 判断字符串是否包含xxx |
| 33 |
bool hv_strcontains(const char* str, const char* sub); |
| 34 |
|
| 35 |
// 安全的strncpy |
| 36 |
char* hv_strncpy(char* dest, const char* src, size_t n); |
| 37 |
|
| 38 |
// 安全的strncat |
| 39 |
char* hv_strncat(char* dest, const char* src, size_t n); |
| 40 |
|
| 41 |
// 字符查找 |
| 42 |
char* hv_strnchr(const char* s, char c, size_t n); |
| 43 |
|
| 44 |
// 查找最后一个点(通常用于提取文件后缀) |
| 45 |
#define hv_strrchr_dot(str) strrchr(str, '.') |
| 46 |
|
| 47 |
// 查找最后的路径(通常用于分离目录和文件) |
| 48 |
char* hv_strrchr_dir(const char* filepath); |
| 49 |
|
| 50 |
// 获取文件名(利用了上面的strrchr_dir) |
| 51 |
const char* hv_basename(const char* filepath); |
| 52 |
|
| 53 |
// 获取文件后缀(利用了上面的strrchr_dot) |
| 54 |
const char* hv_suffixname(const char* filename); |
| 55 |
|
| 56 |
/* 文件&目录 */ |
| 57 |
// mkdir -p: 创建目录 |
| 58 |
int hv_mkdir_p(const char* dir); |
| 59 |
// rmdir -p: 删除目录 |
| 60 |
int hv_rmdir_p(const char* dir); |
| 61 |
|
| 62 |
// 判断路径是否存在 |
| 63 |
bool hv_exists(const char* path); |
| 64 |
|
| 65 |
// 判断是否是目录 |
| 66 |
bool hv_isdir(const char* path); |
| 67 |
|
| 68 |
// 判断是否是文件 |
| 69 |
bool hv_isfile(const char* path); |
| 70 |
|
| 71 |
// 判断是否是链接 |
| 72 |
bool hv_islink(const char* path); |
| 73 |
|
| 74 |
// 获取文件大小 |
| 75 |
size_t hv_filesize(const char* filepath); |
| 76 |
|
| 77 |
// 获取可执行文件绝对路径,例如/usr/local/bin/httpd |
| 78 |
char* get_executable_path(char* buf, int size); |
| 79 |
|
| 80 |
// 获取可执行文件所在目录,例如/usr/local/bin |
| 81 |
char* get_executable_dir(char* buf, int size); |
| 82 |
|
| 83 |
// 获取可执行文件名,例如httpd |
| 84 |
char* get_executable_file(char* buf, int size); |
| 85 |
|
| 86 |
// 获取运行目录,例如/home/www/html |
| 87 |
char* get_run_dir(char* buf, int size); |
| 88 |
|
| 89 |
// 返回一个随机数 |
| 90 |
int hv_rand(int min, int max); |
| 91 |
|
| 92 |
// 返回一个随机字符串 |
| 93 |
char* hv_random_string(char *buf, int len); |
| 94 |
|
| 95 |
// 1 y on yes true enable返回true(通常用于配置文件) |
| 96 |
bool hv_getboolean(const char* str); |
| 97 |
|
| 98 |
// 解析size字符串 |
| 99 |
// 1T2G3M4K5B => ?B |
| 100 |
size_t hv_parse_size(const char* str); |
| 101 |
|
| 102 |
// 解析时间字符串 |
| 103 |
// 1w2d3h4m5s => ?s |
| 104 |
time_t hv_parse_time(const char* str); |
| 105 |
|
| 106 |
// 解析url字符串 |
| 107 |
int hv_parse_url(hurl_t* stURL, const char* strURL); |
| 108 |
|
| 109 |
``` |
| 110 |
|
| 111 |
单元测试代码见 [unittest/hbase_test.c](../../unittest/hbase_test.c) |