ManaPlus
Functions
Files Namespace Reference

Functions

int renameFile (const std::string &pFrom, const std::string &pTo)
 
int copyFile (const std::string &pFrom, const std::string &pTo)
 
bool existsLocal (const std::string &path)
 
bool loadTextFileLocal (const std::string &fileName, StringVect &lines)
 
void saveTextFile (const std::string &path, const std::string &name, const std::string &text)
 
void deleteFilesInDirectory (std::string path)
 
void enumFiles (StringVect &files, std::string path, const bool skipSymlinks)
 

Function Documentation

◆ copyFile()

int Files::copyFile ( const std::string &  pFrom,
const std::string &  pTo 
)

Definition at line 176 of file files.cpp.

178 {
179  FILE *srcFile = fopen(srcName.c_str(), "rb");
180  if (srcFile == nullptr)
181  return -1;
182  FILE *dstFile = fopen(dstName.c_str(), "w+b");
183  if (dstFile == nullptr)
184  {
185  fclose(srcFile);
186  return -1;
187  }
188 
189  const int chunkSize = 512000;
190  char *buf = new char[chunkSize];
191  size_t sz = 0;
192  while ((sz = fread(buf, 1, chunkSize, srcFile)) != 0U)
193  {
194  if (fwrite(buf, 1, sz, dstFile) != sz)
195  {
196  delete [] buf;
197  fclose(srcFile);
198  fclose(dstFile);
199  return -1;
200  }
201  }
202 
203  delete [] buf;
204  fclose(srcFile);
205  fclose(dstFile);
206  return 0;
207 }

Referenced by ConfigManager::backupConfig().

◆ deleteFilesInDirectory()

void Files::deleteFilesInDirectory ( std::string  path)

Definition at line 272 of file files.cpp.

273 {
274  path = pathJoin(path, dirSeparator);
275  const dirent *next_file = nullptr;
276  DIR *const dir = opendir(path.c_str());
277 
278  if (dir != nullptr)
279  {
280  while ((next_file = readdir(dir)) != nullptr)
281  {
282  const std::string file = next_file->d_name;
283  if (file != "." && file != "..")
284  remove((path + file).c_str());
285  }
286  closedir(dir);
287  }
288 }
const char * dirSeparator
Definition: fs.cpp:43
bool remove(const std::string &filename)
Definition: fs.cpp:780
std::string pathJoin(std::string str1, const std::string &str2)

References dirSeparator, pathJoin(), and VirtFs::remove().

◆ enumFiles()

void Files::enumFiles ( StringVect files,
std::string  path,
const bool  skipSymlinks 
)

Definition at line 290 of file files.cpp.

293 {
294  if (findLast(path, dirSeparator) == false)
295  path += dirSeparator;
296  DIR *const dir = opendir(path.c_str());
297 
298  if (dir != nullptr)
299  {
300  const dirent *next_file = nullptr;
301  while ((next_file = readdir(dir)) != nullptr)
302  {
303  const std::string file = next_file->d_name;
304  if (file == "." || file == "..")
305  continue;
306 #ifndef WIN32
307  if (skipSymlinks == true)
308  {
309  struct stat statbuf;
310  if (lstat(path.c_str(), &statbuf) == 0 &&
311  S_ISLNK(statbuf.st_mode) != 0)
312  {
313  continue;
314  }
315  }
316 #endif // WIN32
317  files.push_back(file);
318  }
319  closedir(dir);
320  }
321 }
bool findLast(const std::string &str1, const std::string &str2)

References dirSeparator, and findLast().

◆ existsLocal()

bool Files::existsLocal ( const std::string &  path)

Definition at line 209 of file files.cpp.

210 {
211  struct stat statbuf;
212 #ifdef WIN32
213  // in windows path\file.ext\ by default detected as exists
214  // if file.ext is not directory, need return false
215  const bool res = (stat(path.c_str(), &statbuf) == 0);
216  if (res == false)
217  return false;
218  if ((findLast(path, "/") == true || findLast(path, "\\") == true) &&
219  S_ISDIR(statbuf.st_mode) == 0)
220  {
221  return false;
222  }
223  return true;
224 #else // WIN32
225  return stat(path.c_str(), &statbuf) == 0;
226 #endif // WIN32
227 }

References findLast().

Referenced by ConfigManager::backupConfig(), VirtFs::FsDir::exists(), getPicturesDir(), VirtFs::FsDir::getRealDir(), Configuration::init(), Being::loadComment(), VirtFs::FsDir::loadFile(), VirtFs::mountDir(), VirtFs::mountDir2(), VirtFs::mountDirSilent(), VirtFs::mountDirSilent2(), VirtFs::mountZip(), VirtFs::mountZip2(), Font::openFont(), and VirtFs::FsDir::openInternal().

◆ loadTextFileLocal()

bool Files::loadTextFileLocal ( const std::string &  fileName,
StringVect lines 
)

Definition at line 229 of file files.cpp.

231 {
232  std::ifstream file;
233  char line[501];
234 
235  file.open(fileName.c_str(), std::ios::in);
236 
237  if (!file.is_open())
238  {
239  reportAlways("Couldn't load text file: %s",
240  fileName.c_str())
241  return false;
242  }
243 
244  while (file.getline(line, 500))
245  lines.push_back(line);
246 
247  return true;
248 }
#define reportAlways(...)
Definition: checkutils.h:253
std::string fileName
Definition: testmain.cpp:39

References fileName, and reportAlways.

Referenced by ConfigManager::backupConfig(), getPicturesDir(), Being::loadComment(), and UpdaterWindow::loadFile().

◆ renameFile()

int Files::renameFile ( const std::string &  pFrom,
const std::string &  pTo 
)

Definition at line 134 of file files.cpp.

136 {
137 #if defined __native_client__
138  FILE *srcFile = fopen(srcName.c_str(), "rb");
139  if (srcFile == nullptr)
140  return -1;
141  FILE *dstFile = fopen(dstName.c_str(), "w+b");
142  if (dstFile == nullptr)
143  {
144  fclose(srcFile);
145  return -1;
146  }
147 
148  const int chunkSize = 500000;
149  char *buf = new char[chunkSize];
150  size_t sz = 0;
151  while ((sz = fread(buf, 1, chunkSize, srcFile)))
152  {
153  if (fwrite(buf, 1, sz, dstFile) != sz)
154  {
155  delete [] buf;
156  fclose(srcFile);
157  fclose(dstFile);
158  ::remove(dstName.c_str());
159  return -1;
160  }
161  }
162 
163  delete [] buf;
164  fclose(srcFile);
165  fclose(dstFile);
166  if (!::remove(srcName.c_str()))
167  return 0;
168 
169  return -1;
170 #else // defined __native_client__
171 
172  return ::rename(srcName.c_str(), dstName.c_str());
173 #endif // defined __native_client__
174 }

References VirtFs::remove().

Referenced by ConfigManager::backupConfig(), and Net::Download::downloadThread().

◆ saveTextFile()

void Files::saveTextFile ( const std::string &  path,
const std::string &  name,
const std::string &  text 
)

Definition at line 250 of file files.cpp.

253 {
254  if (mkdir_r(path.c_str()) == 0)
255  {
256  std::ofstream file;
257  std::string fileName = pathJoin(path, name);
258  file.open(fileName.c_str(), std::ios::out);
259  if (file.is_open())
260  {
261  file << text << std::endl;
262  }
263  else
264  {
265  reportAlways("Error opening file for writing: %s",
266  fileName.c_str())
267  }
268  file.close();
269  }
270 }
int mkdir_r(const char *const pathname)
Create a directory, making leading components first if necessary.
Definition: mkdir.cpp:109

References fileName, mkdir_r(), pathJoin(), and reportAlways.

Referenced by Being::saveComment(), and ActorManager::updateNameId().