Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

139 řádky
2.7 KiB

  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 _WIN32
  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. } else
  78. {
  79. len = 0;
  80. b3Assert(maxPathLength>0);
  81. if (maxPathLength>0)
  82. {
  83. path[len] = 0;
  84. }
  85. }
  86. return len;
  87. }
  88. static char toLowerChar(const char t)
  89. {
  90. if (t>=(char)'A' && t<=(char)'Z')
  91. return t + ((char)'a' - (char)'A');
  92. else
  93. return t;
  94. }
  95. static void toLower(char* str)
  96. {
  97. int len=strlen(str);
  98. for (int i=0;i<len;i++)
  99. {
  100. str[i] = toLowerChar(str[i]);
  101. }
  102. }
  103. /*static const char* strip2(const char* name, const char* pattern)
  104. {
  105. size_t const patlen = strlen(pattern);
  106. size_t patcnt = 0;
  107. const char * oriptr;
  108. const char * patloc;
  109. // find how many times the pattern occurs in the original string
  110. for (oriptr = name; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
  111. {
  112. patcnt++;
  113. }
  114. return oriptr;
  115. }
  116. */
  117. };
  118. #endif //B3_FILE_UTILS_H