tool.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * 文件名: tool.h
  3. * 目标: aFun tool公共API
  4. * aFunTool是aFun实用工具库, 内含aFun调用的实用函数
  5. */
  6. #ifndef AFUN__TOOL_H
  7. #define AFUN__TOOL_H
  8. #include <wchar.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include "macro.h"
  12. #ifdef _MSC_VER
  13. #include "../src/deps/dlfcn/dlfcn.h"
  14. #else
  15. #include <dlfcn.h>
  16. #endif
  17. /* md5计算工具 */
  18. #define READ_DATA_SIZE (1024)
  19. #define MD5_SIZE (16)
  20. #define MD5_STR_LEN (MD5_SIZE * 2)
  21. #define MD5_STRING (MD5_STR_LEN + 1)
  22. int getFileMd5(const char *path, char *md5str);
  23. /* 哈希工具 */
  24. typedef long int time33_t;
  25. time33_t time33(char *str);
  26. time33_t w_time33(wchar_t *str);
  27. /* 字符串工具 */
  28. #define EQ_STR(str1, str2) (!strcmp((str1), (str2)))
  29. #define EQ_WSTR(wid1, wid2) (!wcscmp((wid1), (wid2)))
  30. #define pathCopy(path) ((FilePath)strCopy((char *)(path)))
  31. #define NEW_STR(size) (char *)calloc((size) + 1, sizeof(char))
  32. #define NEW_WSTR(size) (wchar_t *)calloc((size) + 1, sizeof(wchar_t))
  33. #define STR_LEN(p) (((p) == NULL) ? 0 : strlen((p)))
  34. #define WSTR_LEN(p) (((p) == NULL) ? 0 : wcslen((p)))
  35. char *strCopy(const char *str);
  36. wchar_t *wstrCopy(const wchar_t *str);
  37. wchar_t *wstrWithWchar(wchar_t *str, size_t size, int free_old, ...);
  38. wchar_t *wstrWithWchar_(wchar_t *str, wint_t new, bool free_old);
  39. wchar_t *wstrExpansion(wchar_t *str, size_t size, bool free_old);
  40. char *strJoinIter(char *base, int free_base, ...);
  41. char *strJoin(char *first, char *second, bool free_first, bool free_last);
  42. char *strJoin_(char *first, char *second, bool free_first, bool free_last);
  43. wchar_t *wstrJoin(wchar_t *first, wchar_t *second, bool free_first, bool free_last);
  44. wchar_t *wstrJoin_(wchar_t *first, wchar_t *second, bool free_first, bool free_last);
  45. wchar_t *wstrCopySelf(wchar_t *str, long times);
  46. wchar_t *wstrReverse(wchar_t *str);
  47. wchar_t *convertToWstr(char *str, bool free_old);
  48. char *convertToStr(wchar_t *wstr, bool free_old);
  49. /* 路径工具 */
  50. #ifdef __linux__
  51. #define SEP "/"
  52. #define SEP_CH '/'
  53. #define SHARED_MARK ".so"
  54. #else
  55. #define SEP "\\"
  56. #define SEP_CH '\\'
  57. #define SHARED_MARK ".dll"
  58. #endif
  59. /* 文件处理工具 */
  60. int checkFile(char *path);
  61. char *getFileName(char *path_1);
  62. char *fileNameToVar(char *name, bool need_free);
  63. char *findPath(char *path, char *env, bool need_free);
  64. /* 信号处理工具 */
  65. typedef int vsignal;
  66. typedef struct SignalTag SignalTag;
  67. struct SignalTag{
  68. volatile vsignal signum; // 信号
  69. volatile enum SignalType{
  70. signal_reset=0, // 没有信号
  71. signal_appear, // 信号未被处理
  72. } status;
  73. };
  74. extern volatile struct SignalTag signal_tag; // 在tool.c中定义
  75. void afSignalHandler(int signum);
  76. /* 时间工具 */
  77. void safeSleep(double ms);
  78. /* 动态库工具(dlc): 处理动态库的使用 */
  79. /*
  80. * NEW_DLC_SYMBOL: 用于定义指定类型的symbol结构体
  81. * DLC_SYMBOL: 指定类型的symbol结构体名
  82. * GET_SYMBOL: 访问symbol成员的值
  83. * MAKE_SYMBOL: 生成一个symbol
  84. * COPY_SYMBOL: 拷贝一个symbol(拷贝其引用)
  85. * READ_SYMBOL: 在dlc中获取一个symbol
  86. * FREE_SYMBOL: 释放symbol
  87. *
  88. * openLibary: 打开动态库
  89. * freeLibary: 释放动态库
  90. * dlcExit: 释放所有动态库
  91. */
  92. #define DEFINE_DLC_SYMBOL(NAME) typedef struct DLC##NAME##SYMBOL *pDLC##NAME##SYMBOL
  93. #define NEW_DLC_SYMBOL(TYPE, NAME) typedef struct DLC##NAME##SYMBOL { \
  94. TYPE *symbol; \
  95. struct DlcHandle *dlc; \
  96. } DLC##NAME##SYMBOL, *pDLC##NAME##SYMBOL
  97. #define DLC_SYMBOL(NAME) pDLC##NAME##SYMBOL
  98. #define GET_SYMBOL(SYMBOL) (*((SYMBOL)->symbol))
  99. #define MAKE_SYMBOL(symbol, TYPE) ((pDLC##TYPE##SYMBOL) (makeSymbol_(symbol)))
  100. #define COPY_SYMBOL(ds, TYPE) ((pDLC##TYPE##SYMBOL) (copySymbol_((DlcSymbol_ *)(ds))))
  101. #define READ_SYMBOL(dlc, name, TYPE) ((pDLC##TYPE##SYMBOL) (getSymbol_((dlc), (name))))
  102. #define FREE_SYMBOL(symbol) ((symbol) != NULL ? (freeSymbol_((DlcSymbol_ *)(symbol)), NULL) : NULL)
  103. typedef struct DlcSymbol_ DlcSymbol_;
  104. typedef struct DlcHandle DlcHandle;
  105. struct DlcHandle *openLibary(const char *file, int mode);
  106. struct DlcSymbol_ *makeSymbol_(void *symbol);
  107. struct DlcSymbol_ *copySymbol_(struct DlcSymbol_ *ds);
  108. struct DlcSymbol_ *getSymbol_(struct DlcHandle *dlc, const char *name);
  109. void freeSymbol_(struct DlcSymbol_ *symbol);
  110. bool freeLibary(struct DlcHandle *dlc);
  111. void dlcExit(void);
  112. // byte工具
  113. #define byteWriteInt_8(file, s) (byteWriteUint_8(file, ((uint8_t)(s))))
  114. #define byteWriteInt_16(file, s) (byteWriteUint_16(file, ((uint16_t)(s))))
  115. #define byteWriteInt_32(file, s) (byteWriteUint_32(file, ((uint32_t)(s))))
  116. #define byteWriteInt_64(file, s) (byteWriteUint_64(file, ((uint64_t)(s))))
  117. #define byteReadInt_8(file, s) (byteReadUint_8(file, ((uint8_t *)(s))))
  118. #define byteReadInt_16(file, s) (byteReadUint_16(file, ((uint16_t *)(s))))
  119. #define byteReadInt_32(file, s) (byteReadUint_32(file, ((uint32_t *)(s))))
  120. #define byteReadInt_64(file, s) (byteReadUint_64(file, ((uint64_t *)(s))))
  121. enum af_EndianType{
  122. little_endian = 0,
  123. big_endian
  124. };
  125. extern enum af_EndianType endian;
  126. void getEndian();
  127. bool byteWriteUint_8(FILE *file, uint8_t ch);
  128. bool byteWriteUint_16(FILE *file, uint16_t num);
  129. bool byteWriteUint_32(FILE *file, uint32_t num);
  130. bool byteWriteUint_64(FILE *file, uint64_t num);
  131. bool byteWriteStr(FILE *file, char *str);
  132. bool byteReadUint_8(FILE *file, uint8_t *ch);
  133. bool byteReadUint_16(FILE *file, uint16_t *num);
  134. bool byteReadUint_32(FILE *file, uint32_t *num);
  135. bool byteReadUint_64(FILE *file, uint64_t *num);
  136. bool byteReadStr(FILE *file, char **str);
  137. #endif //AFUN__TOOL_H