Skip to content

Commit

Permalink
files: add files glob()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeefo committed Jan 30, 2024
1 parent aa8e1e0 commit 5225f85
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
117 changes: 117 additions & 0 deletions crlib/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,121 @@ class MemFile final : public NonCopyable {
}
};

namespace detail {
struct FileEnumeratorEntry : public NonCopyable {
FileEnumeratorEntry () = default;
~FileEnumeratorEntry () = default;

#if defined (CR_WINDOWS)
bool next {};
String path {};
HANDLE handle {};
WIN32_FIND_DATAA data {};
#else
String path {};
String mask {};
DIR *dir {};
dirent *entry {};
#endif
};
}

// file enumerator
class FileEnumerator : public NonCopyable {
private:
UniquePtr <detail::FileEnumeratorEntry> entries_;

public:
FileEnumerator (StringRef mask) : entries_ (cr::makeUnique <detail::FileEnumeratorEntry> ()) {
start (mask);
}

~FileEnumerator () {
close ();
}

public:
void start (StringRef mask) {
auto lastSeperatorPos = mask.findLastOf (kPathSeparator);
Twin <String, String> pam {};

if (lastSeperatorPos != StringRef::InvalidIndex) {
pam = {
mask.substr (0, lastSeperatorPos),
mask.substr (1 + lastSeperatorPos)
};
}
else {
pam = { ".", mask };
}
entries_->path = pam.first;

#if defined (CR_WINDOWS)
entries_->handle = FindFirstFileA (mask.chars (), &entries_->data);
entries_->next = entries_->handle != INVALID_HANDLE_VALUE;
#else
entries_->dir = opendir (entries_->path.chars ());
entries_->mask = pam.second;

if (entries_->dir != nullptr) {
next ();
}
#endif
}

void close () {
#if defined (CR_WINDOWS)
if (entries_->handle != INVALID_HANDLE_VALUE) {
FindClose (entries_->handle);

entries_->handle = INVALID_HANDLE_VALUE;
entries_->next = false;
}
#else
if (entries_->dir != nullptr) {
closedir (entries_->dir);
entries_->dir = nullptr;
}
#endif
}

bool next () {
#if defined (CR_WINDOWS)
entries_->next = !!FindNextFileA (entries_->handle, &entries_->data);
return entries_->next;
#else
while ((entries_->entry = readdir (entries_->dir)) != nullptr) {
if (!fnmatch (entries_->mask.chars (), entries_->entry->d_name, FNM_CASEFOLD | FNM_NOESCAPE | FNM_PERIOD)) {
return true;
}
}
return false;
#endif
}

bool stillValid () const {
#if defined (CR_WINDOWS)
return entries_->next;
#else
return entries_->dir != nullptr && entries_->entry != nullptr;
#endif
}

String getMatch () const {
StringRef match {};

#if defined (CR_WINDOWS)
match = entries_->data.cFileName;
#else
match = entries_->entry->d_name;
#endif
return String::join ({ entries_->path, match }, kPathSeparator);
}

public:
operator bool () const {
return stillValid ();
}
};

CR_NAMESPACE_END
4 changes: 2 additions & 2 deletions crlib/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class HttpClient final : public Singleton <HttpClient> {
size_t responseCodeStart = response.find ("HTTP/1.1");

if (responseCodeStart != String::InvalidIndex) {
String respCode = response.substr (responseCodeStart + 9, 3);
String respCode = response.substr (responseCodeStart + cr::bufsize ("HTTP 1/1 "), 3);
respCode.trim ();

return static_cast <HttpClientResult> (respCode.int_ ());
Expand Down Expand Up @@ -456,6 +456,7 @@ class HttpClient final : public Singleton <HttpClient> {
// send the main request
if (socket->send (request.chars (), static_cast <int32_t> (request.length ())) < 1) {
statusCode_ = HttpClientResult::SocketError;

return false;
}

Expand Down Expand Up @@ -485,7 +486,6 @@ class HttpClient final : public Singleton <HttpClient> {
return false;
}
statusCode_ = parseResponseHeader (socket.get (), buffer.data ());

return statusCode_ == HttpClientResult::Ok;
}

Expand Down
15 changes: 14 additions & 1 deletion crlib/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ constexpr auto kPathSeparator = "/";
constexpr auto kLibrarySuffix = ".so";
#endif
# include <unistd.h>
# include <dirent.h>
# include <fnmatch.h>
# include <strings.h>
# include <sys/time.h>
#endif
Expand Down Expand Up @@ -378,7 +380,7 @@ struct Platform : public Singleton <Platform> {
#endif
}

// anologue of memset
// analogue of memset
template <typename U> void bzero (U *ptr, size_t len) noexcept {
memset (reinterpret_cast <void *> (ptr), 0, len);
}
Expand Down Expand Up @@ -413,6 +415,17 @@ struct Platform : public Singleton <Platform> {
return result;
}

const char *tmpfname () {
#if defined (CR_CXX_MSVC)
static char name[L_tmpnam_s];
tmpnam_s (name);

return name;
#else
return tmpnam (nullptr);
#endif
}

int32_t hardwareConcurrency () {
#if defined (CR_WINDOWS)
SYSTEM_INFO sysinfo;
Expand Down

0 comments on commit 5225f85

Please sign in to comment.