Merge remote-tracking branch 'origin/GP-0_SeparatorFix'

This commit is contained in:
ghidra1 2024-05-20 15:12:14 -04:00
commit 75a86e51c9
2 changed files with 29 additions and 9 deletions

View file

@ -34,8 +34,10 @@ namespace ghidra {
// Path name separator // Path name separator
#ifdef _WINDOWS #ifdef _WINDOWS
char FileManage::separator = '\\'; char FileManage::separator = '\\';
char FileManage::separatorClass[] = "/\\";
#else #else
char FileManage::separator = '/'; char FileManage::separator = '/';
char FileManage::separatorClass[] = "/";
#endif #endif
void FileManage::addDir2Path(const string &path) void FileManage::addDir2Path(const string &path)
@ -43,7 +45,7 @@ void FileManage::addDir2Path(const string &path)
{ {
if (path.size()>0) { if (path.size()>0) {
pathlist.push_back(path); pathlist.push_back(path);
if (path[path.size()-1] != separator) if (!isSeparator(path[path.size()-1]))
pathlist.back() += separator; pathlist.back() += separator;
} }
} }
@ -53,7 +55,7 @@ void FileManage::findFile(string &res,const string &name) const
{ // Search through paths to find file with given name { // Search through paths to find file with given name
vector<string>::const_iterator iter; vector<string>::const_iterator iter;
if (name[0] == separator) { if (isSeparator(name[0])) {
res = name; res = name;
ifstream s(res.c_str()); ifstream s(res.c_str());
if (s) { if (s) {
@ -122,6 +124,22 @@ bool FileManage::isDirectory(const string &path)
#endif #endif
#ifdef _WINDOWS
bool FileManage::isSeparator(char c)
{
return (c == '/' || c == '\\');
}
#else
bool FileManage::isSeparator(char c)
{
return c == separator;
}
#endif
#ifdef _WINDOWS #ifdef _WINDOWS
void FileManage::matchListDir(vector<string> &res,const string &match,bool isSuffix,const string &dirname,bool allowdot) void FileManage::matchListDir(vector<string> &res,const string &match,bool isSuffix,const string &dirname,bool allowdot)
@ -131,7 +149,7 @@ void FileManage::matchListDir(vector<string> &res,const string &match,bool isSuf
string dirfinal; string dirfinal;
dirfinal = dirname; dirfinal = dirname;
if (dirfinal[dirfinal.size()-1] != separator) if (!isSeparator(dirfinal[dirfinal.size()-1]))
dirfinal += separator; dirfinal += separator;
string regex = dirfinal + '*'; string regex = dirfinal + '*';
@ -162,7 +180,7 @@ void FileManage::matchListDir(vector<string> &res,const string &match,bool isSuf
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
string dirfinal = dirname; string dirfinal = dirname;
if (dirfinal[dirfinal.size()-1] != separator) if (!isSeparator(dirfinal[dirfinal.size()-1]))
dirfinal += separator; dirfinal += separator;
dir = opendir(dirfinal.c_str()); dir = opendir(dirfinal.c_str());
@ -205,7 +223,7 @@ void FileManage::directoryList(vector<string> &res,const string &dirname,bool al
WIN32_FIND_DATAA FindFileData; WIN32_FIND_DATAA FindFileData;
HANDLE hFind; HANDLE hFind;
string dirfinal = dirname; string dirfinal = dirname;
if (dirfinal[dirfinal.size()-1] != separator) if (!isSeparator(dirfinal[dirfinal.size()-1]))
dirfinal += separator; dirfinal += separator;
string regex = dirfinal + "*"; string regex = dirfinal + "*";
const char *s = regex.c_str(); const char *s = regex.c_str();
@ -232,7 +250,7 @@ void FileManage::directoryList(vector<string> &res,const string &dirname,bool al
string dirfinal; string dirfinal;
dirfinal = dirname; dirfinal = dirname;
if (dirfinal[dirfinal.size()-1] != separator) if (!isSeparator(dirfinal[dirfinal.size()-1]))
dirfinal += separator; dirfinal += separator;
dir = opendir(dirfinal.c_str()); dir = opendir(dirfinal.c_str());
@ -262,7 +280,7 @@ void FileManage::scanDirectoryRecursive(vector<string> &res,const string &matchn
vector<string>::const_iterator iter; vector<string>::const_iterator iter;
for(iter = subdir.begin();iter!=subdir.end();++iter) { for(iter = subdir.begin();iter!=subdir.end();++iter) {
const string &curpath( *iter ); const string &curpath( *iter );
string::size_type pos = curpath.rfind(separator); string::size_type pos = curpath.find_last_of(separatorClass);
if (pos == string::npos) if (pos == string::npos)
pos = 0; pos = 0;
else else
@ -280,9 +298,9 @@ void FileManage::splitPath(const string &full,string &path,string &base)
// If there is no path, i.e. only a basename in full, then -path- will return as an empty string // If there is no path, i.e. only a basename in full, then -path- will return as an empty string
// otherwise -path- will be non-empty and end in a separator character // otherwise -path- will be non-empty and end in a separator character
string::size_type end = full.size()-1; string::size_type end = full.size()-1;
if (full[full.size()-1] == separator) // Take into account terminating separator if (isSeparator(full[full.size()-1])) // Take into account terminating separator
end = full.size()-2; end = full.size()-2;
string::size_type pos = full.rfind(separator,end); string::size_type pos = full.find_last_of(separatorClass,end);
if (pos == string::npos) { // Didn't find any separator if (pos == string::npos) { // Didn't find any separator
base = full; base = full;
path.clear(); path.clear();

View file

@ -34,6 +34,7 @@ using std::ostringstream;
class FileManage { class FileManage {
vector<string> pathlist; // List of paths to search for files vector<string> pathlist; // List of paths to search for files
static char separator; static char separator;
static char separatorClass[]; // Characters that can be accepted as a separator
static string buildPath(const vector<string> &pathels,int level); static string buildPath(const vector<string> &pathels,int level);
static bool testDevelopmentPath(const vector<string> &pathels,int level,string &root); static bool testDevelopmentPath(const vector<string> &pathels,int level,string &root);
static bool testInstallPath(const vector<string> &pathels,int level,string &root); static bool testInstallPath(const vector<string> &pathels,int level,string &root);
@ -42,6 +43,7 @@ public:
void addCurrentDir(void); void addCurrentDir(void);
void findFile(string &res,const string &name) const; // Resolve full pathname void findFile(string &res,const string &name) const; // Resolve full pathname
void matchList(vector<string> &res,const string &match,bool isSuffix) const; // List of files with suffix void matchList(vector<string> &res,const string &match,bool isSuffix) const; // List of files with suffix
static bool isSeparator(char c);
static bool isDirectory(const string &path); static bool isDirectory(const string &path);
static void matchListDir(vector<string> &res,const string &match,bool isSuffix,const string &dir,bool allowdot); static void matchListDir(vector<string> &res,const string &match,bool isSuffix,const string &dir,bool allowdot);
static void directoryList(vector<string> &res,const string &dirname,bool allowdot=false); static void directoryList(vector<string> &res,const string &dirname,bool allowdot=false);