version 5.0.0_beta

integrate PIRegularExpression into PIString and PIDir
add piliterals_regularexpression.h for ""_regex and ""_glob literals
This commit is contained in:
2025-08-13 18:48:01 +03:00
parent 7a6936ccd9
commit b6c5d65a8d
9 changed files with 158 additions and 57 deletions

View File

@@ -298,9 +298,9 @@ bool sort_compare(const PIFile::FileInfo & v0, const PIFile::FileInfo & v1) {
//! также "." и "..". Возвращаются абсолютные пути.
//! \attention Этот метод не читает содержимое директорий
//! рекурсивно!
PIVector<PIFile::FileInfo> PIDir::entries() {
PIVector<PIFile::FileInfo> l;
if (!isExists()) return l;
PIVector<PIFile::FileInfo> PIDir::entries(const PIRegularExpression & regexp) {
if (!isExists()) return {};
PIVector<PIFile::FileInfo> ret;
PIString dp = absolutePath();
PIString p(dp);
if (dp == ".")
@@ -309,9 +309,9 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
dp += separator;
// piCout << "start entries from" << p;
#ifdef WINDOWS
PIFile::FileInfo fi;
if (dp == separator) {
char letters[1024];
PIFile::FileInfo fi;
DWORD ll = GetLogicalDriveStringsA(1023, letters);
PIString clet;
for (DWORD i = 0; i < ll; ++i) {
@@ -319,7 +319,12 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
clet.resize(2);
fi.path = clet;
fi.flags = PIFile::FileInfo::Dir;
l << fi;
if (regexp.isValid()) {
if (regexp.match(fi.name())) {
ret << fi;
}
} else
ret << fi;
clet.clear();
} else
clet += PIChar(letters[i]);
@@ -329,21 +334,27 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
piZeroMemory(fd);
p += "\\*";
void * hf = FindFirstFileA((LPCSTR)(p.data()), &fd);
if (!hf) return l;
if (!hf) return ret;
bool hdd = false;
do {
PIString fn(fd.cFileName);
if (fn == "..") hdd = true;
l << PIFile::fileInfo(dp + fn);
fi = PIFile::fileInfo(dp + fn);
if (regexp.isValid()) {
if (regexp.match(fi.name())) {
ret << fi;
}
} else
ret << fi;
piZeroMemory(fd);
} while (FindNextFileA(hf, &fd) != 0);
FindClose(hf);
l.sort(sort_compare);
ret.sort(sort_compare);
if (!hdd) {
PIFile::FileInfo fi;
fi.path = "..";
fi.flags = PIFile::FileInfo::Dir | PIFile::FileInfo::DotDot;
l.push_front(fi);
ret.push_front(fi);
}
}
@@ -356,7 +367,7 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
for (;;) {
de = readdir(dir);
if (!de) break;
l << PIFile::fileInfo(dp + PIString(de->d_name));
ret << PIFile::fileInfo(dp + PIString(de->d_name));
}
closedir(dir);
}
@@ -371,14 +382,14 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
versionsort);
# endif
for (int i = 0; i < cnt; ++i) {
l << PIFile::fileInfo(dp + PIString(list[i]->d_name));
ret << PIFile::fileInfo(dp + PIString(list[i]->d_name));
free(list[i]);
}
free(list);
# endif
#endif
// piCout << "end entries from" << p;
return l;
return ret;
}
@@ -394,7 +405,7 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
//! одним списком, сортированным по алфавиту. Список не содержит
//! "." и "..". Возвращаются абсолютные пути, причём файлы
//! располагаются после директорий.
PIVector<PIFile::FileInfo> PIDir::allEntries() {
PIVector<PIFile::FileInfo> PIDir::allEntries(const PIRegularExpression & regexp) {
PIVector<PIFile::FileInfo> ret;
PIVector<PIFile::FileInfo> dirs;
PIStringList cdirs, ndirs;
@@ -409,8 +420,14 @@ PIVector<PIFile::FileInfo> PIDir::allEntries() {
if (de.isDir()) {
dirs << de;
ndirs << de.path;
} else
} else {
if (regexp.isValid()) {
if (!regexp.match(de.name())) {
continue;
}
}
ret << de;
}
}
}
cdirs = ndirs;
@@ -516,8 +533,8 @@ PIDir PIDir::temporary() {
}
PIVector<PIFile::FileInfo> PIDir::allEntries(const PIString & path) {
return PIDir(path).allEntries();
PIVector<PIFile::FileInfo> PIDir::allEntries(const PIString & path, const PIRegularExpression & regexp) {
return PIDir(path).allEntries(regexp);
}