SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileOperations.cpp
Go to the documentation of this file.
1 /*
2  * Original work: 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  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
17 #if _RAKNET_SUPPORT_FileOperations==1
19 #include "slikenet/_FindFirst.h" // For linux
20 #include <stdio.h>
21 #include <string.h>
22 #ifdef _WIN32
23 // For mkdir
24 #include <direct.h>
25 #include <io.h>
26 #else
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include "slikenet/_FindFirst.h"
30 #endif
31 #include "errno.h"
32 #include "slikenet/linux_adapter.h"
33 #include "slikenet/osx_adapter.h"
34 
35 #ifndef MAX_PATH
36 #define MAX_PATH 260
37 #endif
38 
39 bool WriteFileWithDirectories( const char *path, char *data, unsigned dataLength )
40 {
41  int index;
42  FILE *fp;
43  char pathCopy[MAX_PATH];
44  int res;
45 
46  if ( path == 0 || path[ 0 ] == 0 )
47  return false;
48 
49  strcpy_s( pathCopy, path );
50 
51  // Ignore first / if there is one
52  if (pathCopy[0])
53  {
54  index = 1;
55  while ( pathCopy[ index ] )
56  {
57  if ( pathCopy[ index ] == '/' || pathCopy[ index ] == '\\')
58  {
59  pathCopy[ index ] = 0;
60 
61  #ifdef _WIN32
62  res = _mkdir( pathCopy );
63  #else
64 
65  res = mkdir( pathCopy, 0744 );
66  #endif
67  if (res<0 && errno!=EEXIST && errno!=EACCES)
68  {
69  return false;
70  }
71 
72  pathCopy[ index ] = '/';
73  }
74 
75  index++;
76  }
77  }
78 
79  if (data)
80  {
81  if ( fopen_s( &fp, path, "wb" ) != 0 )
82  {
83  return false;
84  }
85 
86  fwrite( data, 1, dataLength, fp );
87 
88  fclose( fp );
89  }
90  else
91  {
92 #ifdef _WIN32
93  res = _mkdir( pathCopy );
94 #else
95  res = mkdir( pathCopy, 0744 );
96 #endif
97 
98  if (res<0 && errno!=EEXIST)
99  {
100  return false;
101  }
102  }
103 
104  return true;
105 }
106 bool IsSlash(unsigned char c)
107 {
108  return c=='/' || c=='\\';
109 }
110 
111 void AddSlash( char *input )
112 {
113  if (input==0 || input[0]==0)
114  return;
115 
116  int lastCharIndex=(int) strlen(input)-1;
117  if (input[lastCharIndex]=='\\')
118  input[lastCharIndex]='/';
119  else if (input[lastCharIndex]!='/')
120  {
121  input[lastCharIndex+1]='/';
122  input[lastCharIndex+2]=0;
123  }
124 }
125 bool DirectoryExists(const char *directory)
126 {
127  _finddata_t fileInfo;
128  intptr_t dir;
129  char baseDirWithStars[560];
130  strcpy_s(baseDirWithStars, directory);
131  AddSlash(baseDirWithStars);
132  strcat_s(baseDirWithStars, "*.*");
133  dir=_findfirst(baseDirWithStars, &fileInfo );
134  if (dir==-1)
135  return false;
136  _findclose(dir);
137  return true;
138 }
139 void QuoteIfSpaces(char *str)
140 {
141  unsigned i;
142  bool hasSpace=false;
143  for (i=0; str[i]; i++)
144  {
145  if (str[i]==' ')
146  {
147  hasSpace=true;
148  break;
149  }
150  }
151  if (hasSpace)
152  {
153  int len=(int)strlen(str);
154  memmove(str+1, str, len);
155  str[0]='\"';
156  str[len]='\"';
157  str[len+1]=0;
158  }
159 }
160 unsigned int GetFileLength(const char *path)
161 {
162  FILE *fp;
163  if (fopen_s(&fp, path, "rb")!=0) return 0;
164  fseek(fp, 0, SEEK_END);
165  unsigned int fileLength = ftell(fp);
166  fclose(fp);
167  return fileLength;
168 
169 }
170 
171 #endif // _RAKNET_SUPPORT_FileOperations