Misc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include <string>
  3. #include <comutil.h>
  4. #pragma comment(lib, "comsupp.lib")
  5. #pragma comment(lib, "comsuppw.lib")
  6. inline std::string ws2s(const std::wstring& ws)
  7. {
  8. _bstr_t t = ws.c_str();
  9. char* pchar = (char*)t;
  10. return std::string(pchar);
  11. }
  12. inline std::wstring s2ws(const std::string& s)
  13. {
  14. _bstr_t t = s.c_str();
  15. wchar_t* pwchar = (wchar_t*)t;
  16. return std::wstring(pwchar);
  17. }
  18. inline int CheckTemplate(char* expr, char* tok_end, const char* group, char* prev, int* deep)
  19. {
  20. char tmp = 0;
  21. char *lastwc, *prev_tok;
  22. const char *cp;
  23. //---
  24. if ((*deep)++ >= 10) return(FALSE);
  25. //---
  26. while (*expr == '*' && expr != tok_end) expr++;
  27. if (expr == tok_end) return(TRUE);
  28. //---
  29. lastwc = expr;
  30. while (*lastwc != '*' && *lastwc != 0) lastwc++;
  31. //---
  32. if ((tmp = *(lastwc)) != 0) //
  33. {
  34. tmp = *(lastwc); *(lastwc) = 0;
  35. if ((prev_tok = (char*)strstr(group, expr)) == NULL)
  36. {
  37. if (tmp != 0) *(lastwc) = tmp;
  38. return(FALSE);
  39. }
  40. *(lastwc) = tmp;
  41. }
  42. else //
  43. {
  44. //---
  45. cp = group + strlen(group);
  46. for (; cp >= group; cp--)
  47. if (*cp == expr[0] && strcmp(cp, expr) == 0)
  48. return(TRUE);
  49. return(FALSE);
  50. }
  51. //---
  52. if (prev != NULL && prev_tok <= prev) return(FALSE);
  53. prev = prev_tok;
  54. //---
  55. group = prev_tok + (lastwc - expr - 1);
  56. //---
  57. if (lastwc != tok_end) return CheckTemplate(lastwc, tok_end, group, prev, deep);
  58. //---
  59. return(TRUE);
  60. }
  61. inline int CheckTemplate(wchar_t* expr, wchar_t* tok_end, const wchar_t* group, wchar_t* prev, int* deep)
  62. {
  63. wchar_t tmp = 0;
  64. wchar_t *lastwc, *prev_tok;
  65. const wchar_t *cp;
  66. //---
  67. if ((*deep)++ >= 10) return(FALSE);
  68. //---
  69. while (*expr == L'*' && expr != tok_end) expr++;
  70. if (expr == tok_end) return(TRUE);
  71. //---
  72. lastwc = expr;
  73. while (*lastwc != L'*' && *lastwc != 0) lastwc++;
  74. //---
  75. if ((tmp = *(lastwc)) != 0) //
  76. {
  77. tmp = *(lastwc); *(lastwc) = 0;
  78. if ((prev_tok = (wchar_t*)wcsstr(group, expr)) == NULL)
  79. {
  80. if (tmp != 0) *(lastwc) = tmp;
  81. return(FALSE);
  82. }
  83. *(lastwc) = tmp;
  84. }
  85. else //
  86. {
  87. //---
  88. cp = group + wcslen(group);
  89. for (; cp >= group; cp--)
  90. if (*cp == expr[0] && wcscmp(cp, expr) == 0)
  91. return(TRUE);
  92. return(FALSE);
  93. }
  94. //---
  95. if (prev != NULL && prev_tok <= prev) return(FALSE);
  96. prev = prev_tok;
  97. //---
  98. group = prev_tok + (lastwc - expr - 1);
  99. //---
  100. if (lastwc != tok_end) return CheckTemplate(lastwc, tok_end, group, prev, deep);
  101. //---
  102. return(TRUE);
  103. }
  104. inline int CheckGroup(char* grouplist, const char *group)
  105. {
  106. //---
  107. if (grouplist == NULL || group == NULL) return(FALSE);
  108. char *tok_start = grouplist, end;
  109. int res = TRUE, deep = 0, normal_mode;
  110. while (*tok_start != 0)
  111. {
  112. //---
  113. while (*tok_start == ',') tok_start++;
  114. //---
  115. if (*tok_start == '!') { tok_start++; normal_mode = FALSE; }
  116. else normal_mode = TRUE;
  117. //---
  118. char *tok_end = tok_start;
  119. while (*tok_end != ',' && *tok_end != 0) tok_end++;
  120. end = *tok_end; *tok_end = NULL;
  121. //---
  122. char *tp = tok_start;
  123. const char *gp = group;
  124. char *prev = NULL;
  125. res = TRUE;
  126. while (tp != tok_end && *gp != NULL)
  127. {
  128. if (*tp == '*')
  129. {
  130. deep = 0;
  131. if ((res = CheckTemplate(tp, tok_end, gp, prev, &deep)) == TRUE)
  132. {
  133. *tok_end = end;
  134. return(normal_mode);
  135. }
  136. break;
  137. }
  138. if (*tp != *gp)
  139. {
  140. *tok_end = end;
  141. res = FALSE;
  142. break;
  143. }
  144. tp++;
  145. gp++;
  146. }
  147. *tok_end = end;
  148. if (*gp == NULL && (tp == tok_end || *tp == '*') && res == TRUE) return(normal_mode);
  149. if (*tok_end == 0) break;
  150. tok_start = tok_end + 1;
  151. }
  152. return(FALSE);
  153. }
  154. inline int CheckGroup(wchar_t* grouplist, const wchar_t *group)
  155. {
  156. //---
  157. if (grouplist == NULL || group == NULL) return(FALSE);
  158. wchar_t *tok_start = grouplist, end;
  159. int res = TRUE, deep = 0, normal_mode;
  160. while (*tok_start != 0)
  161. {
  162. //---
  163. while (*tok_start == L',') tok_start++;
  164. //---
  165. if (*tok_start == L'!') { tok_start++; normal_mode = FALSE; }
  166. else normal_mode = TRUE;
  167. //---
  168. wchar_t *tok_end = tok_start;
  169. while (*tok_end != L',' && *tok_end != 0) tok_end++;
  170. end = *tok_end; *tok_end = NULL;
  171. //---
  172. wchar_t *tp = tok_start;
  173. const wchar_t *gp = group;
  174. wchar_t *prev = NULL;
  175. res = TRUE;
  176. while (tp != tok_end && *gp != NULL)
  177. {
  178. if (*tp == L'*')
  179. {
  180. deep = 0;
  181. if ((res = CheckTemplate(tp, tok_end, gp, prev, &deep)) == TRUE)
  182. {
  183. *tok_end = end;
  184. return(normal_mode);
  185. }
  186. break;
  187. }
  188. if (*tp != *gp)
  189. {
  190. *tok_end = end;
  191. res = FALSE;
  192. break;
  193. }
  194. tp++;
  195. gp++;
  196. }
  197. *tok_end = end;
  198. if (*gp == NULL && (tp == tok_end || *tp == L'*') && res == TRUE) return(normal_mode);
  199. if (*tok_end == 0) break;
  200. tok_start = tok_end + 1;
  201. }
  202. return(FALSE);
  203. }