FileOperations.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "FileOperations.h"
  11. #if _RAKNET_SUPPORT_FileOperations==1
  12. #include "RakMemoryOverride.h"
  13. #include "_FindFirst.h" // For linux
  14. #include <stdio.h>
  15. #include <string.h>
  16. #ifdef _WIN32
  17. // For mkdir
  18. #include <direct.h>
  19. #include <io.h>
  20. #else
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include "_FindFirst.h"
  24. #endif
  25. #include "errno.h"
  26. #ifndef MAX_PATH
  27. #define MAX_PATH 260
  28. #endif
  29. #ifdef _MSC_VER
  30. #pragma warning( push )
  31. #endif
  32. #ifdef _MSC_VER
  33. #pragma warning( disable : 4996 ) // mkdir declared deprecated by Microsoft in order to make it harder to be cross platform. I don't agree it's deprecated.
  34. #endif
  35. bool WriteFileWithDirectories( const char *path, char *data, unsigned dataLength )
  36. {
  37. int index;
  38. FILE *fp;
  39. char pathCopy[MAX_PATH];
  40. int res;
  41. if ( path == 0 || path[ 0 ] == 0 )
  42. return false;
  43. strcpy( pathCopy, path );
  44. // Ignore first / if there is one
  45. if (pathCopy[0])
  46. {
  47. index = 1;
  48. while ( pathCopy[ index ] )
  49. {
  50. if ( pathCopy[ index ] == '/' || pathCopy[ index ] == '\\')
  51. {
  52. pathCopy[ index ] = 0;
  53. #ifdef _WIN32
  54. res = _mkdir( pathCopy );
  55. #else
  56. res = mkdir( pathCopy, 0744 );
  57. #endif
  58. if (res<0 && errno!=EEXIST && errno!=EACCES)
  59. {
  60. return false;
  61. }
  62. pathCopy[ index ] = '/';
  63. }
  64. index++;
  65. }
  66. }
  67. if (data)
  68. {
  69. fp = fopen( path, "wb" );
  70. if ( fp == 0 )
  71. {
  72. return false;
  73. }
  74. fwrite( data, 1, dataLength, fp );
  75. fclose( fp );
  76. }
  77. else
  78. {
  79. #ifdef _WIN32
  80. #pragma warning( disable : 4996 ) // mkdir declared deprecated by Microsoft in order to make it harder to be cross platform. I don't agree it's deprecated.
  81. res = _mkdir( pathCopy );
  82. #else
  83. res = mkdir( pathCopy, 0744 );
  84. #endif
  85. if (res<0 && errno!=EEXIST)
  86. {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. bool IsSlash(unsigned char c)
  93. {
  94. return c=='/' || c=='\\';
  95. }
  96. void AddSlash( char *input )
  97. {
  98. if (input==0 || input[0]==0)
  99. return;
  100. int lastCharIndex=(int) strlen(input)-1;
  101. if (input[lastCharIndex]=='\\')
  102. input[lastCharIndex]='/';
  103. else if (input[lastCharIndex]!='/')
  104. {
  105. input[lastCharIndex+1]='/';
  106. input[lastCharIndex+2]=0;
  107. }
  108. }
  109. bool DirectoryExists(const char *directory)
  110. {
  111. _finddata_t fileInfo;
  112. intptr_t dir;
  113. char baseDirWithStars[560];
  114. strcpy(baseDirWithStars, directory);
  115. AddSlash(baseDirWithStars);
  116. strcat(baseDirWithStars, "*.*");
  117. dir=_findfirst(baseDirWithStars, &fileInfo );
  118. if (dir==-1)
  119. return false;
  120. _findclose(dir);
  121. return true;
  122. }
  123. void QuoteIfSpaces(char *str)
  124. {
  125. unsigned i;
  126. bool hasSpace=false;
  127. for (i=0; str[i]; i++)
  128. {
  129. if (str[i]==' ')
  130. {
  131. hasSpace=true;
  132. break;
  133. }
  134. }
  135. if (hasSpace)
  136. {
  137. int len=(int)strlen(str);
  138. memmove(str+1, str, len);
  139. str[0]='\"';
  140. str[len]='\"';
  141. str[len+1]=0;
  142. }
  143. }
  144. unsigned int GetFileLength(const char *path)
  145. {
  146. FILE *fp = fopen(path, "rb");
  147. if (fp==0) return 0;
  148. fseek(fp, 0, SEEK_END);
  149. unsigned int fileLength = ftell(fp);
  150. fclose(fp);
  151. return fileLength;
  152. }
  153. #ifdef _MSC_VER
  154. #pragma warning( pop )
  155. #endif
  156. #endif // _RAKNET_SUPPORT_FileOperations
粤ICP备19079148号