PlatformHelper.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "../Common.h"
  3. #define OUTRESULT(res) do { if (outResult != NULL) *outResult = (res); } while (0)
  4. static bool TryStringOut(const Beefy::String& str, char* outStr, int* inOutSize)
  5. {
  6. if ((outStr == NULL) || (*inOutSize < str.length() + 1))
  7. {
  8. if ((outStr != NULL) && (*inOutSize != 0))
  9. outStr[0] = 0; // Return empty string
  10. *inOutSize = (int)str.length() + 1;
  11. return false;
  12. }
  13. *inOutSize = (int)str.length() + 1;
  14. memcpy(outStr, str.c_str(), (int)str.length() + 1);
  15. return true;
  16. }
  17. static bool TryStringOut(const Beefy::String& str, char* outStr, int* inOutSize, BfpResult* outResult)
  18. {
  19. if (TryStringOut(str, outStr, inOutSize))
  20. {
  21. OUTRESULT(BfpResult_Ok);
  22. return true;
  23. }
  24. else
  25. {
  26. OUTRESULT(BfpResult_InsufficientBuffer);
  27. return false;
  28. }
  29. }
  30. static BfpResult BfpGetStrHelper(Beefy::StringImpl& outStr, std::function<void(char* outStr, int* inOutStrSize, BfpResult* result)> func)
  31. {
  32. const int initSize = 4096;
  33. char localBuf[initSize];
  34. int strSize = initSize;
  35. BfpResult result = BfpResult_Ok;
  36. func(localBuf, &strSize, &result);
  37. if (result == BfpResult_Ok)
  38. {
  39. outStr.Append(localBuf, strSize - 1);
  40. return BfpResult_Ok;
  41. }
  42. else if (result == BfpResult_InsufficientBuffer)
  43. {
  44. while (true)
  45. {
  46. char* localBufPtr = (char*)malloc(strSize);
  47. func(localBufPtr, &strSize, &result);
  48. if (result == BfpResult_InsufficientBuffer)
  49. {
  50. free(localBufPtr);
  51. continue;
  52. }
  53. outStr.Append(localBuf, strSize - 1);
  54. free(localBufPtr);
  55. return BfpResult_Ok;
  56. }
  57. }
  58. return result;
  59. }
  60. #define BFP_GETSTR_HELPER(STRNAME, __RESULT, CMD) \
  61. { \
  62. int strLen = 0; \
  63. int* __STRLEN = &strLen; \
  64. char* __STR = NULL; \
  65. CMD; \
  66. if ((BfpResult)__RESULT == BfpResult_InsufficientBuffer) \
  67. { \
  68. STRNAME.Reserve(strLen); \
  69. __STR = STRNAME.GetMutablePtr(); \
  70. CMD; \
  71. STRNAME.mLength = strLen - 1; \
  72. }\
  73. }