1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 //=============================================================================
24 //
25 // Platform-independent Directory functions
26 //
27 //=============================================================================
28 
29 #ifndef AGS_SHARED_UTIL_DIRECTORY_H
30 #define AGS_SHARED_UTIL_DIRECTORY_H
31 
32 #include "common/fs.h"
33 #include "common/stack.h"
34 #include "ags/lib/std/memory.h"
35 #include "ags/shared/core/platform.h"
36 #include "ags/shared/util/string.h"
37 
38 namespace AGS3 {
39 namespace AGS {
40 namespace Shared {
41 
42 extern const char *SAVE_FOLDER_PREFIX;
43 
44 namespace Directory {
45 
46 // Creates new directory (if it does not exist)
47 bool   CreateDirectory(const String &path);
48 // Makes sure all the sub-directories in the path are created. Parent path is
49 // not touched, and function must fail if parent path is not accessible.
50 bool   CreateAllDirectories(const String &parent, const String &sub_dirs);
51 // Sets current working directory, returns the resulting path
52 String SetCurrentDirectory(const String &path);
53 // Gets current working directory
54 String GetCurrentDirectory();
55 
56 // Get list of subdirs found in the given directory
57 bool   GetDirs(const String &dir_path, std::vector<String> &dirs);
58 // Get list of files found in the given directory
59 bool   GetFiles(const String &dir_path, std::vector<String> &files);
60 
61 } // namespace Directory
62 
63 class FindFile {
64 private:
65 	Common::FSNode _folder;
66 	Common::FSList _files;
67 	int _index = 0;
68 
69 private:
70 	static FindFile Open(const String &path, const String &wildcard,
71 		bool do_file, bool do_dir);
72 
73 public:
FindFile()74 	FindFile() {}
75 	~FindFile();
76 	static FindFile OpenFiles(const String &path, const String &wildcard = "*") {
77 		return Open(path, wildcard, true, false);
78 	}
79 	static FindFile OpenDirs(const String &path, const String &wildcard = "*") {
80 		return Open(path, wildcard, false, true);
81 	}
AtEnd()82 	bool AtEnd() const {
83 		return (_index >= (int)_files.size());
84 	}
Current()85 	String Current() const {
86 		return AtEnd() ? String() : String(_files[_index].getName().c_str());
87 	}
88 	void Close();
89 	bool Next();
90 };
91 
92 class FindFileRecursive {
93 public:
FindFileRecursive()94 	FindFileRecursive() {}
95 	~FindFileRecursive();
96 
97 	static FindFileRecursive Open(const String &path, const String &wildcard = "*",
98 		size_t max_level = -1);
99 	// TODO: directory mode, like in FindFile
AtEnd()100 	bool AtEnd() const {
101 		return _ffile.AtEnd();
102 	}
Current()103 	String Current() const {
104 		return _curFile;
105 	}
106 	void Close();
107 	bool Next();
108 
109 private:
110 	bool PushDir(const String &sub);
111 	bool PopDir();
112 
113 	Common::Stack<FindFile> _fdirs;
114 	FindFile _fdir; // current find dir iterator
115 	FindFile _ffile; // current find file iterator
116 	int _maxLevel = -1; // max nesting level, -1 for unrestricted
117 	String _fullDir; // full directory path
118 	String _curDir; // current dir path, relative to the base path
119 	String _curFile; // current file path with parent dirs
120 };
121 
122 } // namespace Shared
123 } // namespace AGS
124 } // namespace AGS3
125 
126 #endif
127