tool.h 5.1 KB

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