1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/config-manager.h"
#include "common/fs.h"
#include "common/std/regex.h"
#include "ags/shared/core/platform.h"
#include "ags/shared/util/string_utils.h"
#include "ags/shared/util/directory.h"
#include "ags/shared/util/path.h"
#include "ags/shared/util/stdio_compat.h"
namespace AGS3 {
namespace AGS {
namespace Shared {
const char *SAVE_FOLDER_PREFIX = "/saves/";
namespace Directory {
bool CreateDirectory(const String &path) {
return Common::FSNode(path.GetCStr()).createDirectory();
}
bool CreateAllDirectories(const String &parent, const String &sub_dirs) {
if (sub_dirs == SAVE_FOLDER_PREFIX)
// ScummVM save folder doesn't need creating
return true;
if (parent.IsEmpty() || !ags_directory_exists(parent.GetCStr()))
return false; // no sense, or base dir not exist
if (sub_dirs.IsEmpty())
return true; // nothing to create, so fine
String make_path = String::FromFormat("%s/", parent.GetCStr());
for (const char *sect = sub_dirs.GetCStr();
sect < sub_dirs.GetCStr() + sub_dirs.GetLength();) {
const char *cur = sect + 1;
for (; *cur && *cur != '/' && *cur != PATH_ALT_SEPARATOR; ++cur);
// Skip empty dirs (duplicated separators etc)
if ((cur - sect == 1) && (*sect == '.' || *sect == '/' || *sect == PATH_ALT_SEPARATOR)) {
sect = cur;
continue;
}
// In case of ".." just fail
if (strncmp(sect, "..", cur - sect) == 0)
return false;
make_path.Append(sect, cur - sect);
if (!CreateDirectory(make_path))
return false;
sect = cur;
}
return true;
}
String SetCurrentDirectory(const String &path) {
warning("TODO: SetCurrentDirectory: %s", path.GetCStr());
// chdir(path);
// return GetCurrentDirectory();
return path;
}
String GetCurrentDirectory() {
#ifdef TODO
char buf[512];
getcwd(buf, 512);
String str(buf);
Path::FixupPath(str);
return str;
#else
// Use / separator
return ConfMan.getPath("path").toString('/');
#endif
}
static bool GetFilesImpl(const String &dir_path, std::vector<String> &files, bool isDirectories) {
Common::FSNode fsNode(dir_path.GetCStr());
Common::FSList fsList;
fsNode.getChildren(fsList,
isDirectories ? Common::FSNode::kListDirectoriesOnly :
Common::FSNode::kListFilesOnly);
for (uint i = 0; i < fsList.size(); ++i)
files.emplace_back(fsList[i].getName());
return true;
}
bool GetDirs(const String &dir_path, std::vector<String> &dirs) {
return GetFilesImpl(dir_path, dirs, true);
}
bool GetFiles(const String &dir_path, std::vector<String> &files) {
return GetFilesImpl(dir_path, files, false);
}
} // namespace Directory
FindFile::~FindFile() {
Close();
}
FindFile FindFile::Open(const String &path, const String &wildcard, bool do_file, bool do_dir) {
FindFile ff;
ff._folder = Common::FSNode(path.GetCStr());
Common::FSNode::ListMode mode = Common::FSNode::kListAll;
if (do_file && !do_dir)
mode = Common::FSNode::kListFilesOnly;
else if (!do_file && do_dir)
mode = Common::FSNode::kListDirectoriesOnly;
warning("TODO: Wildcard not yet supported - %s", wildcard.GetCStr());
ff._folder.getChildren(ff._files, mode);
return ff;
}
void FindFile::Close() {
_index = 0;
}
bool FindFile::Next() {
++_index;
return _index < (int)_files.size();
}
FindFileRecursive::~FindFileRecursive() {
Close();
}
FindFileRecursive FindFileRecursive::Open(const String &path, const String &wildcard, size_t max_level) {
FindFile fdir = FindFile::OpenDirs(path);
FindFile ffile = FindFile::OpenFiles(path, wildcard);
if (ffile.AtEnd() && fdir.AtEnd())
return {}; // return invalid object
FindFileRecursive ff;
ff._fdir = std::move(fdir);
ff._ffile = std::move(ffile);
// Try get the first matching entry
if (ff._ffile.AtEnd() && !ff.Next())
return {}; // return invalid object
ff._maxLevel = max_level;
ff._fullDir = path;
ff._curFile = ff._ffile.Current();
return ff; // success
}
void FindFileRecursive::Close() {
while (!_fdirs.empty())
_fdirs.pop();
_fdir.Close();
_ffile.Close();
}
bool FindFileRecursive::Next() {
// Look up for the next file in the current dir
if (_ffile.Next()) {
Path::ConcatPaths(_curFile, _curDir, _ffile.Current());
return true;
}
// No more files? Find a directory that still has
while (_ffile.AtEnd()) {
// first make sure there are unchecked subdirs left in current dir
while (_fdir.AtEnd()) { // if not, go up, until found any, or hit the top
if (!PopDir())
return false; // no more directories
}
// Found an unchecked subdirectory/ies, try opening one
while (!PushDir(_fdir.Current()) && !_fdir.AtEnd())
_fdir.Next();
}
Path::ConcatPaths(_curFile, _curDir, _ffile.Current());
return true; // success
}
bool FindFileRecursive::PushDir(const String &sub) {
if (_maxLevel != SIZE_MAX && (uint32_t)_fdirs.size() == _maxLevel)
return false; // no more nesting allowed
String path = Path::ConcatPaths(_fullDir, sub);
FindFile fdir = FindFile::OpenDirs(path);
FindFile ffile = FindFile::OpenFiles(path);
if (ffile.AtEnd() && fdir.AtEnd())
return false; // dir is empty, or error
_fdirs.push(std::move(_fdir)); // save previous dir iterator
_fdir = std::move(fdir);
_ffile = std::move(ffile);
_fullDir = path;
_curDir = Path::ConcatPaths(_curDir, sub);
return true;
}
bool FindFileRecursive::PopDir() {
if (_fdirs.empty())
return false; // no more parent levels
// restore parent level
_fdir = std::move(_fdirs.top());
_fdirs.pop();
_fullDir = Path::GetParent(_fullDir);
_curDir = Path::GetParent(_curDir);
if (_curDir.Compare(".") == 0)
_curDir = ""; // hotfix for GetParent returning "."
// advance dir iterator that we just recovered
_fdir.Next();
return true;
}
} // namespace Shared
} // namespace AGS
} // namespace AGS3
|