Daniele Bartolini 12 rokov pred
rodič
commit
cf6466490c
2 zmenil súbory, kde vykonal 43 pridanie a 0 odobranie
  1. 8 0
      engine/os/OS.h
  2. 35 0
      engine/os/linux/LinuxOS.cpp

+ 8 - 0
engine/os/OS.h

@@ -30,6 +30,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Config.h"
 #include "Types.h"
+#include "Vector.h"
+#include "DynamicString.h"
 
 namespace crown
 {
@@ -82,6 +84,12 @@ bool			unlink(const char* path);		//! Deletes a regular file. Returns true if su
 bool			mkdir(const char* path);		//! Creates a directory. Returns true if success, false if not
 bool			rmdir(const char* path);		//! Deletes a directory. Returns true if success, false if not
 
+/// Returns the list of @a files in the given @a dir directory. Optionally walks into
+/// subdirectories whether @a recursive is true.
+/// @note
+/// Does not follow symbolic links.
+void			list_files(const char* path, bool recursive, Vector<DynamicString>& files);
+
 //-----------------------------------------------------------------------------
 // OS ambient variables
 //-----------------------------------------------------------------------------

+ 35 - 0
engine/os/linux/LinuxOS.cpp

@@ -39,6 +39,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "OS.h"
 #include "StringUtils.h"
+#include "TempAllocator.h"
 
 namespace crown
 {
@@ -178,6 +179,40 @@ bool rmdir(const char* path)
 	return (::rmdir(path) == 0);
 }
 
+//-----------------------------------------------------------------------------
+void list_files(const char* path, bool recursive, Vector<DynamicString>& files)
+{
+	DIR *dir;
+	struct dirent *entry;
+
+	if (!(dir = opendir(path)))
+	{
+		return;
+	}
+
+	while ((entry = readdir(dir)))
+	{
+		if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
+		{
+			continue;
+		}
+
+		DynamicString filename(default_allocator());
+
+		filename += path;
+		filename += PATH_SEPARATOR;
+		filename += entry->d_name;
+		files.push_back(filename);
+
+		if (entry->d_type == DT_DIR && recursive)
+		{
+			list_files(filename.c_str(), recursive, files);
+		}
+	}
+
+	closedir(dir);
+}
+
 //-----------------------------------------------------------------------------
 const char* get_cwd()
 {