2
0

BeefFuzz.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/util/Array.h"
  4. #include "BeefySysLib/util/String.h"
  5. #include <set>
  6. //#include <direct.h>
  7. #pragma warning(disable:4996)
  8. NS_BF_BEGIN
  9. #define APPEND2(VAL1, VAL2) VAL1##VAL2
  10. #define APPEND(VAL1, VAL2) APPEND2(VAL1, VAL2)
  11. #define ENUM_VAL_GENERATE(ENUM_ENTRY) APPEND(ENUM_TYPE, _##ENUM_ENTRY),
  12. #define ENUM_NAME_GENERATE(ENUM_ENTRY) #ENUM_ENTRY,
  13. #define ENUM_CREATE_DO2(EnumName) \
  14. static const char* EnumName##_Names[] = { ENUM_DECLARE(ENUM_NAME_GENERATE) }; \
  15. enum EnumName { ENUM_DECLARE(ENUM_VAL_GENERATE) }; \
  16. static bool EnumParse(const String& name, EnumName& result) \
  17. { \
  18. for (int i = 0; i < sizeof(EnumName##_Names)/sizeof(const char*); i++) \
  19. if (name == EnumName##_Names[i]) { result = (EnumName)i; return true; } \
  20. return false; \
  21. } \
  22. static const char* EnumToString(EnumName enumVal) \
  23. { \
  24. return EnumName##_Names[(int)enumVal]; \
  25. }
  26. #define ENUM_CREATE_DO(EnumType) ENUM_CREATE_DO2(EnumType)
  27. #define ENUM_CREATE ENUM_CREATE_DO(ENUM_TYPE)
  28. class IDEUtils
  29. {
  30. public:
  31. static bool FixFilePath(String& filePath)
  32. {
  33. if (filePath.length() == 0)
  34. return false;
  35. if (filePath[0] == '<')
  36. return false;
  37. if ((filePath.length() > 1) && (filePath[1] == ':'))
  38. filePath[0] = tolower(filePath[0]);
  39. bool prevWasSlash = false;
  40. for (int i = 0; i < filePath.length(); i++)
  41. {
  42. //if ((filePath[i] == '/') && (filePath[i - 1])
  43. if (filePath[i] == DIR_SEP_CHAR_ALT)
  44. filePath[i] = DIR_SEP_CHAR;
  45. if (filePath[i] == DIR_SEP_CHAR)
  46. {
  47. if ((prevWasSlash) && (i > 1))
  48. {
  49. filePath.Remove(i, 1);
  50. i--;
  51. continue;
  52. }
  53. prevWasSlash = true;
  54. }
  55. else
  56. prevWasSlash = false;
  57. if ((i >= 4) && (filePath[i - 3] == DIR_SEP_CHAR) && (filePath[i - 2] == '.') && (filePath[i - 1] == '.') && (filePath[i] == DIR_SEP_CHAR))
  58. {
  59. int prevSlash = (int)filePath.LastIndexOf(DIR_SEP_CHAR, i - 4);
  60. if (prevSlash != -1)
  61. {
  62. filePath.Remove(prevSlash, i - prevSlash);
  63. i = prevSlash;
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. static void GetDirWithSlash(String& dirName)
  70. {
  71. if (dirName.length() == 0)
  72. return;
  73. char lastC = dirName[dirName.length() - 1];
  74. if ((lastC != '\\') && (lastC != '/'))
  75. dirName += DIR_SEP_CHAR;
  76. }
  77. static FILE* CreateFileWithDir(const String& fileName, const char* options)
  78. {
  79. FILE* fp = fopen(fileName.c_str(), options);
  80. if (fp == NULL)
  81. {
  82. String fileDir = GetFileDir(fileName);
  83. if (!fileDir.empty())
  84. {
  85. RecursiveCreateDirectory(fileDir);
  86. fp = fopen(fileName.c_str(), "w");
  87. }
  88. }
  89. return fp;
  90. }
  91. static bool WriteAllText(const String& fileName, const String& data)
  92. {
  93. FILE* fp = CreateFileWithDir(fileName, "w");
  94. if (fp == NULL)
  95. return false;
  96. fwrite(data.c_str(), 1, data.length(), fp);
  97. fclose(fp);
  98. return true;
  99. }
  100. static void GetFileNameWithoutExtension(const String& filePath, String& outFileName)
  101. {
  102. outFileName += GetFileName(filePath);
  103. int dot = (int)outFileName.LastIndexOf('.');
  104. if (dot != -1)
  105. outFileName.RemoveToEnd(dot);
  106. }
  107. static void GetExtension(const String& filePath, String& ext)
  108. {
  109. int idx = (int)filePath.LastIndexOf('.');
  110. if (idx != -1)
  111. ext = filePath.Substring(idx);
  112. }
  113. static void AppendWithOptionalQuotes(String& targetStr, const String& srcFileName)
  114. {
  115. if ((int)srcFileName.IndexOf(' ') == -1)
  116. targetStr += srcFileName;
  117. else
  118. targetStr += "\"" + srcFileName + "\"";
  119. }
  120. };
  121. class ArgBuilder
  122. {
  123. public:
  124. String* mTarget;
  125. bool mDoLongBreak;
  126. int mLastBreak;
  127. std::multiset<String> mLinkPaths;
  128. public:
  129. ArgBuilder(String& target, bool doLongBreak)
  130. {
  131. mTarget = &target;
  132. mDoLongBreak = doLongBreak;
  133. if (mDoLongBreak)
  134. mLastBreak = (int)mTarget->LastIndexOf('\n');
  135. else
  136. mLastBreak = 0;
  137. }
  138. void AddSep()
  139. {
  140. if (mDoLongBreak)
  141. {
  142. if (mTarget->length() - mLastBreak > 0x1F000)
  143. {
  144. mLastBreak = (int)mTarget->length();
  145. mTarget->Append('\n');
  146. return;
  147. }
  148. }
  149. mTarget->Append(' ');
  150. }
  151. void AddFileName(const String& filePath)
  152. {
  153. IDEUtils::AppendWithOptionalQuotes(*mTarget, filePath);
  154. }
  155. };
  156. NS_BF_END