Skip to content

Commit 0a1e273

Browse files
[System::IO] add GetFile Support for windows
1 parent b89931c commit 0a1e273

12 files changed

+148
-90
lines changed

src/framework/OS.h

+12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,34 @@
33

44
#ifdef _WIN32
55

6+
#include <windows.h>
67
#include <direct.h>
78
#define PATH_SEPARATOR "\\"
89
#define SUPPORTS_NULLPTR
910

1011
#elif defined(_APPLE_)
1112

1213
#include "unistd.h"
14+
#include <dirent.h>
1315
#define SUPPORTS_NULLPTR
1416
#define PATH_SEPARATOR "/"
17+
#define SUPPORT_DIRENT
1518

1619
#else
1720

1821
#include "unistd.h"
22+
#include <dirent.h>
1923
#include "NullPtr.h"
2024
#define PATH_SEPARATOR "/"
25+
#define SUPPORT_DIRENT
2126

2227
#endif
2328

29+
// for when C++11 is supported
30+
#ifdef SUPPORTS_CXX_11
31+
#define OVERRIDE override
32+
#else
33+
#define OVERRIDE
34+
#endif
35+
2436
#include <stdint.h>

src/framework/System/Console.h

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ namespace System
3232
{
3333
WriteLine(str.str().c_str());
3434
}
35+
36+
/// function to support {0} arguments passing
37+
static void WriteLine(const System::String& str,const Object &a)
38+
{
39+
String s;
40+
s = s.Format(str,a);
41+
WriteLine(s.str().c_str());
42+
}
43+
44+
/// function with const char * format
45+
static void WriteLine(const char *str,const Object &a)
46+
{
47+
String s;
48+
s = s.Format(str,a);
49+
WriteLine(s.str().c_str());
50+
}
3551

3652
/// write function which does not print a new line
3753
static void Write(const char* str)

src/framework/System/IO/Directory.cxx

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11

22
#include "Directory.h"
33

4-
#include <dirent.h>
54

65
Array<System::IO::FileInfo> System::IO::DirectoryInfo::GetFiles()
76
{
87
List<System::IO::FileInfo> files;
98

109
String currentPath = FullName();
11-
10+
11+
#ifdef SUPPORT_DIRENT
1212
DIR *dir;
1313
struct dirent *ent;
14-
if ((dir = opendir (currentPath.str().c_str())) != nullptr) {
14+
if ((dir = opendir (currentPath.c_str())) != nullptr) {
1515
while ((ent = readdir (dir)) != nullptr) {
1616
// we need to be careful here because of the way
1717
// we have FileInfo contrsutor working
1818
System::IO::FileInfo file(ent->d_name);
1919
files.Add(file);
2020
}
2121
closedir (dir);
22-
}
22+
}
23+
#else
24+
WIN32_FIND_DATA ffd;
25+
TCHAR szDir[MAX_PATH];
26+
HANDLE hFind = INVALID_HANDLE_VALUE;
27+
hFind = FindFirstFile(currentPath.c_str(), &ffd);
28+
29+
do
30+
{
31+
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
32+
{
33+
// is a directory ffd.cFileName;
34+
}
35+
else
36+
{
37+
// ffd.cFileName
38+
System::IO::FileInfo file(String(ffd.cFileName));
39+
files.Add(file);
40+
}
41+
}
42+
while (FindNextFile(hFind, &ffd) != 0);
43+
44+
#endif
2345
return files.ToArray();
2446
}

src/framework/System/IO/Directory.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
#include "System/Array.h"
99
#include "System/Collections/Generic.h"
1010

11-
#include <dirent.h>
1211

1312
namespace System
1413
{
1514
namespace IO
1615
{
1716
class FileInfo;
1817

18+
/// the directory class
1919
class Directory
2020
{
2121
public:
@@ -26,23 +26,28 @@ namespace System
2626
S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
2727
}
2828

29+
/// delete a named directory
2930
static void Delete(const System::String& file)
3031
{
3132
rmdir(file.str().c_str());
3233
}
3334

3435
};
3536

37+
/// directory info class
3638
class DirectoryInfo : public FileSystemInfo
3739
{
3840
public:
41+
/// default constructor
3942
DirectoryInfo()
4043
{}
4144

45+
/// constructor
4246
DirectoryInfo(const System::String & dir):
4347
FileSystemInfo(dir)
4448
{}
4549

50+
// get the files in the directory
4651
Array<System::IO::FileInfo> GetFiles();
4752
};
4853

src/framework/System/IO/FileInfo.cxx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "FileInfo.h"
22

33
FileInfo::FileInfo(const System::String& s)
4-
: m_path(s)
4+
: FileSystemInfo(s)
55
{
6-
String cwd = Environment::CurrentDirectory();
7-
m_path = System::Path::Combine(cwd, s);
86
}

src/framework/System/IO/FileInfo.h

+2-76
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
#include "Path.h"
99
#include "System/DateTime.h"
1010
#include "OS.h"
11+
#include "System/IO/FileSystemInfo.h"
1112

1213
namespace System
1314
{
1415
namespace IO
1516
{
1617
/// A file information class
17-
class FileInfo
18+
class FileInfo : public FileSystemInfo
1819
{
19-
System::String m_path;
20-
2120
public:
2221
/// constructor
2322
FileInfo()
@@ -26,79 +25,6 @@ namespace System
2625

2726
/// constructor within the current directory
2827
FileInfo(const System::String& s);
29-
30-
/// the full name of the file
31-
System::String FullName() const
32-
{
33-
return m_path;
34-
}
35-
36-
/// does the file exist
37-
bool Exists() const
38-
{
39-
struct stat statStruct;
40-
bool fileExists =
41-
(stat(m_path.str().c_str(), &statStruct) == 0);
42-
return fileExists;
43-
}
44-
45-
/// the files extension
46-
String Extension() const
47-
{
48-
int dotPos = m_path.LastIndexOf('.');
49-
if (dotPos == -1) {
50-
return String("");
51-
}
52-
// Extension include the "."
53-
String ext = m_path.Substring(dotPos);
54-
return ext;
55-
}
56-
57-
/// the name of the file
58-
String Name() const
59-
{
60-
// from the end of the path back up to the last / or \\ to strip
61-
// to the filename
62-
int lastForwardSlash = m_path.LastIndexOf('/');
63-
int lastBackwardSlash = m_path.LastIndexOf('\\');
64-
65-
if (lastForwardSlash == -1 && lastBackwardSlash == -1) {
66-
return m_path;
67-
}
68-
69-
if (lastForwardSlash == -1) {
70-
String part = m_path.Substring(lastBackwardSlash + 1);
71-
return part;
72-
}
73-
if (lastBackwardSlash == -1) {
74-
String part = m_path.Substring(lastForwardSlash + 1);
75-
return part;
76-
}
77-
78-
String part = m_path.Substring(
79-
Math::Max(lastForwardSlash, lastBackwardSlash) + 1);
80-
return part;
81-
}
82-
83-
/// delete the file
84-
virtual void Delete()
85-
{
86-
int errVal = std::remove(FullName().str().c_str());
87-
if (errVal) {
88-
// an error occurred TODO perhaps it should throw if not
89-
// found)
90-
// throw if a directory
91-
static_cast<void>(errVal);
92-
}
93-
return;
94-
}
95-
96-
/// get the creation date of the file
97-
DateTime CreationTime()
98-
{
99-
throw System::NotImplementedException(
100-
"FileInfo::Creation time not implemented");
101-
}
10228
};
10329
}
10430
}

src/framework/System/IO/FileSystemInfo.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace System
1414
namespace IO
1515
{
1616
/// A file information class
17-
class FileSystemInfo
17+
class FileSystemInfo : public Object
1818
{
1919
System::String m_path;
2020

@@ -23,6 +23,11 @@ namespace System
2323
FileSystemInfo()
2424
{
2525
}
26+
27+
virtual String ToString() const OVERRIDE
28+
{
29+
return FullName();
30+
}
2631

2732
/// constructor within the current directory
2833
FileSystemInfo(const System::String& s);

src/framework/System/IO/StreamReader.h

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ namespace System
4646
System::String strOut = sIn.Replace("\r", "");
4747
return strOut;
4848
}
49+
50+
/// read the file all the way to the end of file
51+
System::String ReadToEnd()
52+
{
53+
String input;
54+
while (!EndOfStream()) {
55+
String s = ReadLine();
56+
input += s;
57+
input += "\n";
58+
}
59+
return input;
60+
}
4961

5062
/// dispose method for closing the file
5163
void Dispose()

src/framework/System/IO/StreamWriter.h

+18
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ namespace System
4949
{
5050
(*m_fd) << s.str() << "\n";
5151
}
52+
53+
/// write a line to the file with a {N} parameter files
54+
/// TODO handle more than 1 parameter
55+
void WriteLine(const System::String& str,const Object &a)
56+
{
57+
String s;
58+
s=s.Format(str,a);
59+
WriteLine(s);
60+
}
61+
62+
/// write a line to the file with a {N} parameter files
63+
/// TODO handle more than 1 parameter
64+
void WriteLine(const char *format,const Object &a)
65+
{
66+
String s;
67+
s=s.Format(String(format),a);
68+
WriteLine(s);
69+
}
5270

5371
/// call the dispose method
5472
void Dispose()

src/framework/System/String.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,40 @@ namespace System
3434
{
3535
}
3636

37-
/// constructor from std::string (NOT .NET API)
37+
/// constructor from std::string
38+
/// (NOT .NET API)
3839
String(const std::string& strIn)
3940
: m_str(strIn)
4041
{
4142
}
4243

43-
/// get the std::string from the String (NOT .NET call)
44+
/// get the std::string from the String
45+
/// (NOT .NET call)
4446
std::string str() const
4547
{
4648
return m_str;
4749
}
4850

49-
/// an std::string casting converter (NOT .NET call)
51+
/// an std::string casting converter
52+
/// (NOT .NET call)
5053
const std::string operator()(std::string&) const
5154
{
5255
return str();
5356
}
5457

55-
/// an const char* casting converter (NOT .NET call)
58+
/// an const char* casting converter
59+
/// (NOT .NET call)
5660
const char* operator()(const char*) const
5761
{
5862
return str().c_str();
5963
}
64+
65+
/// prevent the need to go via stl::string
66+
/// (NOT .NET call)
67+
const char * c_str() const
68+
{
69+
return str().c_str();
70+
}
6071

6172
/// a function to determine if the string is empty (NOT .NET call)
6273
bool Empty() const
@@ -286,7 +297,7 @@ namespace System
286297
}
287298

288299
/// turn the string into a string
289-
virtual String ToString() const
300+
virtual String ToString() const OVERRIDE
290301
{
291302
return (*this);
292303
}

src/unittests/Directory_Test.cxx

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ TEST(DirectoryTest, BasicFileOperations)
1717
// I assume this is "." and ".."
1818
EXPECT_EQ(2,files.Length());
1919

20+
System::Console::WriteLine("First File [{0}]",files[0]);
21+
System::Console::WriteLine("Second File [{0}]",files[1]);
22+
2023
Directory::Delete("ABC");
2124
EXPECT_FALSE(info.Exists());
2225

0 commit comments

Comments
 (0)