Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit b55a3c7

Browse files
author
年迈的老秋风Windy
committed
Add List Dir Functionality
1 parent aac0e31 commit b55a3c7

7 files changed

Lines changed: 123 additions & 2 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <EasyCrossPlatform.h>
2+
#include <iostream>
3+
int main(int argc, char** args) {
4+
std::string APPPath = EasyCrossPlatform::Runtime::Path::APPPath();
5+
std::string CWDPath = EasyCrossPlatform::Runtime::Path::CWDPath();
6+
std::vector<std::string> PathFileList = EasyCrossPlatform::File::FileInfo::FileInfoCls::getDirectoryFileList(CWDPath);
7+
std::cout << "Path: " << CWDPath << std::endl;
8+
std::cout << "----------------------------------------" << std::endl;
9+
for (std::string i : PathFileList) {
10+
std::cout << i << std::endl;
11+
}
12+
std::cout << "----------------------------------------" << std::endl;
13+
system("pause");
14+
}

include/XSYDAPPRuntime.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
#define __EasyCrossPlatform_APPRuntimeFile__
33
#include <EasyCP_Common.h>
44
#include <whereami/whereami.h>
5+
#ifdef EASYCROSSPLATFORM_PLATFORM_WINDOWS
6+
#include <direct.h>
7+
#else //if defined(EASYCROSSPLATFORM_PLATFORM_UNIX)
8+
#include <unistd.h>
9+
#endif
510
namespace EasyCrossPlatform {
611
namespace Runtime {
712
namespace Path {
13+
const constexpr unsigned int PathMaxSize = 1024U;
814
std::string APPPath(); //Returns the directory that the program is in, ending with "\\" or "/"
915
std::string APPExeName();
16+
std::string CWDPath();
1017
}
1118
}
1219
}

include/XSYDFileInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
//#include <filewatcher/FileWatch.hpp> //Add FileWatcher Functionality
77
#ifdef EASYCROSSPLATFORM_PLATFORM_WINDOWS
88
#include <Windows.h>
9+
#include <direct.h>
10+
#include <io.h>
911
#else //if defined(EASYCROSSPLATFORM_PLATFORM_UNIX)
1012
#include <sys/stat.h>
1113
#include <fcntl.h>
1214
#include <stdio.h>
1315
#include <time.h>
16+
#include <dirent.h>
1417
#include <unistd.h>
1518
#endif
1619
namespace EasyCrossPlatform {
@@ -47,6 +50,7 @@
4750
#endif
4851
public:
4952
static FileInfos readFileInfo(const std::string& Path);
53+
static std::vector<std::string> getDirectoryFileList(const std::string& DirectoryPath);
5054
};
5155
}
5256
}

src/XSYDAPPRuntime.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
std::string EasyCrossPlatform::Runtime::Path::APPPath()
44
{
55
int ExePathLength = wai_getExecutablePath(NULL, 0, NULL);
6-
char* myPath = new char[ExePathLength+1];
6+
char* myPath = new char[ExePathLength + 1];
77
int DirPathLength = 0;
88
wai_getExecutablePath(myPath, ExePathLength, &DirPathLength);
99
myPath[ExePathLength] = '\0';
10-
std::string mPath = std::string(myPath, DirPathLength+1);
10+
std::string mPath = std::string(myPath, DirPathLength);
11+
if (mPath[mPath.length() - 1U] == '\\' || mPath[mPath.length() - 1U] == '/') {
12+
//Directory Separator Ended.
13+
}
14+
else {
15+
#ifdef EASYCROSSPLATFORM_PLATFORM_WINDOWS
16+
mPath += "\\";
17+
#else //if EASYCROSSPLATFORM_PLATFORM_UNIX
18+
mPath += "/";
19+
#endif
20+
}
1121
delete[] myPath;
1222
return mPath;
1323
}
@@ -23,3 +33,27 @@ std::string EasyCrossPlatform::Runtime::Path::APPExeName()
2333
delete[] myPath;
2434
return mPath;
2535
}
36+
37+
std::string EasyCrossPlatform::Runtime::Path::CWDPath()
38+
{
39+
char buffer[Path::PathMaxSize];
40+
#ifdef EASYCROSSPLATFORM_PLATFORM_WINDOWS
41+
if (_getcwd(buffer, Path::PathMaxSize) == NULL) {
42+
#else //if EASYCROSSPLATFORM_PLATFORM_UNIX
43+
if (getcwd(buffer, Path::PathMaxSize) == NULL) {
44+
#endif
45+
throw std::runtime_error("Encountered an error while getting the current working directory.");
46+
}
47+
std::string mResult = std::string(buffer);
48+
if (mResult[mResult.length() - 1U] == '\\' || mResult[mResult.length() - 1U] == '/') {
49+
//Directory Separator Ended
50+
}
51+
else {
52+
#ifdef EASYCROSSPLATFORM_PLATFORM_WINDOWS
53+
mResult += "\\";
54+
#else //if EASYCROSSPLATFORM_PLATFORM_UNIX
55+
mResult += "/";
56+
#endif
57+
}
58+
return mResult;
59+
}

src/XSYDFileInfo.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,68 @@ EasyCrossPlatform::File::FileInfo::FileInfos EasyCrossPlatform::File::FileInfo::
101101
return mResult;
102102
}
103103

104+
std::vector<std::string> EasyCrossPlatform::File::FileInfo::FileInfoCls::getDirectoryFileList(const std::string& DirectoryPath)
105+
{
106+
std::vector<std::string> files;//存放文件名
107+
std::string NSDirPath = DirectoryPath;
108+
if (NSDirPath[NSDirPath.length() - 1U] != '/' && NSDirPath[NSDirPath.length() - 1U] != '\\') {
109+
NSDirPath += File::FolderSeparator;
110+
}
111+
112+
#ifdef WIN32
113+
NSDirPath += "*";
114+
_finddata_t file;
115+
long lf;
116+
//输入文件夹路径
117+
if ((lf = _findfirst(NSDirPath.c_str(), &file)) == -1) {
118+
throw std::runtime_error("Directory not found");
119+
}
120+
else {
121+
while (_findnext(lf, &file) == 0) {
122+
//输出文件名
123+
//cout<<file.name<<endl;
124+
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
125+
continue;
126+
files.push_back(file.name);
127+
}
128+
}
129+
_findclose(lf);
130+
#endif
131+
132+
#ifdef linux
133+
DIR *dir;
134+
struct dirent *ptr;
135+
//char base[1000];
136+
137+
if ((dir = opendir(NSDirPath.c_str())) == NULL)
138+
{
139+
throw std::runtime_error("Directory not found");
140+
}
141+
142+
while ((ptr = readdir(dir)) != NULL)
143+
{
144+
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
145+
continue;
146+
else if(ptr->d_type == DT_REG || ptr->d_type == DT_DIR || ptr->d_type == DT_SOCK || ptr->d_type == DT_LNK || ptr->d_type == DT_UNKNOWN)
147+
{
148+
files.push_back(ptr->d_name);
149+
/*
150+
memset(base,'\0',sizeof(base));
151+
strcpy(base,basePath);
152+
strcat(base,"/");
153+
strcat(base,ptr->d_nSame);
154+
readFileList(base);
155+
*/
156+
}
157+
}
158+
closedir(dir);
159+
#endif
160+
161+
//排序,按从小到大排序
162+
std::sort(files.begin(), files.end());
163+
return files;
164+
}
165+
104166
void EasyCrossPlatform::File::FileInfo::CleanFileTypesVar(FileTypes & VarToDeal)
105167
{
106168
VarToDeal.disabled = false;

0 commit comments

Comments
 (0)