ManaPlus
tools.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "fs/virtfs/tools.h"
23 
24 #include "logger.h"
25 
26 #include "fs/paths.h"
27 
28 #include "fs/virtfs/fs.h"
29 #include "fs/virtfs/list.h"
30 
31 #include "utils/foreach.h"
32 #include "utils/stringutils.h"
33 
34 #include <algorithm>
35 #include <sstream>
36 
37 #include "debug.h"
38 
39 namespace VirtFs
40 {
41  void searchAndAddArchives(const std::string &restrict path,
42  const std::string &restrict ext,
43  const Append append)
44  {
45  List *const list = VirtFs::enumerateFiles(path);
46  FOR_EACH (StringVectCIter, i, list->names)
47  {
48  const std::string str = *i;
49  const size_t len = str.size();
50 
51  if (len > ext.length() &&
52  ext == str.substr(len - ext.length()))
53  {
54  const std::string file = path + str;
55  const std::string realPath = VirtFs::getRealDir(file);
56  VirtFs::mountZip(pathJoin(realPath, file), append);
57  }
58  }
59  VirtFs::freeList(list);
60  }
61 
62  void searchAndRemoveArchives(const std::string &restrict path,
63  const std::string &restrict ext)
64  {
65  List *const list = VirtFs::enumerateFiles(path);
66  FOR_EACH (StringVectCIter, i, list->names)
67  {
68  const std::string str = *i;
69  const size_t len = str.size();
70  if (len > ext.length() &&
71  ext == str.substr(len - ext.length()))
72  {
73  const std::string file = path + str;
74  const std::string realPath = VirtFs::getRealDir(file);
75  VirtFs::unmountZip(pathJoin(realPath, file));
76  }
77  }
78  VirtFs::freeList(list);
79  }
80 
81  void getFilesInDir(const std::string &dir,
82  StringVect &list,
83  const std::string &ext)
84  {
85  const std::string &path = dir;
86  StringVect tempList;
87  VirtFs::getFilesWithDir(path, tempList);
88  FOR_EACH (StringVectCIter, it, tempList)
89  {
90  const std::string &str = *it;
91  if (findLast(str, ext))
92  list.push_back(str);
93  }
94  std::sort(list.begin(), list.end());
95  }
96 
97  std::string getPath(const std::string &file)
98  {
99  // get the real path to the file
100  const std::string tmp = VirtFs::getRealDir(file);
101  std::string path;
102 
103  // if the file is not in the search path, then its empty
104  if (!tmp.empty())
105  {
106  path = pathJoin(tmp, file);
107 #if defined __native_client__
108  std::string dataZip = "/http/data.zip/";
109  if (path.substr(0, dataZip.length()) == dataZip)
110  path = path.replace(0, dataZip.length(), "/http/data/");
111 #endif // defined __native_client__
112  }
113  else
114  {
115  // if not found in search path return the default path
116  path = pathJoin(getPackageDir(), file);
117  }
118 
119  return path;
120  }
121 
122  std::string loadTextFileString(const std::string &fileName)
123  {
124  int contentsLength;
125  const char *fileContents = VirtFs::loadFile(fileName, contentsLength);
126 
127  if (fileContents == nullptr)
128  {
129  logger->log("Couldn't load text file: %s", fileName.c_str());
130  return std::string();
131  }
132  const std::string str = std::string(fileContents, contentsLength);
133  delete [] fileContents;
134  return str;
135  }
136 
137  bool loadTextFile(const std::string &fileName,
138  StringVect &lines)
139  {
140  int contentsLength;
141  const char *fileContents = VirtFs::loadFile(fileName, contentsLength);
142 
143  if (fileContents == nullptr)
144  {
145  logger->log("Couldn't load text file: %s", fileName.c_str());
146  return false;
147  }
148 
149  std::istringstream iss(std::string(fileContents, contentsLength));
150  std::string line;
151 
152  while (getline(iss, line))
153  lines.push_back(line);
154 
155  delete [] fileContents;
156  return true;
157  }
158 } // namespace VirtFs
bool Append
Definition: append.h:30
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define restrict
Definition: localconsts.h:165
Logger * logger
Definition: logger.cpp:89
std::string getPath(const std::string &file)
Definition: tools.cpp:97
std::string getRealDir(std::string fileName)
Definition: fs.cpp:746
std::string loadTextFileString(const std::string &fileName)
Definition: tools.cpp:122
void freeList(List *const handle)
Definition: fs.cpp:269
bool mountZip(std::string newDir, const Append append)
Definition: fs.cpp:590
void searchAndAddArchives(const std::string &path, const std::string &ext, const Append append)
Definition: tools.cpp:41
bool unmountZip(std::string oldDir)
Definition: fs.cpp:675
void getFilesWithDir(std::string dirName, StringVect &list)
Definition: fs.cpp:194
const char * loadFile(std::string filename, int &fileSize)
Definition: fs.cpp:859
List * enumerateFiles(std::string dirName)
Definition: fs.cpp:147
void searchAndRemoveArchives(const std::string &path, const std::string &ext)
Definition: tools.cpp:62
bool loadTextFile(const std::string &fileName, StringVect &lines)
Definition: tools.cpp:137
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:81
std::string getPackageDir()
Definition: paths.cpp:293
bool findLast(const std::string &str1, const std::string &str2)
std::string pathJoin(std::string str1, const std::string &str2)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29
StringVect names
Definition: list.h:40
std::string fileName
Definition: testmain.cpp:39