code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,46 +1,47 @@
/*
PIP - Platform Independent Primitives
Directory
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
PIP - Platform Independent Primitives
Directory
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "piincludes_p.h"
#include "pidir.h"
#include "piincludes_p.h"
const PIChar PIDir::separator = '/';
#ifdef QNX
# define _stat_struct_ struct stat
# define _stat_call_ stat
# define _stat_link_ lstat
# define _stat_struct_ struct stat
# define _stat_call_ stat
# define _stat_link_ lstat
#else
# define _stat_struct_ struct stat64
# define _stat_call_ stat64
# define _stat_link_ lstat64
# define _stat_struct_ struct stat64
# define _stat_call_ stat64
# define _stat_link_ lstat64
#endif
#ifndef WINDOWS
# ifdef ANDROID
# include <dirent.h>
# include <dirent.h>
# else
# ifdef FREERTOS
# ifdef FREERTOS
extern "C" {
# include <sys/dirent.h>
# include <sys/dirent.h>
}
# else
# include <sys/dir.h>
# endif
# else
# include <sys/dir.h>
# endif
# endif
# include <sys/stat.h>
#endif
@@ -70,7 +71,7 @@ PIDir::PIDir(const PIFile & file) {
}
bool PIDir::operator ==(const PIDir & d) const {
bool PIDir::operator==(const PIDir & d) const {
return d.absolutePath() == absolutePath();
}
@@ -94,7 +95,7 @@ PIString PIDir::path() const {
return path_.mid(1);
} else
#endif
return path_;
return path_;
}
@@ -108,7 +109,7 @@ PIString PIDir::absolutePath() const {
return path_.mid(1);
} else
#endif
return path_;
return path_;
}
return PIDir(PIDir::current().path() + separator + path_).path();
}
@@ -131,7 +132,7 @@ PIDir & PIDir::cleanPath() {
}
PIString sep = PIString(separator);
path_.replaceAll(sep + sep, sep);
bool is_abs = isAbsolute();
bool is_abs = isAbsolute();
PIStringList l = PIString(p).split(separator);
l.removeAll(".");
l.removeAll("");
@@ -150,8 +151,7 @@ PIDir & PIDir::cleanPath() {
break;
}
path_ = l.join(separator);
if (is_abs)
path_.prepend(separator);
if (is_abs) path_.prepend(separator);
if (path_.isEmpty()) path_ = ".";
return *this;
}
@@ -160,7 +160,7 @@ PIDir & PIDir::cleanPath() {
PIString PIDir::relative(const PIString & path) const {
PIDir td(path);
PIStringList dl(absolutePath().split(separator)), pl(td.absolutePath().split(separator)), rl;
//piCout << pl << "rel to" << dl;
// piCout << pl << "rel to" << dl;
while (!dl.isEmpty() && !pl.isEmpty()) {
if (dl.front() != pl.front()) break;
dl.pop_front();
@@ -179,8 +179,7 @@ PIDir & PIDir::setDir(const PIString & path) {
#ifdef WINDOWS
path_.replaceAll("\\", separator);
if (path_.length() > 2)
if (path_.mid(1, 2).contains(":"))
path_.prepend(separator);
if (path_.mid(1, 2).contains(":")) path_.prepend(separator);
#endif
cleanPath();
return *this;
@@ -197,56 +196,54 @@ PIDir & PIDir::cd(const PIString & path) {
bool PIDir::make(bool withParents) {
PIDir d = cleanedPath();
//PIString tp;
// PIString tp;
#ifndef WINDOWS
bool is_abs = isAbsolute();
#endif
if (withParents) {
PIStringList l = d.path().split(separator);
//piCout << l;
// piCout << l;
l.removeAll("");
//piCout << l;
// piCout << l;
PIString cdp;
piForeachC (PIString & i, l) {
piForeachC(PIString & i, l) {
if (!cdp.isEmpty()
#ifndef WINDOWS
|| is_abs
|| is_abs
#endif
)
)
cdp += separator;
cdp += i;
//piCout << "dir" << cdp;
// piCout << "dir" << cdp;
if (!isExists(cdp))
if (!makeDir(cdp))
return false;
if (!makeDir(cdp)) return false;
}
/*for (int i = l.size_s() - 1; i >= 0; --i) {
if (i > 1) tp = PIStringList(l).remove(i, l.size_s() - i).join(separator);
else {
tp = separator;
if (!is_abs) tp.push_front('.');
}
piCout << "check" << tp;
if (isExists(tp)) {
for (int j = i + 1; j <= l.size_s(); ++j) {
tp = PIStringList(l).remove(j, l.size_s() - j).join(separator);
piCout << "make" << tp;
if (makeDir(tp)) continue;
else return false;
}
break;
};
if (i > 1) tp = PIStringList(l).remove(i, l.size_s() - i).join(separator);
else {
tp = separator;
if (!is_abs) tp.push_front('.');
}
piCout << "check" << tp;
if (isExists(tp)) {
for (int j = i + 1; j <= l.size_s(); ++j) {
tp = PIStringList(l).remove(j, l.size_s() - j).join(separator);
piCout << "make" << tp;
if (makeDir(tp)) continue;
else return false;
}
break;
};
}*/
return true;
} else
if (makeDir(d.path())) return true;
} else if (makeDir(d.path()))
return true;
return false;
}
bool PIDir::rename(const PIString & new_name) {
if (!PIDir::rename(path(), new_name))
return false;
if (!PIDir::rename(path(), new_name)) return false;
setDir(new_name);
return true;
}
@@ -278,8 +275,10 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
if (!isExists()) return l;
PIString dp = absolutePath();
PIString p(dp);
if (dp == ".") dp.clear();
else if (!dp.endsWith(separator)) dp += separator;
if (dp == ".")
dp.clear();
else if (!dp.endsWith(separator))
dp += separator;
// piCout << "start entries from" << p;
#ifdef WINDOWS
if (dp == separator) {
@@ -290,7 +289,7 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
for (DWORD i = 0; i < ll; ++i) {
if (letters[i] == '\0') {
clet.resize(2);
fi.path = clet;
fi.path = clet;
fi.flags = PIFile::FileInfo::Dir;
l << fi;
clet.clear();
@@ -298,7 +297,8 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
clet += PIChar(letters[i]);
}
} else {
WIN32_FIND_DATAA fd; memset(&fd, 0, sizeof(fd));
WIN32_FIND_DATAA fd;
memset(&fd, 0, sizeof(fd));
p += "\\*";
void * hf = FindFirstFileA((LPCSTR)(p.data()), &fd);
if (!hf) return l;
@@ -313,7 +313,7 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
l.sort(sort_compare);
if (!hdd) {
PIFile::FileInfo fi;
fi.path = "..";
fi.path = "..";
fi.flags = PIFile::FileInfo::Dir | PIFile::FileInfo::DotDot;
l.push_front(fi);
}
@@ -322,8 +322,8 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
#else
# if defined(QNX) || defined(FREERTOS)
struct dirent * de = 0;
DIR * dir = 0;
dir = opendir(p.data());
DIR * dir = 0;
dir = opendir(p.data());
if (dir) {
for (;;) {
de = readdir(dir);
@@ -334,12 +334,14 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
}
# else
dirent ** list;
int cnt = scandir(p.data(), &list, 0,
# if defined(MAC_OS) || defined(ANDROID) || defined(BLACKBERRY)
alphasort);
# else
versionsort);
# endif
int cnt = scandir(p.data(),
&list,
0,
# if defined(MAC_OS) || defined(ANDROID) || defined(BLACKBERRY)
alphasort);
# else
versionsort);
# endif
for (int i = 0; i < cnt; ++i) {
l << PIFile::fileInfo(dp + PIString(list[i]->d_name));
free(list[i]);
@@ -347,7 +349,7 @@ PIVector<PIFile::FileInfo> PIDir::entries() {
free(list);
# endif
#endif
// piCout << "end entries from" << p;
// piCout << "end entries from" << p;
return l;
}
@@ -370,16 +372,17 @@ PIVector<PIFile::FileInfo> PIDir::allEntries() {
PIStringList cdirs, ndirs;
cdirs << path();
while (!cdirs.isEmpty()) {
piForeachC (PIString & d, cdirs) {
scan_ = d;
piForeachC(PIString & d, cdirs) {
scan_ = d;
PIVector<PIFile::FileInfo> el = PIDir(d).entries();
piForeachC (PIFile::FileInfo & de, el) {
piForeachC(PIFile::FileInfo & de, el) {
if (de.name() == "." || de.name() == "..") continue;
if (de.isSymbolicLink()) continue; /// TODO: resolve symlinks
if (de.isDir()) {
dirs << de;
ndirs << de.path;
} else ret << de;
} else
ret << de;
}
}
cdirs = ndirs;
@@ -391,7 +394,6 @@ PIVector<PIFile::FileInfo> PIDir::allEntries() {
}
bool PIDir::isExists(const PIString & path) {
#ifdef WINDOWS
DWORD ret = GetFileAttributes((LPCTSTR)(path.data()));
@@ -434,20 +436,20 @@ PIDir PIDir::home() {
#ifdef WINDOWS
rc = new char[1024];
memset(rc, 0, 1024);
if (ExpandEnvironmentStrings((LPCTSTR)"%HOMEPATH%", (LPTSTR)rc, 1024) == 0) {
if (ExpandEnvironmentStrings((LPCTSTR) "%HOMEPATH%", (LPTSTR)rc, 1024) == 0) {
delete[] rc;
return PIDir();
}
PIString hp(rc);
memset(rc, 0, 1024);
if (ExpandEnvironmentStrings((LPCTSTR)"%HOMEDRIVE%", (LPTSTR)rc, 1024) == 0) {
if (ExpandEnvironmentStrings((LPCTSTR) "%HOMEDRIVE%", (LPTSTR)rc, 1024) == 0) {
delete[] rc;
return PIDir();
}
PIString hd(rc);
hp.replaceAll("\\", PIDir::separator);
delete[] rc;
//s.prepend(separator);
// s.prepend(separator);
return PIDir(hd + hp);
#else
# ifndef ESP_PLATFORM
@@ -485,7 +487,7 @@ PIDir PIDir::temporary() {
}
PIVector<PIFile::FileInfo> PIDir::allEntries(const PIString &path) {
PIVector<PIFile::FileInfo> PIDir::allEntries(const PIString & path) {
return PIDir(path).allEntries();
}
@@ -535,5 +537,3 @@ bool PIDir::renameDir(const PIString & path, const PIString & new_name) {
printf("[PIDir] renameDir(\"%s\", \"%s\") error: %s\n", path.data(), new_name.data(), errorString().data());
return false;
}