b3FileUtils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef B3_FILE_UTILS_H
  2. #define B3_FILE_UTILS_H
  3. #include <stdio.h>
  4. #include "b3Scalar.h"
  5. #include <stddef.h> //ptrdiff_h
  6. #include <string.h>
  7. struct b3FileUtils
  8. {
  9. b3FileUtils()
  10. {
  11. }
  12. virtual ~b3FileUtils()
  13. {
  14. }
  15. static bool findFile(const char* orgFileName, char* relativeFileName, int maxRelativeFileNameMaxLen)
  16. {
  17. FILE* f = 0;
  18. f = fopen(orgFileName, "rb");
  19. if (f)
  20. {
  21. //printf("original file found: [%s]\n", orgFileName);
  22. sprintf(relativeFileName, "%s", orgFileName);
  23. fclose(f);
  24. return true;
  25. }
  26. //printf("Trying various directories, relative to current working directory\n");
  27. const char* prefix[] = {"./", "./data/", "../data/", "../../data/", "../../../data/", "../../../../data/"};
  28. int numPrefixes = sizeof(prefix) / sizeof(const char*);
  29. f = 0;
  30. bool fileFound = false;
  31. for (int i = 0; !f && i < numPrefixes; i++)
  32. {
  33. #ifdef _MSC_VER
  34. sprintf_s(relativeFileName, maxRelativeFileNameMaxLen, "%s%s", prefix[i], orgFileName);
  35. #else
  36. sprintf(relativeFileName, "%s%s", prefix[i], orgFileName);
  37. #endif
  38. f = fopen(relativeFileName, "rb");
  39. if (f)
  40. {
  41. fileFound = true;
  42. break;
  43. }
  44. }
  45. if (f)
  46. {
  47. fclose(f);
  48. }
  49. return fileFound;
  50. }
  51. static const char* strip2(const char* name, const char* pattern)
  52. {
  53. size_t const patlen = strlen(pattern);
  54. size_t patcnt = 0;
  55. const char* oriptr;
  56. const char* patloc;
  57. // find how many times the pattern occurs in the original string
  58. for (oriptr = name; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
  59. {
  60. patcnt++;
  61. }
  62. return oriptr;
  63. }
  64. static int extractPath(const char* fileName, char* path, int maxPathLength)
  65. {
  66. const char* stripped = strip2(fileName, "/");
  67. stripped = strip2(stripped, "\\");
  68. ptrdiff_t len = stripped - fileName;
  69. b3Assert((len + 1) < maxPathLength);
  70. if (len && ((len + 1) < maxPathLength))
  71. {
  72. for (int i = 0; i < len; i++)
  73. {
  74. path[i] = fileName[i];
  75. }
  76. path[len] = 0;
  77. }
  78. else
  79. {
  80. len = 0;
  81. b3Assert(maxPathLength > 0);
  82. if (maxPathLength > 0)
  83. {
  84. path[len] = 0;
  85. }
  86. }
  87. return len;
  88. }
  89. static char toLowerChar(const char t)
  90. {
  91. if (t >= (char)'A' && t <= (char)'Z')
  92. return t + ((char)'a' - (char)'A');
  93. else
  94. return t;
  95. }
  96. static void toLower(char* str)
  97. {
  98. int len = strlen(str);
  99. for (int i = 0; i < len; i++)
  100. {
  101. str[i] = toLowerChar(str[i]);
  102. }
  103. }
  104. /*static const char* strip2(const char* name, const char* pattern)
  105. {
  106. size_t const patlen = strlen(pattern);
  107. size_t patcnt = 0;
  108. const char * oriptr;
  109. const char * patloc;
  110. // find how many times the pattern occurs in the original string
  111. for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  112. {
  113. patcnt++;
  114. }
  115. return oriptr;
  116. }
  117. */
  118. };
  119. #endif //B3_FILE_UTILS_H