style port v1
This commit is contained in:
1 parent
8b731e941f
commit
1e203a2245
15 files changed
+456
-124
No files matched your search
@@ -11,6 +11,31 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <stdexcept>
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
|
||||
namespace std
|
||||
{
|
||||
namespace experimental
|
||||
{
|
||||
namespace fundamentals_v1
|
||||
{
|
||||
void __throw_out_of_range_fmt(const char *format, ...)
|
||||
{
|
||||
char buffer[1024];
|
||||
va_list argptr;
|
||||
|
||||
va_start(argptr, format);
|
||||
vsnprintf(buffer, sizeof(buffer), format, argptr);
|
||||
buffer[sizeof(buffer) - 1] = 0;
|
||||
va_end(argptr);
|
||||
|
||||
throw std::out_of_range(buffer);
|
||||
}
|
||||
} // namespace fundamentals_v1
|
||||
} // namespace experimental
|
||||
} // namespace std
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// astyle namespace
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,32 +1,53 @@
|
||||
CXX:=kos32-g++
|
||||
CC:=kos32-gcc
|
||||
LD:=kos32-ld
|
||||
SDK_DIR:=/home/pt/dev/kolibri/latest/contrib/sdk
|
||||
CXXFLAGS:=-c -fno-ident -O2 -std=c++17 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -Wno-pointer-arith
|
||||
#-c -m32 -march=native -mtune=generic -std=c99 -O2 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32
|
||||
#LDFLAGS:=-static -nostdlib --subsystem native --image-base 0 -T $(SDK_DIR)/sources/newlib/static.lds -Map=$app_name.map
|
||||
INCLUDE:=-I $(SDK_DIR)/sources/newlib/libc/include -I $(SDK_DIR)/sources/libstdc++-v3/include -I /usr/local/kos32/lib/gcc/mingw32/5.4.0/include/c++/experimental
|
||||
CXXFLAGS:=-c -fno-ident -O2 -std=c++14 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -Wno-pointer-arith -Wall
|
||||
CFLAGS:=-c -fno-ident -O2 -std=c99 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -Wno-pointer-arith
|
||||
INCLUDE:=-I $(SDK_DIR)/sources/newlib/libc/include -I $(SDK_DIR)/sources/libstdc++-v3/include -I /usr/local/kos32/lib/gcc/mingw32/5.4.0/include/c++/experimental -I.
|
||||
LDFLAGS:=-static -S -Tapp-dynamic.lds --image-base 0
|
||||
LIBPATH:=-L $(SDK_DIR)/lib -L /usr/local/kos32/lib
|
||||
|
||||
all: compile link make_iso copy_iso
|
||||
|
||||
compile:
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o main.o main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o ASBeautifier.o ASBeautifier.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o ASEnhancer.o ASEnhancer.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o ASFormatter.o ASFormatter.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o ASLocalizer.o ASLocalizer.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o ASResource.o ASResource.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o astyle_main.o astyle_main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o filesystem.o filesystem.cpp
|
||||
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o opendir.o $(SDK_DIR)/sources/newlib/libc/posix/opendir.c
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o readdir.o $(SDK_DIR)/sources/newlib/libc/posix/readdir.c
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o closedir.o $(SDK_DIR)/sources/newlib/libc/posix/closedir.c
|
||||
|
||||
clean:
|
||||
rm -f main.o main iso app.iso
|
||||
rm -f unit.o filesystem.o opendir.o readdir.o closedir.o unit iso app.iso
|
||||
|
||||
link:
|
||||
$(LD) $(LDFLAGS) $(LIBPATH) main.o -o main -lstdc++ -lsupc++ -lgcc -lc.dll
|
||||
#-lc -lgcc -lstdc++ -lsupc++
|
||||
kos32-strip -s main -o main
|
||||
kos32-objcopy main -O binary
|
||||
copy:
|
||||
cp main /mnt/e/
|
||||
$(LD) $(LDFLAGS) $(LIBPATH) \
|
||||
ASBeautifier.o \
|
||||
ASEnhancer.o \
|
||||
ASFormatter.o \
|
||||
ASLocalizer.o \
|
||||
ASResource.o \
|
||||
astyle_main.o \
|
||||
filesystem.o \
|
||||
opendir.o \
|
||||
closedir.o \
|
||||
readdir.o -o astyle -lstdc++ -lsupc++ -lgcc -lc.dll /usr/local/kos32/sdk/lib/libstdc++.a
|
||||
kos32-strip -s astyle -o astyle
|
||||
kos32-objcopy astyle -O binary
|
||||
|
||||
make_iso:
|
||||
rm -rf iso
|
||||
mkdir -p iso
|
||||
cp main iso/
|
||||
mkisofs -appid KolibriTestApp -volid KolibriTestApp -XA -sysid KOLIBRI -publisher pvoid -volset-size 1 -volset-seqno 1 -iso-level 3 -J -o app.iso iso/main
|
||||
cp astyle iso/
|
||||
mkisofs -appid KolibriTestApp -volid KolibriTestApp -XA -sysid KOLIBRI -publisher pvoid -volset-size 1 -volset-seqno 1 -iso-level 3 -J -o app.iso iso/astyle
|
||||
rm -rf iso
|
||||
|
||||
copy_iso:
|
||||
rm -f /mnt/e/app.iso
|
||||
cp app.iso /mnt/e/
|
||||
cp app.iso /mnt/e/
|
||||
@@ -0,0 +1,33 @@
|
||||
CXX:=kos32-g++
|
||||
CC:=kos32-gcc
|
||||
LD:=kos32-ld
|
||||
SDK_DIR:=/home/pt/dev/kolibri/latest/contrib/sdk
|
||||
CXXFLAGS:=-c -fno-ident -O2 -std=c++14 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -Wno-pointer-arith -Wall -Weffc++
|
||||
CFLAGS:=-c -fno-ident -O2 -std=c99 -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -Wno-pointer-arith
|
||||
INCLUDE:=-I $(SDK_DIR)/sources/newlib/libc/include -I $(SDK_DIR)/sources/libstdc++-v3/include -I /usr/local/kos32/lib/gcc/mingw32/5.4.0/include/c++/experimental
|
||||
LDFLAGS:=-static -S -Tapp-dynamic.lds --image-base 0
|
||||
LIBPATH:=-L $(SDK_DIR)/lib -L /usr/local/kos32/lib
|
||||
|
||||
all: compile link make_iso copy_iso
|
||||
|
||||
compile:
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o unit.o unit.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDE) -o filesystem.o filesystem.cpp
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o opendir.o $(SDK_DIR)/sources/newlib/libc/posix/opendir.c
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o readdir.o $(SDK_DIR)/sources/newlib/libc/posix/readdir.c
|
||||
$(CC) $(CFLAGS) $(INCLUDE) -o closedir.o $(SDK_DIR)/sources/newlib/libc/posix/closedir.c
|
||||
|
||||
clean:
|
||||
rm -f unit.o filesystem.o opendir.o readdir.o closedir.o unit iso app.iso
|
||||
|
||||
link:
|
||||
$(LD) $(LDFLAGS) $(LIBPATH) unit.o filesystem.o opendir.o readdir.o closedir.o -o unit -lstdc++ -lsupc++ -lgcc -lc.dll
|
||||
kos32-strip -s unit -o unit
|
||||
kos32-objcopy unit -O binary
|
||||
|
||||
make_iso:
|
||||
rm -rf iso
|
||||
mkdir -p iso
|
||||
cp unit iso/
|
||||
mkisofs -appid KolibriTestApp -volid KolibriTestApp -XA -sysid KOLIBRI -publisher pvoid -volset-size 1 -volset-seqno 1 -iso-level 3 -J -o unit.iso iso/unit
|
||||
rm -rf iso
|
||||
@@ -28,7 +28,7 @@
|
||||
namespace std
|
||||
{
|
||||
using string_view=std::experimental::fundamentals_v1::string_view;
|
||||
int isblank(int c){return ::isblank(c);}
|
||||
inline int isblank(int c){return ::isblank(c);}
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
@@ -915,12 +915,15 @@ void ASConsole::getFileNames(const std::string& directory, const std::vector<std
|
||||
// Save files and subdirectories
|
||||
std::vector<std::string> subDirectory;
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
|
||||
const std::string entryPath = entry.path().string();
|
||||
const std::string entryName = entry.path().filename().string();
|
||||
std::list<std::filesystem::path> paths;
|
||||
std::filesystem::scan_directory(directory, paths);
|
||||
|
||||
for (const auto& entry : paths) {
|
||||
const std::string entryPath = entry.string();
|
||||
const std::string entryName = entry.filename().string();
|
||||
|
||||
// Skip hidden or read-only files
|
||||
if (entryName[0] == '.' || !isWriteable(entry.path()))
|
||||
if (entryName[0] == '.' || !isWriteable(entry))
|
||||
continue;
|
||||
|
||||
// If it's a directory and recursive is enabled
|
||||
@@ -1277,9 +1280,9 @@ void ASConsole::launchDefaultBrowser(const char* filePathIn /*nullptr*/) const
|
||||
printf(_("Opening HTML documentation %s\n"), htmlFilePath.c_str());
|
||||
if (!bypassBrowserOpen)
|
||||
{
|
||||
execlp(fileOpen, fileOpen, htmlFilePath.c_str(), nullptr);
|
||||
//execlp(fileOpen, fileOpen, htmlFilePath.c_str(), nullptr);
|
||||
// execlp will NOT return if successful
|
||||
error(_("Command execute failure"), fileOpen);
|
||||
error(_("Cannot start default browser"), fileOpen);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2561,10 +2564,10 @@ int ASConsole::wildcmp(const char* wild, const char* data) const
|
||||
void ASConsole::writeFile(const std::string& fileName_, FileEncoding encoding, std::ostringstream& out) const
|
||||
{
|
||||
// save date accessed and date modified of original file
|
||||
struct stat stBuf;
|
||||
bool statErr = false;
|
||||
if (stat(fileName_.c_str(), &stBuf) == -1)
|
||||
statErr = true;
|
||||
// struct stat stBuf;
|
||||
// bool statErr = false;
|
||||
// if (stat(fileName_.c_str(), &stBuf) == -1)
|
||||
// statErr = true;
|
||||
|
||||
// create a backup
|
||||
if (!noBackup)
|
||||
@@ -2595,26 +2598,29 @@ void ASConsole::writeFile(const std::string& fileName_, FileEncoding encoding, s
|
||||
|
||||
fout.close();
|
||||
|
||||
// TODO : change date modified to original file date
|
||||
// No utime() implementation now
|
||||
|
||||
// change date modified to original file date
|
||||
// Embarcadero must be linked with cw32mt not cw32
|
||||
if (preserveDate)
|
||||
{
|
||||
if (!statErr)
|
||||
{
|
||||
struct utimbuf outBuf;
|
||||
outBuf.actime = stBuf.st_atime;
|
||||
// add ticks so 'make' will recognize a change
|
||||
// Visual Studio 2008 needs more than 1
|
||||
outBuf.modtime = stBuf.st_mtime + 10;
|
||||
if (utime(fileName_.c_str(), &outBuf) == -1)
|
||||
statErr = true;
|
||||
}
|
||||
if (statErr)
|
||||
{
|
||||
perror("errno message");
|
||||
(*errorStream) << "********* Cannot preserve file date" << '\n';
|
||||
}
|
||||
}
|
||||
// if (preserveDate)
|
||||
// {
|
||||
// if (!statErr)
|
||||
// {
|
||||
// struct utimbuf outBuf;
|
||||
// outBuf.actime = stBuf.st_atime;
|
||||
// // add ticks so 'make' will recognize a change
|
||||
// // Visual Studio 2008 needs more than 1
|
||||
// outBuf.modtime = stBuf.st_mtime + 10;
|
||||
// if (utime(fileName_.c_str(), &outBuf) == -1)
|
||||
// statErr = true;
|
||||
// }
|
||||
// if (statErr)
|
||||
// {
|
||||
// perror("errno message");
|
||||
// (*errorStream) << "********* Cannot preserve file date" << '\n';
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
#else // ASTYLE_LIB
|
||||
File renamed without changes.
+123
@@ -0,0 +1,123 @@
|
||||
// See boost/filesystem/src/filesystem.cpp
|
||||
|
||||
#include "filesystem.h"
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace std {
|
||||
namespace filesystem {
|
||||
|
||||
typedef path::value_type value_type;
|
||||
typedef path::string_type string_type;
|
||||
typedef string_type::size_type size_type;
|
||||
|
||||
inline size_type find_separator(const value_type* p, size_type size) noexcept
|
||||
{
|
||||
const value_type* sep = static_cast< const value_type* >(std::memchr(p, path::preferred_separator, size));
|
||||
size_type pos = size;
|
||||
if (!!sep)
|
||||
pos = sep - p;
|
||||
return pos;
|
||||
}
|
||||
|
||||
path operator/(path lhs, path const& rhs)
|
||||
{
|
||||
return lhs /= rhs;
|
||||
}
|
||||
|
||||
path current_path()
|
||||
{
|
||||
char buf[path::max_path];
|
||||
memset(buf, 0, path::max_path);
|
||||
const auto rv = _ksys_getcwd(buf, path::max_path - 1u);
|
||||
return (rv == NULL) ? path{} : path{buf};
|
||||
}
|
||||
|
||||
path absolute(path const& p)
|
||||
{
|
||||
return p; // TODO
|
||||
}
|
||||
|
||||
void scan_directory (path const& directory, std::list<path>& paths)
|
||||
{
|
||||
static const auto scopedCloseDir = [](DIR* pdir)
|
||||
{
|
||||
closedir(pdir);
|
||||
};
|
||||
|
||||
std::unique_ptr<DIR, decltype(scopedCloseDir)> pdir(opendir (directory.c_str()), scopedCloseDir);
|
||||
dirent *pent = NULL;
|
||||
|
||||
while (pent = readdir (pdir.get()))
|
||||
{
|
||||
if (pent->d_name[0] == path::dot)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
path fileName(directory.c_str());
|
||||
fileName /= pent->d_name;
|
||||
|
||||
if (is_directory(fileName))
|
||||
{
|
||||
scan_directory (fileName, paths);
|
||||
}
|
||||
else if (is_regular_file(fileName))
|
||||
{
|
||||
paths.emplace_back(std::move(fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pos is position of the separator
|
||||
bool is_root_separator(string_type const& str, size_type root_dir_pos, size_type pos);
|
||||
|
||||
// Returns: Size of the filename element that ends at end_pos (which is past-the-end position). 0 if no filename found.
|
||||
size_type find_filename_size(string_type const& str, size_type root_name_size, size_type end_pos);
|
||||
|
||||
// Returns: starting position of root directory or size if not found. Sets root_name_size to length
|
||||
// of the root name if the characters before the returned position (if any) are considered a root name.
|
||||
size_type find_root_directory_start(const value_type* path, size_type size, size_type& root_name_size);
|
||||
|
||||
// Finds position and size of the first element of the path
|
||||
void first_element(string_type const& src, size_type& element_pos, size_type& element_size, size_type size);
|
||||
|
||||
// Finds position and size of the first element of the path
|
||||
inline void first_element(string_type const& src, size_type& element_pos, size_type& element_size)
|
||||
{
|
||||
first_element(src, element_pos, element_size, src.size());
|
||||
}
|
||||
|
||||
// Checks if the second string overlaps in memory with the first string
|
||||
inline bool is_overlapping(string_type const& str, const value_type* arg)
|
||||
{
|
||||
std::less< const value_type* > ptr_less{};
|
||||
return !(ptr_less(arg, str.data()) || ptr_less(str.data() + str.size(), arg));
|
||||
}
|
||||
|
||||
inline size_type find_filename_size(string_type const& str, size_type root_name_size, size_type end_pos)
|
||||
{
|
||||
size_type pos = end_pos;
|
||||
while (pos > root_name_size)
|
||||
{
|
||||
--pos;
|
||||
|
||||
if (path::is_directory_separator(str[pos]))
|
||||
{
|
||||
++pos; // filename starts past the separator
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return end_pos - pos;
|
||||
}
|
||||
|
||||
} // namespace filesystem
|
||||
} // namespace std
|
||||
+112
-25
@@ -4,24 +4,49 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
#include <list>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace std
|
||||
{
|
||||
|
||||
namespace filesystem
|
||||
{
|
||||
|
||||
class path
|
||||
class path;
|
||||
|
||||
namespace path_detail {
|
||||
|
||||
template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
|
||||
struct path_constants
|
||||
{
|
||||
typedef path_constants< Char, Separator, PreferredSeparator, Dot > path_constants_base;
|
||||
typedef Char value_type;
|
||||
static constexpr value_type separator = Separator;
|
||||
static constexpr value_type preferred_separator = PreferredSeparator;
|
||||
static constexpr value_type dot = Dot;
|
||||
};
|
||||
|
||||
template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
|
||||
constexpr typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
|
||||
path_constants< Char, Separator, PreferredSeparator, Dot >::separator;
|
||||
template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
|
||||
constexpr typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
|
||||
path_constants< Char, Separator, PreferredSeparator, Dot >::preferred_separator;
|
||||
template< typename Char, Char Separator, Char PreferredSeparator, Char Dot >
|
||||
constexpr typename path_constants< Char, Separator, PreferredSeparator, Dot >::value_type
|
||||
path_constants< Char, Separator, PreferredSeparator, Dot >::dot;
|
||||
|
||||
} // namespace path_detail
|
||||
|
||||
class path : public filesystem::path_detail::path_constants<char, '/', '/', '.'>
|
||||
{
|
||||
public:
|
||||
using value_type = char;
|
||||
|
||||
using string_type = std::basic_string< value_type >;
|
||||
constexpr static size_t max_path = 256u;
|
||||
constexpr static const value_type* const root_path = "/";
|
||||
|
||||
constexpr static value_type preferred_separator = '/';
|
||||
inline bool is_directory_separator(path::value_type c) noexcept
|
||||
inline constexpr static bool is_directory_separator(path::value_type c) noexcept
|
||||
{
|
||||
return c == preferred_separator;
|
||||
}
|
||||
@@ -29,7 +54,7 @@ public:
|
||||
template<class Char>
|
||||
path(const std::basic_string<Char>& s) : m_pathname(s) {};
|
||||
|
||||
path() noexcept {}
|
||||
path() noexcept : m_pathname() {}
|
||||
path(path const& p) : m_pathname(p.m_pathname) {}
|
||||
path(const value_type* s) : m_pathname(s) {}
|
||||
|
||||
@@ -91,7 +116,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
static string_type& quoted(const string_type& s, value_type q = '"')
|
||||
static string_type quoted(const string_type& s, value_type q = '"')
|
||||
{
|
||||
return string_type{}.append(1,q).append(s).append(1,q);
|
||||
}
|
||||
@@ -103,10 +128,55 @@ public:
|
||||
|
||||
path& operator/= (const path& p)
|
||||
{
|
||||
m_pathname = m_pathname + preferred_separator + p.m_pathname;
|
||||
if(empty())
|
||||
{
|
||||
if(p.string().find_first_of(preferred_separator) == 0)
|
||||
{
|
||||
assign(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pathname = preferred_separator + p.m_pathname;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const bool leftHasTrailingSeparator = m_pathname.at(m_pathname.size() - 1u) == preferred_separator;
|
||||
const bool rightStartsWithSeparator = p.string().find_first_of(preferred_separator) == 0;
|
||||
|
||||
if(leftHasTrailingSeparator && rightStartsWithSeparator)
|
||||
{
|
||||
const string_type& right = p.string();
|
||||
m_pathname.append(right.cbegin() + 1u, right.cend());
|
||||
}
|
||||
else if (!leftHasTrailingSeparator && !rightStartsWithSeparator)
|
||||
{
|
||||
m_pathname += (preferred_separator + p.m_pathname);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_pathname += p.m_pathname;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
path filename() const
|
||||
{
|
||||
if(empty() || m_pathname == "/")
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto pos = m_pathname.find_last_of(preferred_separator);
|
||||
if(pos == string_type::npos)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
return (pos == m_pathname.size() - 1) ? path{} : m_pathname.substr(pos + 1u);
|
||||
}
|
||||
|
||||
bool empty() const noexcept { return m_pathname.empty(); }
|
||||
|
||||
const value_type* c_str() const noexcept
|
||||
@@ -135,21 +205,22 @@ operator>>(std::basic_istream< Char, Traits >& is, path& p)
|
||||
return is;
|
||||
}
|
||||
|
||||
path operator/(path lhs, path const& rhs)
|
||||
{
|
||||
return lhs.concat(path::string_type{path::preferred_separator}).concat(rhs);
|
||||
}
|
||||
|
||||
path current_path()
|
||||
{
|
||||
char buf[path::max_path];
|
||||
memset(buf, 0, path::max_path);
|
||||
const auto rv = _ksys_getcwd(buf, path::max_path - 1u);
|
||||
return (rv == NULL) ? path{} : path{buf};
|
||||
}
|
||||
path current_path();
|
||||
path operator/(path lhs, path const& rhs);
|
||||
inline bool is_directory(path const& p);
|
||||
inline bool is_regular_file(path const& p);
|
||||
bool exists(path const& p);
|
||||
path absolute(path const& p);
|
||||
inline bool is_absolute(path const& p);
|
||||
void scan_directory (path const& directory, std::list<path>& paths);
|
||||
|
||||
inline bool is_directory(path const& p)
|
||||
{
|
||||
if(p.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat sb;
|
||||
if (stat(p.string().c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
|
||||
{
|
||||
@@ -160,6 +231,11 @@ inline bool is_directory(path const& p)
|
||||
|
||||
inline bool is_regular_file(path const& p)
|
||||
{
|
||||
if(p.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
struct stat sb;
|
||||
if (stat(p.string().c_str(), &sb) == 0 && S_ISREG(sb.st_mode))
|
||||
{
|
||||
@@ -168,17 +244,28 @@ inline bool is_regular_file(path const& p)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool exists(path const& p)
|
||||
inline bool exists(path const& p)
|
||||
{
|
||||
return (is_regular_file(p) || is_directory(p));
|
||||
}
|
||||
|
||||
path absolute(path const& p)
|
||||
inline bool is_absolute(path const& p)
|
||||
{
|
||||
return p; // TODO
|
||||
if(p.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto strPath(p.string());
|
||||
const auto pos = strPath.find_first_of(path::root_path);
|
||||
if(pos == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return pos == 0;
|
||||
}
|
||||
|
||||
} // namespace filesystem
|
||||
} // namespace std
|
||||
|
||||
#endif // ~__FILESYSTEM_H__
|
||||
#endif // !__FILESYSTEM_H__
|
||||
@@ -1,55 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <fstream>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <cctype>
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
// add this to astyle.h to compile under toolchain
|
||||
namespace std
|
||||
{
|
||||
using string_view=std::experimental::fundamentals_v1::string_view;
|
||||
int isblank(int c){return ::isblank(c);}
|
||||
}
|
||||
|
||||
void check_directory(const fs::path& p)
|
||||
{
|
||||
std::cout << "checking directory " << p << std::endl;
|
||||
std::cout << std::boolalpha << "result : " << fs::is_directory(p) << std::endl;
|
||||
}
|
||||
|
||||
void check_file(const fs::path& p)
|
||||
{
|
||||
std::cout << "checking file " << p << std::endl;
|
||||
std::cout << std::boolalpha << "result : " << fs::is_regular_file(p) << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
|
||||
const auto cwd = fs::current_path();
|
||||
std::cout << "cwd = " << cwd << std::endl;
|
||||
|
||||
check_directory(cwd);
|
||||
check_directory(cwd / "main");
|
||||
|
||||
check_directory("abcabc");
|
||||
|
||||
check_file(cwd);
|
||||
check_file(cwd / "main");
|
||||
|
||||
check_file("abcabc");
|
||||
|
||||
std::isblank(15);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
#include "filesystem.h"
|
||||
#include "unit_test_framework.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
struct TestData
|
||||
{
|
||||
struct fs
|
||||
{
|
||||
static constexpr char separator = '/';
|
||||
static constexpr char preferred_separator = '/';
|
||||
static constexpr char dot = '.';
|
||||
static constexpr auto emptyPath = "";
|
||||
static constexpr auto rootPath = "/";
|
||||
static constexpr auto sysPath = "/sys";
|
||||
static constexpr auto calcPath = "/sys/Calc";
|
||||
static constexpr auto calcFileName = "Calc";
|
||||
static constexpr auto invalidPath = "~-=%r7";
|
||||
};
|
||||
};
|
||||
|
||||
TEST(DefaultPathConstants)
|
||||
{
|
||||
ASSERT_EQUAL(TestData::fs::separator, fs::path::separator);
|
||||
ASSERT_EQUAL(TestData::fs::preferred_separator, fs::path::preferred_separator);
|
||||
ASSERT_EQUAL(TestData::fs::dot, fs::path::dot);
|
||||
}
|
||||
|
||||
TEST(PathIsDirectory)
|
||||
{
|
||||
ASSERT_TRUE(fs::is_directory(fs::current_path()));
|
||||
ASSERT_TRUE(fs::is_directory(TestData::fs::sysPath));
|
||||
ASSERT_TRUE(fs::is_directory(TestData::fs::rootPath));
|
||||
ASSERT_FALSE(fs::is_directory(TestData::fs::calcPath));
|
||||
ASSERT_FALSE(fs::is_directory(TestData::fs::invalidPath));
|
||||
ASSERT_FALSE(fs::is_directory(TestData::fs::emptyPath));
|
||||
}
|
||||
|
||||
TEST(PathIsRegularFile)
|
||||
{
|
||||
ASSERT_FALSE(fs::is_regular_file(fs::current_path()));
|
||||
ASSERT_FALSE(fs::is_regular_file(TestData::fs::rootPath));
|
||||
ASSERT_FALSE(fs::is_regular_file(TestData::fs::emptyPath));
|
||||
ASSERT_FALSE(fs::is_regular_file(TestData::fs::invalidPath));
|
||||
ASSERT_TRUE(fs::is_regular_file(TestData::fs::calcPath));
|
||||
}
|
||||
|
||||
TEST(PathIsAbsolute)
|
||||
{
|
||||
ASSERT_TRUE(fs::is_absolute(fs::current_path()));
|
||||
ASSERT_TRUE(fs::is_absolute(TestData::fs::rootPath));
|
||||
ASSERT_TRUE(fs::is_absolute(TestData::fs::sysPath));
|
||||
ASSERT_TRUE(fs::is_absolute(TestData::fs::calcPath));
|
||||
ASSERT_FALSE(fs::is_absolute(TestData::fs::emptyPath));
|
||||
ASSERT_FALSE(fs::is_absolute(TestData::fs::invalidPath));
|
||||
}
|
||||
|
||||
TEST(PathConcat)
|
||||
{
|
||||
fs::path p;
|
||||
ASSERT_TRUE(p.empty());
|
||||
ASSERT_EQUAL((p /= TestData::fs::rootPath).string(), "/");
|
||||
ASSERT_EQUAL((p / TestData::fs::calcFileName).string(), "/Calc");
|
||||
ASSERT_EQUAL((p / TestData::fs::sysPath).string(), "/sys");
|
||||
ASSERT_EQUAL(((p /= TestData::fs::sysPath) / TestData::fs::calcFileName).string(), "/sys/Calc");
|
||||
}
|
||||
|
||||
TEST(PathFileName)
|
||||
{
|
||||
ASSERT_EQUAL(fs::path(TestData::fs::emptyPath).filename().string(), TestData::fs::emptyPath);
|
||||
ASSERT_EQUAL(fs::path(TestData::fs::invalidPath).filename().string(), TestData::fs::invalidPath);
|
||||
ASSERT_EQUAL(fs::path(TestData::fs::calcPath).filename().string(), TestData::fs::calcFileName);
|
||||
ASSERT_EQUAL(fs::path(TestData::fs::rootPath).filename().string(), TestData::fs::emptyPath);
|
||||
ASSERT_EQUAL(fs::path(TestData::fs::sysPath).filename().string(), "sys");
|
||||
}
|
||||
|
||||
// just for cli output
|
||||
TEST(ScanDirectory)
|
||||
{
|
||||
std::list<fs::path> l;
|
||||
fs::scan_directory(fs::current_path().c_str(), l);
|
||||
std::cout << "entries: " << l.size() << std::endl;
|
||||
for(const auto& p : l)
|
||||
{
|
||||
std::cout << p << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_MAIN()
|
||||
Reference in new issue
Block a user