Skip to content

Commit b89931c

Browse files
[System::IO] new GetFiles() function
1 parent 93356fa commit b89931c

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

src/framework/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SET(SOURCE
99
System/IO/FileSystemInfo.cxx
1010
System/IO/StreamReader.cxx
1111
System/IO/File.cxx
12+
System/IO/Directory.cxx
1213
)
1314

1415
SET(HEADERS
@@ -20,6 +21,8 @@ SET(HEADERS
2021
System/StringBuilder.h
2122
System/IO.h
2223
System/IO/FileInfo.h
24+
System/IO/FileSystemInfo.h
25+
System/IO/Directory.h
2326
System/IO/StreamReader.h
2427
System/IO/StreamWriter.h
2528
System.h

src/framework/System/Collections/Generic/List.h

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ namespace System
5757
{
5858
m_vec.clear();
5959
}
60+
61+
Array<T> ToArray()
62+
{
63+
Array<T> array;
64+
65+
for(size_t i=0;i<m_vec.size();i++){
66+
array.Add((*this)[i]);
67+
}
68+
69+
return array;
70+
}
6071
};
6172
}
6273
}

src/framework/System/IO/Directory.cxx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "Directory.h"
3+
4+
#include <dirent.h>
5+
6+
Array<System::IO::FileInfo> System::IO::DirectoryInfo::GetFiles()
7+
{
8+
List<System::IO::FileInfo> files;
9+
10+
String currentPath = FullName();
11+
12+
DIR *dir;
13+
struct dirent *ent;
14+
if ((dir = opendir (currentPath.str().c_str())) != nullptr) {
15+
while ((ent = readdir (dir)) != nullptr) {
16+
// we need to be careful here because of the way
17+
// we have FileInfo contrsutor working
18+
System::IO::FileInfo file(ent->d_name);
19+
files.Add(file);
20+
}
21+
closedir (dir);
22+
}
23+
return files.ToArray();
24+
}

src/framework/System/IO/Directory.h

+14
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
#include "System/String.h"
55
#include "System/IO.h"
66
#include "System/IO/FileSystemInfo.h"
7+
#include "System/IO/FileInfo.h"
8+
#include "System/Array.h"
9+
#include "System/Collections/Generic.h"
10+
11+
#include <dirent.h>
712

813
namespace System
914
{
15+
namespace IO
16+
{
17+
class FileInfo;
18+
1019
class Directory
1120
{
1221
public:
@@ -33,9 +42,14 @@ namespace System
3342
DirectoryInfo(const System::String & dir):
3443
FileSystemInfo(dir)
3544
{}
45+
46+
Array<System::IO::FileInfo> GetFiles();
3647
};
48+
49+
}
3750
}
3851

3952
using namespace System;
53+
using namespace System::IO;
4054

4155
#endif

src/unittests/Directory_Test.cxx

+7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ TEST(DirectoryTest, BasicFileOperations)
1111

1212
DirectoryInfo info("ABC");
1313
EXPECT_TRUE(info.Exists());
14+
15+
Array<FileInfo> files = info.GetFiles();
16+
17+
// I assume this is "." and ".."
18+
EXPECT_EQ(2,files.Length());
19+
1420
Directory::Delete("ABC");
1521
EXPECT_FALSE(info.Exists());
1622

1723
EXPECT_EQ(info.Name(),"ABC");
24+
1825
}

0 commit comments

Comments
 (0)