Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_FileIOHelper.c
Go to the documentation of this file.
2 {
3  protected static const string DELIMITER = "/";
4 
5  //------------------------------------------------------------------------------------------------
9  static bool CreateDirectory(string absoluteDirectory)
10  {
11  if (SCR_StringHelper.IsEmptyOrWhiteSpace(absoluteDirectory))
12  return false;
13 
14  if (FileIO.FileExists(absoluteDirectory))
15  return true;
16 
17  absoluteDirectory.Replace("\\", DELIMITER);
18  absoluteDirectory = SCR_StringHelper.ReplaceRecursive(absoluteDirectory, DELIMITER + DELIMITER, DELIMITER);
19 
20  if (FileIO.FileExists(absoluteDirectory))
21  return true;
22 
23  array<string> pieces = {};
24  absoluteDirectory.Split(DELIMITER, pieces, true);
25 
26  if (pieces.Count() < 2)
27  {
28  Print("Cannot create directory " + absoluteDirectory + "; not enough directories", LogLevel.DEBUG);
29  return false;
30  }
31 
32  string path = pieces[0]; // C: part on Windows
33  for (int i = 1, count = pieces.Count(); i < count; i++)
34  {
35  path += DELIMITER + pieces[i];
36 
37  if (FileIO.FileExists(path))
38  continue;
39 
40  if (!FileIO.MakeDirectory(path))
41  {
42  Print("Cannot create directory " + absoluteDirectory + "; blocked at " + path, LogLevel.DEBUG);
43  return false;
44  }
45  }
46 
47  return true;
48  }
49 
50  //------------------------------------------------------------------------------------------------
56  static bool Copy(string source, string destination, bool overwrite = true)
57  {
58  array<string> content = ReadFileContent(source);
59  if (!content)
60  {
61  Print("Cannot get file content - " + source, LogLevel.ERROR);
62  return false;
63  }
64 
65 #ifdef WORKBENCH
66  string absolutePath;
67  if (!FilePath.IsAbsolutePath(destination))
68  {
69  if (!Workbench.GetAbsolutePath(destination, absolutePath, false))
70  {
71  Print("Cannot get destination's absolute file path - " + destination, LogLevel.WARNING);
72  return false;
73  }
74  }
75 #endif // WORKBENCH
76 
77  if (!overwrite && FileIO.FileExists(destination))
78  {
79  Print("Not allowed to overwrite file - " + destination, LogLevel.WARNING);
80  return false;
81  }
82 
83  if (!WriteFileContent(destination, content))
84  {
85  Print("Cannot write file - " + destination, LogLevel.ERROR);
86  return false;
87  }
88 
89  return true;
90  }
91 
92  //------------------------------------------------------------------------------------------------
96  static array<string> ReadFileContent(string filePath)
97  {
98  FileHandle fileHandle = FileIO.OpenFile(filePath, FileMode.READ);
99  if (!fileHandle)
100  {
101  Print("Could not open " + filePath, LogLevel.WARNING);
102  return null;
103  }
104 
105  string lineContent;
106  array<string> result = {};
107  while (!fileHandle.IsEOF())
108  {
109  fileHandle.ReadLine(lineContent);
110  result.Insert(lineContent);
111  }
112 
113  fileHandle.Close();
114 
115  return result;
116  }
117 
118  //------------------------------------------------------------------------------------------------
124  static bool WriteFileContent(string filePath, notnull array<string> lines)
125  {
126  FileHandle fileHandle = FileIO.OpenFile(filePath, FileMode.WRITE);
127  if (!fileHandle)
128  {
129  Print("Could not open " + filePath, LogLevel.WARNING);
130  return false;
131  }
132 
133  int linesCountMinusOne = lines.Count() - 1;
134  if (linesCountMinusOne == -1)
135  {
136  fileHandle.Write(string.Empty); // needed?
137  }
138  else
139  {
140  foreach (int lineNumber, string line : lines)
141  {
142  if (lineNumber < linesCountMinusOne)
143  fileHandle.WriteLine(line);
144  }
145 
146  // avoid final line return due to WriteLine
147  fileHandle.Write(lines[linesCountMinusOne]);
148  }
149 
150  fileHandle.Close();
151 
152  return true;
153  }
154 }
SCR_StringHelper
Definition: SCR_StringHelper.c:1
SCR_FileIOHelper
Definition: SCR_FileIOHelper.c:1