b3FileUtils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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. int result = 0;
  32. for (int i=0;!f && i<numPrefixes;i++)
  33. {
  34. #ifdef _WIN32
  35. sprintf_s(relativeFileName,maxRelativeFileNameMaxLen,"%s%s",prefix[i],orgFileName);
  36. #else
  37. sprintf(relativeFileName,"%s%s",prefix[i],orgFileName);
  38. #endif
  39. f = fopen(relativeFileName,"rb");
  40. if (f)
  41. {
  42. fileFound = true;
  43. break;
  44. }
  45. }
  46. if (f)
  47. {
  48. fclose(f);
  49. }
  50. return fileFound;
  51. }
  52. static const char* strip2(const char* name, const char* pattern)
  53. {
  54. size_t const patlen = strlen(pattern);
  55. size_t patcnt = 0;
  56. const char * oriptr;
  57. const char * patloc;
  58. // find how many times the pattern occurs in the original string
  59. for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  60. {
  61. patcnt++;
  62. }
  63. return oriptr;
  64. }
  65. static void extractPath(const char* fileName, char* path, int maxPathLength)
  66. {
  67. const char* stripped = strip2(fileName, "/");
  68. stripped = strip2(stripped, "\\");
  69. ptrdiff_t len = stripped-fileName;
  70. b3Assert((len+1)<maxPathLength);
  71. if (len && ((len+1)<maxPathLength))
  72. {
  73. for (int i=0;i<len;i++)
  74. {
  75. path[i] = fileName[i];
  76. }
  77. path[len]=0;
  78. } else
  79. {
  80. b3Assert(maxPathLength>0);
  81. if (maxPathLength>0)
  82. {
  83. path[0] = 0;
  84. }
  85. }
  86. }
  87. static char toLowerChar(const char t)
  88. {
  89. if (t>=(char)'A' && t<=(char)'Z')
  90. return t + ((char)'a' - (char)'A');
  91. else
  92. return t;
  93. }
  94. static void toLower(char* str)
  95. {
  96. int len=strlen(str);
  97. for (int i=0;i<len;i++)
  98. {
  99. str[i] = toLowerChar(str[i]);
  100. }
  101. }
  102. /*static const char* strip2(const char* name, const char* pattern)
  103. {
  104. size_t const patlen = strlen(pattern);
  105. size_t patcnt = 0;
  106. const char * oriptr;
  107. const char * patloc;
  108. // find how many times the pattern occurs in the original string
  109. for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  110. {
  111. patcnt++;
  112. }
  113. return oriptr;
  114. }
  115. */
  116. };
  117. #endif //B3_FILE_UTILS_H