2
0

POSIXFileio.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "core/fileio.h"
  2. #include "core/util/tVector.h"
  3. #include "core/stringTable.h"
  4. #include "console/console.h"
  5. #include "core/strings/stringFunctions.h"
  6. #include "util/tempAlloc.h"
  7. #include "cinterface/c_controlInterface.h"
  8. #include "core/volume.h"
  9. /* these are for reading directors, getting stats, etc. */
  10. #include <dirent.h>
  11. #include <sys/stat.h>
  12. const int MaxPath = PATH_MAX;
  13. //------------------------------------------------------------------------------
  14. // munge the case of the specified pathName. This means try to find the actual
  15. // filename in with case-insensitive matching on the specified pathName, and
  16. // store the actual found name.
  17. bool ResolvePathCaseInsensitive(char* pathName, S32 pathNameSize, bool requiredAbsolute)
  18. {
  19. char tempBuf[MaxPath];
  20. dStrncpy(tempBuf, pathName, pathNameSize);
  21. // Check if we're an absolute path
  22. if (pathName[0] != '/')
  23. {
  24. AssertFatal(!requiredAbsolute, "PATH must be absolute");
  25. return false;
  26. }
  27. struct stat filestat;
  28. const int MaxPathEl = 200;
  29. char *currChar = pathName;
  30. char testPath[MaxPath];
  31. char pathEl[MaxPathEl];
  32. bool done = false;
  33. bool foundMatch = false;
  34. dStrncpy(tempBuf, "/", MaxPath);
  35. currChar++;
  36. while (!done)
  37. {
  38. char* termChar = dStrchr(currChar, '/');
  39. if (termChar == NULL)
  40. termChar = dStrchr(currChar, '\0');
  41. AssertFatal(termChar, "Can't find / or NULL terminator");
  42. S32 pathElLen = (termChar - currChar);
  43. dStrncpy(pathEl, currChar, pathElLen);
  44. pathEl[pathElLen] = '\0';
  45. dStrncpy(testPath, tempBuf, MaxPath);
  46. dStrcat(testPath, pathEl, MaxPath);
  47. if (stat(testPath, &filestat) != -1)
  48. {
  49. dStrncpy(tempBuf, testPath, MaxPath);
  50. }
  51. else
  52. {
  53. DIR *dir = opendir(tempBuf);
  54. struct dirent* ent;
  55. while (dir != NULL && (ent = readdir(dir)) != NULL)
  56. {
  57. if (dStricmp(pathEl, ent->d_name) == 0)
  58. {
  59. foundMatch = true;
  60. dStrcat(tempBuf, ent->d_name, MaxPath);
  61. break;
  62. }
  63. }
  64. if (!foundMatch)
  65. dStrncpy(tempBuf, testPath, MaxPath);
  66. if (dir)
  67. closedir(dir);
  68. }
  69. if (*termChar == '/')
  70. {
  71. dStrcat(tempBuf, "/", MaxPath);
  72. termChar++;
  73. currChar = termChar;
  74. }
  75. else
  76. done = true;
  77. }
  78. dStrncpy(pathName, tempBuf, pathNameSize);
  79. return foundMatch;
  80. }