ManaPlus
Functions
paths.h File Reference

(986a3bf)

#include <string>
#include "localconsts.h"

Go to the source code of this file.

Functions

std::string getRealPath (const std::string &str)
 
bool isRealPath (const std::string &str)
 
bool checkPath (const std::string &path)
 
void prepareFsPath (std::string &path)
 
std::string & fixDirSeparators (std::string &str)
 
std::string removeLast (const std::string &str)
 
std::string getSelfName ()
 
std::string getPicturesDir ()
 
std::string getPackageDir ()
 
void setPackageDir (const std::string &dir)
 
std::string getHomePath ()
 

Function Documentation

◆ checkPath()

bool checkPath ( const std::string &  path)

Definition at line 121 of file paths.cpp.

122 {
123  if (path.empty())
124  return true;
125  return path.find("../") == std::string::npos
126  && path.find("..\\") == std::string::npos
127  && path.find("/..") == std::string::npos
128  && path.find("\\..") == std::string::npos
129  && path.find("(unreachable)") == std::string::npos;
130 }

Referenced by VirtFs::enumerateFiles(), VirtFs::exists(), VirtFs::getDirs(), VirtFs::getFiles(), VirtFs::getFilesWithDir(), VirtFs::getRealDir(), ConfigManager::initConfiguration(), Dirs::initUpdatesDir(), VirtFs::isDirectory(), VirtFs::FsDir::isSymbolicLink(), VirtFs::loadFile(), HelpWindow::loadHelp(), HelpWindow::loadHelpSimple(), loadHostsGroup(), loadTxtFile(), loadXMLFile(), VirtFs::openAppend(), VirtFs::openRead(), VirtFs::openWrite(), parseOptions(), LoginDialog::prepareUpdate(), Ea::LoginRecv::processUpdateHost(), and EAthena::LoginRecv::processUpdateHost2().

◆ fixDirSeparators()

std::string& fixDirSeparators ( std::string &  str)

Definition at line 149 of file paths.cpp.

150 {
151 #ifdef WIN32
152  return replaceAll(str, "/", "\\");
153 #else
154  return str;
155 #endif
156 }
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)

References replaceAll().

Referenced by Font::Font(), and Font::loadFont().

◆ getHomePath()

std::string getHomePath ( )

Definition at line 252 of file paths.cpp.

253 {
254 #if defined(UNITTESTS) && defined(UNITESTSDIR)
255  std::string dir = UNITESTSDIR;
256  if (findLast(dir, std::string(dirSeparator)) == false)
257  dir += dirSeparator;
258  return dir;
259 #else // defined(UNITTESTS) && defined(UNITESTSDIR)
260 #ifdef WIN32
261  return getSpecialFolderLocation(CSIDL_LOCAL_APPDATA);
262 #elif defined(__SWITCH__)
263  return VirtFs::getBaseDir();
264 #else
265  const char *path = getenv("HOME");
266  if (path == nullptr)
267  {
268  const uid_t uid = getuid();
269  const passwd *const pw = getpwuid(uid);
270  if (pw != nullptr &&
271  pw->pw_dir != nullptr)
272  {
273  path = pw->pw_dir;
274  }
275  if (path == nullptr)
276  return dirSeparator;
277  }
278  std::string dir = path;
279  if (findLast(dir, std::string(dirSeparator)) == false)
280  dir += dirSeparator;
281  return dir;
282 #endif // WIN32
283 #endif // defined(UNITTESTS) && defined(UNITESTSDIR)
284 }
const char * dirSeparator
Definition: fs.cpp:43
const char * getBaseDir()
Definition: fs.cpp:79
bool findLast(const std::string &str1, const std::string &str2)

References dirSeparator, findLast(), and VirtFs::getBaseDir().

Referenced by VirtFs::FsDir::init().

◆ getPackageDir()

std::string getPackageDir ( )

Definition at line 293 of file paths.cpp.

294 {
295  return mPackageDir;
296 }

References anonymous_namespace{paths.cpp}::mPackageDir.

Referenced by VirtFs::getPath().

◆ getPicturesDir()

std::string getPicturesDir ( )

Definition at line 207 of file paths.cpp.

208 {
209 #ifdef WIN32
210  std::string dir = getSpecialFolderLocation(CSIDL_MYPICTURES);
211  if (dir.empty())
212  dir = getSpecialFolderLocation(CSIDL_DESKTOP);
213  return dir;
214 #elif defined USE_X11
215  char *xdg = getenv("XDG_CONFIG_HOME");
216  std::string file;
217  if (!xdg)
218  {
219  file = pathJoin(VirtFs::getUserDir(),
220  ".config/user-dirs.dirs");
221  }
222  else
223  {
224  file = pathJoin(xdg, "user-dirs.dirs");
225  }
226 
227  if (Files::existsLocal(file))
228  {
229  StringVect arr;
230  Files::loadTextFileLocal(file, arr);
231  FOR_EACH (StringVectCIter, it, arr)
232  {
233  std::string str = *it;
234  if (findCutFirst(str, "XDG_PICTURES_DIR=\""))
235  {
236  str = str.substr(0, str.size() - 1);
237  // use hack to replace $HOME var.
238  // if in string other vars, fallback to default path
239  replaceAll(str, "$HOME/", VirtFs::getUserDir());
240  str = getRealPath(str);
241  if (str.empty())
242  str = pathJoin(VirtFs::getUserDir(), "Desktop");
243  return str;
244  }
245  }
246  }
247 #endif // WIN32
248 
249  return pathJoin(VirtFs::getUserDir(), "Desktop");
250 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
bool loadTextFileLocal(const std::string &fileName, StringVect &lines)
Definition: files.cpp:229
bool existsLocal(const std::string &path)
Definition: files.cpp:209
const char * getUserDir()
Definition: fs.cpp:84
std::string getRealPath(const std::string &str)
Definition: paths.cpp:84
bool findCutFirst(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

References Files::existsLocal(), findCutFirst(), FOR_EACH, getRealPath(), VirtFs::getUserDir(), Files::loadTextFileLocal(), pathJoin(), and replaceAll().

Referenced by Dirs::initScreenshotDir().

◆ getRealPath()

std::string getRealPath ( const std::string &  str)

Definition at line 84 of file paths.cpp.

85 {
86 #if defined(__GLIBC__)
87  if (str.find("(unreachable)") != std::string::npos)
88  return "";
89 #endif // defined(__GLIBC__)
90 
91  // possible fix for realpath overflow
92  if (str.size() > SSIZE_MAX / 10)
93  {
94  return std::string();
95  }
96 #if defined(__OpenBSD__) || defined(__ANDROID__) || \
97  defined(__native_client__) || defined(__SWITCH__)
98  char *realPath = reinterpret_cast<char*>(calloc(PATH_MAX, sizeof(char)));
99  if (!realPath)
100  return "";
101  realpath(str.c_str(), realPath);
102 #else // defined(__OpenBSD__) || defined(__ANDROID__) ||
103  // defined(__native_client__)
104 
105  char *realPath = realpath(str.c_str(), nullptr);
106  if (realPath == nullptr)
107  return "";
108 #endif // defined(__OpenBSD__) || defined(__ANDROID__) ||
109  // defined(__native_client__)
110 
111  std::string path = realPath;
112  free(realPath);
113  return path;
114 }

Referenced by getPicturesDir(), Configuration::init(), VirtFs::FsDir::init(), and isRealPath().

◆ getSelfName()

std::string getSelfName ( )

Definition at line 200 of file paths.cpp.

201 {
202  return "";
203 }

Referenced by TestMain::TestMain().

◆ isRealPath()

bool isRealPath ( const std::string &  str)

Definition at line 116 of file paths.cpp.

117 {
118  return str == getRealPath(str);
119 }

References getRealPath().

Referenced by Dirs::updateDataPath().

◆ prepareFsPath()

void prepareFsPath ( std::string &  path)

◆ removeLast()

std::string removeLast ( const std::string &  str)

Definition at line 158 of file paths.cpp.

159 {
160  size_t pos2 = str.rfind('/');
161  const size_t pos3 = str.rfind('\\');
162  if (pos3 != std::string::npos)
163  {
164  if (pos2 == std::string::npos || pos3 > pos2)
165  pos2 = pos3;
166  }
167  if (pos2 != std::string::npos)
168  return str.substr(0, pos2);
169  return str;
170 }

◆ setPackageDir()

void setPackageDir ( const std::string &  dir)

Definition at line 298 of file paths.cpp.

299 {
300  mPackageDir = dir;
301 }

References anonymous_namespace{paths.cpp}::mPackageDir.

Referenced by Dirs::mountDataDir().