diff options
-rw-r--r-- | libadhocutil/curlHandle.h | 104 | ||||
-rw-r--r-- | libadhocutil/curlMultiHandle.h | 50 | ||||
-rw-r--r-- | libadhocutil/curlStream.h | 62 | ||||
-rw-r--r-- | libadhocutil/fileUtils.h | 312 | ||||
-rw-r--r-- | libadhocutil/lexer-regex.h | 22 | ||||
-rw-r--r-- | libadhocutil/processPipes.h | 91 | ||||
-rw-r--r-- | libadhocutil/runtimeContext.h | 72 |
7 files changed, 347 insertions, 366 deletions
diff --git a/libadhocutil/curlHandle.h b/libadhocutil/curlHandle.h index 1501080..ba1df78 100644 --- a/libadhocutil/curlHandle.h +++ b/libadhocutil/curlHandle.h @@ -7,59 +7,57 @@ #include <memory> #include <string> -namespace AdHoc { - namespace Net { - - /// libcurl handle wrapper. - /** Wraps a libcurl CURL * object in a C++ friendly manner. */ - class DLL_PUBLIC CurlHandle { - public: - /** - * Create a new CurlHandle. - * @param url Set the required CURLOPT_URL property to the given url. - */ - explicit CurlHandle(const std::string & url); - /// Standard move/copy support - SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlHandle); - virtual ~CurlHandle(); - - /** Set option wrapper. */ - template<typename T> - void - setopt(CURLoption opt, const T & val) - { - curl_easy_setopt(curl_handle, opt, val); - } - /** Get info for long values */ - void getinfo(CURLINFO info, long & val) const; - /** Get info for int values (avoids ambiguous call errors for ease of use) */ - void getinfo(CURLINFO info, int & val) const; - /** Get info for double values */ - void getinfo(CURLINFO info, double & val) const; - /** Get info for char * values */ - void getinfo(CURLINFO info, char *& val) const; - /** Append the given HTTP header */ - void appendHeader(const char *); - /** Append the given HTTP post content */ - void appendPost(const char *, const char *); - /** Perform the CURL transfer. */ - void perform(); - - /** Get the underlying CURL * handle. @warning Make changes at your own risk. */ - // NOLINTNEXTLINE(hicpp-explicit-conversions) - operator CURL *() const; - - protected: - /// @cond - void checkCurlCode(CURLcode res) const; - - CURL * curl_handle; - curl_slist * curl_headers; - curl_httppost *postS, *postE; - /// @endcond - }; - using CurlHandlePtr = std::shared_ptr<CurlHandle>; - } +namespace AdHoc::Net { + + /// libcurl handle wrapper. + /** Wraps a libcurl CURL * object in a C++ friendly manner. */ + class DLL_PUBLIC CurlHandle { + public: + /** + * Create a new CurlHandle. + * @param url Set the required CURLOPT_URL property to the given url. + */ + explicit CurlHandle(const std::string & url); + /// Standard move/copy support + SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlHandle); + virtual ~CurlHandle(); + + /** Set option wrapper. */ + template<typename T> + void + setopt(CURLoption opt, const T & val) + { + curl_easy_setopt(curl_handle, opt, val); + } + /** Get info for long values */ + void getinfo(CURLINFO info, long & val) const; + /** Get info for int values (avoids ambiguous call errors for ease of use) */ + void getinfo(CURLINFO info, int & val) const; + /** Get info for double values */ + void getinfo(CURLINFO info, double & val) const; + /** Get info for char * values */ + void getinfo(CURLINFO info, char *& val) const; + /** Append the given HTTP header */ + void appendHeader(const char *); + /** Append the given HTTP post content */ + void appendPost(const char *, const char *); + /** Perform the CURL transfer. */ + void perform(); + + /** Get the underlying CURL * handle. @warning Make changes at your own risk. */ + // NOLINTNEXTLINE(hicpp-explicit-conversions) + operator CURL *() const; + + protected: + /// @cond + void checkCurlCode(CURLcode res) const; + + CURL * curl_handle; + curl_slist * curl_headers; + curl_httppost *postS, *postE; + /// @endcond + }; + using CurlHandlePtr = std::shared_ptr<CurlHandle>; } #endif diff --git a/libadhocutil/curlMultiHandle.h b/libadhocutil/curlMultiHandle.h index 56964dd..81dc9c2 100644 --- a/libadhocutil/curlMultiHandle.h +++ b/libadhocutil/curlMultiHandle.h @@ -11,40 +11,36 @@ #include <set> #include <string> -namespace AdHoc { - namespace Net { +namespace AdHoc::Net { + class RunningCurl; + using RunningCurlPtr = std::shared_ptr<RunningCurl>; - class RunningCurl; - using RunningCurlPtr = std::shared_ptr<RunningCurl>; + /// Perform multiple CURL operations at once. + class DLL_PUBLIC CurlMultiHandle { + public: + /** A function that should consume the inbound byte stream. */ + using Consumer = std::function<void(std::istream &)>; - /// Perform multiple CURL operations at once. - class DLL_PUBLIC CurlMultiHandle { - public: - /** A function that should consume the inbound byte stream. */ - using Consumer = std::function<void(std::istream &)>; + CurlMultiHandle(); + ~CurlMultiHandle(); - CurlMultiHandle(); - ~CurlMultiHandle(); + /// Standard move/copy support + SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlMultiHandle); - /// Standard move/copy support - SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlMultiHandle); + /** Adds a new consumer for the given URL to the set of operations to perform. */ + CurlHandlePtr addCurl(const std::string &, const Consumer &); + /** Perform all queued operations. */ + void performAll(); - /** Adds a new consumer for the given URL to the set of operations to perform. */ - CurlHandlePtr addCurl(const std::string &, const Consumer &); - /** Perform all queued operations. */ - void performAll(); + private: + using CURLs = std::set<RunningCurlPtr>; + using Running = std::map<CURL *, RunningCurlPtr>; - private: - using CURLs = std::set<RunningCurlPtr>; - using Running = std::map<CURL *, RunningCurlPtr>; + DLL_PRIVATE void addRunner(CURLM * curlm, Running & running, CURLs & curls); - DLL_PRIVATE void addRunner(CURLM * curlm, Running & running, CURLs & curls); - - CURLs curls; - }; - using CurlMultiHandlePtr = std::shared_ptr<CurlMultiHandle>; - - } + CURLs curls; + }; + using CurlMultiHandlePtr = std::shared_ptr<CurlMultiHandle>; } #endif diff --git a/libadhocutil/curlStream.h b/libadhocutil/curlStream.h index 6425a2c..51d7d31 100644 --- a/libadhocutil/curlStream.h +++ b/libadhocutil/curlStream.h @@ -14,38 +14,36 @@ namespace boost { template<class T> class reference_wrapper; } -namespace AdHoc { - namespace Net { - - /// boost::iostreams::source implementation for CURL downloads. - class DLL_PUBLIC CurlStreamSource : - public boost::iostreams::source, - public CurlHandle, - ::AdHoc::System::RuntimeContext { - public: - /** Construct a new stream source for the given URL. */ - explicit CurlStreamSource(const std::string & url); - /// Standard move/copy support - SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlStreamSource); - ~CurlStreamSource() override; - - /** Required member function for reading of the stream source by boost::iostreams::stream. */ - std::streamsize read(char * target, std::streamsize targetSize); - - private: - friend class CurlMultiHandle; - DLL_PRIVATE void callback() override; - - DLL_PRIVATE static size_t recvWrapper(void * data, size_t sz, size_t nm, void * css); - DLL_PRIVATE std::streamsize recv(void * data, std::streamsize datalen); - - std::streamsize buflen; - char * buf; - CURLcode res; - }; - - using CurlStream = boost::iostreams::stream<boost::reference_wrapper<CurlStreamSource>>; - } +namespace AdHoc::Net { + + /// boost::iostreams::source implementation for CURL downloads. + class DLL_PUBLIC CurlStreamSource : + public boost::iostreams::source, + public CurlHandle, + ::AdHoc::System::RuntimeContext { + public: + /** Construct a new stream source for the given URL. */ + explicit CurlStreamSource(const std::string & url); + /// Standard move/copy support + SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlStreamSource); + ~CurlStreamSource() override; + + /** Required member function for reading of the stream source by boost::iostreams::stream. */ + std::streamsize read(char * target, std::streamsize targetSize); + + private: + friend class CurlMultiHandle; + DLL_PRIVATE void callback() override; + + DLL_PRIVATE static size_t recvWrapper(void * data, size_t sz, size_t nm, void * css); + DLL_PRIVATE std::streamsize recv(void * data, std::streamsize datalen); + + std::streamsize buflen; + char * buf; + CURLcode res; + }; + + using CurlStream = boost::iostreams::stream<boost::reference_wrapper<CurlStreamSource>>; } #endif diff --git a/libadhocutil/fileUtils.h b/libadhocutil/fileUtils.h index f0a3cb2..eee8ab3 100644 --- a/libadhocutil/fileUtils.h +++ b/libadhocutil/fileUtils.h @@ -8,178 +8,176 @@ #include <string_view> #include <sys/stat.h> // IWYU pragma: export -namespace AdHoc { - namespace FileUtils { +namespace AdHoc::FileUtils { + /** + * Extract an element of a path. + * @param p The source path. + * @param n The index of the element to extract. + * @return The path element. + */ + DLL_PUBLIC std::filesystem::path operator/(const std::filesystem::path & p, unsigned int n); + + /** + * File handle wrapper to ensure closure on scope exit + */ + class DLL_PUBLIC FileHandle { + public: /** - * Extract an element of a path. - * @param p The source path. - * @param n The index of the element to extract. - * @return The path element. + * Move constructor. */ - DLL_PUBLIC std::filesystem::path operator/(const std::filesystem::path & p, unsigned int n); + FileHandle(FileHandle &&) noexcept; /** - * File handle wrapper to ensure closure on scope exit + * Construct from an existing file descriptor. + * @param fd An open file descriptor. */ - class DLL_PUBLIC FileHandle { - public: - /** - * Move constructor. - */ - FileHandle(FileHandle &&) noexcept; - - /** - * Construct from an existing file descriptor. - * @param fd An open file descriptor. - */ - explicit FileHandle(int fd) noexcept; - - /** - * Open a new file handle. - * @param path Path of file to open. - * @param flags File handle flags - */ - explicit FileHandle(const std::filesystem::path & path, int flags = O_RDONLY); - - /** - * Open a new file handle. - * @param path Path of file to open. - * @param flags File handle flags - * @param mode File handle mode - */ - FileHandle(const std::filesystem::path & path, int flags, int mode); - - virtual ~FileHandle() noexcept; - - /// Standard move/copy support - SPECIAL_MEMBERS_COPY(FileHandle, delete); - - /// Standard move/copy support - FileHandle & operator=(FileHandle &&) noexcept; - - /** - * Implicit conversion back to raw Unix file descriptor. - * @return The container file descriptor. - */ - // NOLINTNEXTLINE(hicpp-explicit-conversions) - operator int() const noexcept; - - /// The file handle. - const int fh; - }; + explicit FileHandle(int fd) noexcept; /** - * An extension to FileHandle that automatically calls fstat on the opened handle. + * Open a new file handle. + * @param path Path of file to open. + * @param flags File handle flags */ - class DLL_PUBLIC FileHandleStat : public FileHandle { - public: - ~FileHandleStat() override = default; - - /** - * Construct from an existing file descriptor. - * @param fd An open file descriptor. - */ - explicit FileHandleStat(int fd); - - /** - * Open a new file handle (with the default flags). - * @param path Path of file to open. - * @param flags File handle flags - */ - explicit FileHandleStat(const std::filesystem::path & path, int flags = O_RDONLY); - - /** - * Open a new file handle. - * @param path Path of file to open. - * @param flags File handle flags - * @param mode File handle mode - */ - FileHandleStat(const std::filesystem::path & path, int flags, int mode); - - /// Standard move/copy support - SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(FileHandleStat); - - /** - * Get the stat structure. - * @return The stat structure. - */ - [[nodiscard]] const struct stat & getStat() const noexcept; - - /** - * Refresh and return the stat structure. - * @return The stat structure. - */ - const struct stat & refreshStat(); - - protected: - /// The stat structure. - struct stat st; - - private: - DLL_PRIVATE void refreshStat(const std::filesystem::path & path); - }; + explicit FileHandle(const std::filesystem::path & path, int flags = O_RDONLY); /** - * Extension to FileHandle to automatically memmaps the file. + * Open a new file handle. + * @param path Path of file to open. + * @param flags File handle flags + * @param mode File handle mode */ - class DLL_PUBLIC MemMap : public FileHandleStat { - public: - /** - * Move constructor. - */ - MemMap(MemMap &&) noexcept = default; - - /** - * Construct from an existing file descriptor. - * @param fd An open file descriptor. - * @param flags File handle flags - */ - explicit MemMap(int fd, int flags = O_RDONLY); - - /** - * Open a new file handle (with the default flags). - * @param path Path of file to open. - * @param flags File handle flags - */ - explicit MemMap(const std::filesystem::path & path, int flags = O_RDONLY); - - /** - * Open a new file handle. - * @param path Path of file to open. - * @param flags File handle flags - * @param mode File handle mode - */ - MemMap(const std::filesystem::path & path, int flags, int mode); - - ~MemMap() override; - - /// Standard move/copy support - SPECIAL_MEMBERS_COPY(MemMap, delete); - - MemMap & operator=(MemMap &&) = delete; - - /// The file data. - const void * const data; + FileHandle(const std::filesystem::path & path, int flags, int mode); + + virtual ~FileHandle() noexcept; + + /// Standard move/copy support + SPECIAL_MEMBERS_COPY(FileHandle, delete); + + /// Standard move/copy support + FileHandle & operator=(FileHandle &&) noexcept; + + /** + * Implicit conversion back to raw Unix file descriptor. + * @return The container file descriptor. + */ + // NOLINTNEXTLINE(hicpp-explicit-conversions) + operator int() const noexcept; + + /// The file handle. + const int fh; + }; + + /** + * An extension to FileHandle that automatically calls fstat on the opened handle. + */ + class DLL_PUBLIC FileHandleStat : public FileHandle { + public: + ~FileHandleStat() override = default; + + /** + * Construct from an existing file descriptor. + * @param fd An open file descriptor. + */ + explicit FileHandleStat(int fd); + + /** + * Open a new file handle (with the default flags). + * @param path Path of file to open. + * @param flags File handle flags + */ + explicit FileHandleStat(const std::filesystem::path & path, int flags = O_RDONLY); + + /** + * Open a new file handle. + * @param path Path of file to open. + * @param flags File handle flags + * @param mode File handle mode + */ + FileHandleStat(const std::filesystem::path & path, int flags, int mode); + + /// Standard move/copy support + SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(FileHandleStat); + + /** + * Get the stat structure. + * @return The stat structure. + */ + [[nodiscard]] const struct stat & getStat() const noexcept; + + /** + * Refresh and return the stat structure. + * @return The stat structure. + */ + const struct stat & refreshStat(); + + protected: + /// The stat structure. + struct stat st; + + private: + DLL_PRIVATE void refreshStat(const std::filesystem::path & path); + }; + + /** + * Extension to FileHandle to automatically memmaps the file. + */ + class DLL_PUBLIC MemMap : public FileHandleStat { + public: + /** + * Move constructor. + */ + MemMap(MemMap &&) noexcept = default; + + /** + * Construct from an existing file descriptor. + * @param fd An open file descriptor. + * @param flags File handle flags + */ + explicit MemMap(int fd, int flags = O_RDONLY); + + /** + * Open a new file handle (with the default flags). + * @param path Path of file to open. + * @param flags File handle flags + */ + explicit MemMap(const std::filesystem::path & path, int flags = O_RDONLY); + + /** + * Open a new file handle. + * @param path Path of file to open. + * @param flags File handle flags + * @param mode File handle mode + */ + MemMap(const std::filesystem::path & path, int flags, int mode); + + ~MemMap() override; + + /// Standard move/copy support + SPECIAL_MEMBERS_COPY(MemMap, delete); + + MemMap & operator=(MemMap &&) = delete; + + /// The file data. + const void * const data; #ifdef __cpp_lib_string_view - /** - * Create a std::string_view of the mapped data. - */ - template<typename T = char> - [[nodiscard]] auto - sv() const - { - return std::basic_string_view<T>( - static_cast<const T *>(data), static_cast<std::string_view::size_type>(st.st_size) / sizeof(T)); - } + /** + * Create a std::string_view of the mapped data. + */ + template<typename T = char> + [[nodiscard]] auto + sv() const + { + return std::basic_string_view<T>( + static_cast<const T *>(data), static_cast<std::string_view::size_type>(st.st_size) / sizeof(T)); + } #endif - private: - [[nodiscard]] DLL_PRIVATE void * setupMapInt(int flags) const; - [[nodiscard]] DLL_PRIVATE void * setupMap(int flags) const; - [[nodiscard]] DLL_PRIVATE void * setupMap(const std::filesystem::path & path, int flags) const; - }; - } + private: + [[nodiscard]] DLL_PRIVATE void * setupMapInt(int flags) const; + [[nodiscard]] DLL_PRIVATE void * setupMap(int flags) const; + [[nodiscard]] DLL_PRIVATE void * setupMap(const std::filesystem::path & path, int flags) const; + }; } #endif diff --git a/libadhocutil/lexer-regex.h b/libadhocutil/lexer-regex.h index 1d1e80c..3b8ca04 100644 --- a/libadhocutil/lexer-regex.h +++ b/libadhocutil/lexer-regex.h @@ -13,18 +13,16 @@ #include <glibmm/ustring.h> #pragma GCC diagnostic pop -namespace AdHoc { - namespace LexerMatchers { - /** - * Create a AdHoc::Lexer pattern matcher using regexen. - * @param regex The regex string. - * @param compile The regex compile flags. - * @param match The regex match flags. - * @return Pointer to the newly created pattern matcher. - */ - DLL_PUBLIC Lexer::PatternPtr regex( - const Glib::ustring & regex, GRegexCompileFlags compile = {}, GRegexMatchFlags match = {}); - } +namespace AdHoc::LexerMatchers { + /** + * Create a AdHoc::Lexer pattern matcher using regexen. + * @param regex The regex string. + * @param compile The regex compile flags. + * @param match The regex match flags. + * @return Pointer to the newly created pattern matcher. + */ + DLL_PUBLIC Lexer::PatternPtr regex( + const Glib::ustring & regex, GRegexCompileFlags compile = {}, GRegexMatchFlags match = {}); } #endif diff --git a/libadhocutil/processPipes.h b/libadhocutil/processPipes.h index 130f9a1..8eee53f 100644 --- a/libadhocutil/processPipes.h +++ b/libadhocutil/processPipes.h @@ -9,54 +9,49 @@ #include <utility> #include <vector> -namespace AdHoc { - - namespace System { - - /// Spawn a process and attach to its IO handles. - class DLL_PUBLIC ProcessPipes { - public: - /** - * Spawn a new process, providing (optional) access to its stdin, stdout and - * stderr file handles. - * @param args path and arguments to spawn (passed to execv) - * @param in Attach to stdin? - * @param out Attach to stdout? - * @param err Attach to stderr? - */ - ProcessPipes(const std::vector<std::string> & args, bool in, bool out, bool err); - - /// Close input pipe to process - int closeIn(); - - /** FD handle to child's stdin. */ - [[nodiscard]] int fdIn() const noexcept; - /** FD handle to child's stdout. */ - [[nodiscard]] int fdOut() const noexcept; - /** FD handle to child's stderr. */ - [[nodiscard]] int fdError() const noexcept; - /** Process id of child. */ - [[nodiscard]] pid_t pid() const noexcept; - - private: - using PipePair = std::pair<int, int>; - using InitPipe = std::optional<PipePair>; - - ProcessPipes(const std::vector<std::string> & args, InitPipe &&, InitPipe &&, InitPipe &&); - - static InitPipe pipeSetup(bool setup, bool swap); - static void closeChild(const InitPipe & child) noexcept; - static void dupChild(int, const InitPipe & child) noexcept; - static void closeAllOpenFiles() noexcept; - - using FHandle = ::AdHoc::Handle<int, int (*)(int)>; - using OFHandle = std::optional<FHandle>; - const pid_t child; - OFHandle in; - const OFHandle out, error; - }; - - } +namespace AdHoc::System { + /// Spawn a process and attach to its IO handles. + class DLL_PUBLIC ProcessPipes { + public: + /** + * Spawn a new process, providing (optional) access to its stdin, stdout and + * stderr file handles. + * @param args path and arguments to spawn (passed to execv) + * @param in Attach to stdin? + * @param out Attach to stdout? + * @param err Attach to stderr? + */ + ProcessPipes(const std::vector<std::string> & args, bool in, bool out, bool err); + + /// Close input pipe to process + int closeIn(); + + /** FD handle to child's stdin. */ + [[nodiscard]] int fdIn() const noexcept; + /** FD handle to child's stdout. */ + [[nodiscard]] int fdOut() const noexcept; + /** FD handle to child's stderr. */ + [[nodiscard]] int fdError() const noexcept; + /** Process id of child. */ + [[nodiscard]] pid_t pid() const noexcept; + + private: + using PipePair = std::pair<int, int>; + using InitPipe = std::optional<PipePair>; + + ProcessPipes(const std::vector<std::string> & args, InitPipe &&, InitPipe &&, InitPipe &&); + + static InitPipe pipeSetup(bool setup, bool swap); + static void closeChild(const InitPipe & child) noexcept; + static void dupChild(int, const InitPipe & child) noexcept; + static void closeAllOpenFiles() noexcept; + + using FHandle = ::AdHoc::Handle<int, int (*)(int)>; + using OFHandle = std::optional<FHandle>; + const pid_t child; + OFHandle in; + const OFHandle out, error; + }; } #endif diff --git a/libadhocutil/runtimeContext.h b/libadhocutil/runtimeContext.h index 64ee1e2..08c6614 100644 --- a/libadhocutil/runtimeContext.h +++ b/libadhocutil/runtimeContext.h @@ -7,45 +7,43 @@ #include <ucontext.h> #include <vector> -namespace AdHoc { - namespace System { - - /// Runtime Context +namespace AdHoc::System { + + /// Runtime Context + /** + * Create an alternate stack for processing. + */ + class DLL_PUBLIC RuntimeContext { + public: /** - * Create an alternate stack for processing. + * Create a new RuntimeContent + * @param stacksize The size in bytes of the new stack. */ - class DLL_PUBLIC RuntimeContext { - public: - /** - * Create a new RuntimeContent - * @param stacksize The size in bytes of the new stack. - */ - explicit RuntimeContext(size_t stacksize = 16384); - virtual ~RuntimeContext() = default; - /// Standard move/copy support - SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(RuntimeContext); - - /** Swap to/from the contained stack. */ - void swapContext(); - - /** Has the callback on the contained stack run to completion? */ - [[nodiscard]] bool hasCompleted() const; - - protected: - /** Overridden in a sub-class to implement functionality in the alternate stack */ - DLL_PRIVATE virtual void callback() = 0; - - private: - DLL_PRIVATE static void callbackWrapper(RuntimeContext * rc); - - std::vector<char> stack; - ucontext_t ctxInitial {}; - ucontext_t ctxCallback {}; - bool completed {false}; - bool swapped {false}; - }; - - } + explicit RuntimeContext(size_t stacksize = 16384); + virtual ~RuntimeContext() = default; + /// Standard move/copy support + SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(RuntimeContext); + + /** Swap to/from the contained stack. */ + void swapContext(); + + /** Has the callback on the contained stack run to completion? */ + [[nodiscard]] bool hasCompleted() const; + + protected: + /** Overridden in a sub-class to implement functionality in the alternate stack */ + DLL_PRIVATE virtual void callback() = 0; + + private: + DLL_PRIVATE static void callbackWrapper(RuntimeContext * rc); + + std::vector<char> stack; + ucontext_t ctxInitial {}; + ucontext_t ctxCallback {}; + bool completed {false}; + bool swapped {false}; + }; + } #endif |