From d15441a30e910f34332c3498fd347e94eb9c7deb Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Sun, 7 Jan 2024 09:20:03 +0200 Subject: [PATCH] common files used by crash printer --- crash_printer/common.cpp | 38 ++++++++++++++++++++++++++ crash_printer/crash_printer/common.hpp | 18 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 crash_printer/common.cpp create mode 100644 crash_printer/crash_printer/common.hpp diff --git a/crash_printer/common.cpp b/crash_printer/common.cpp new file mode 100644 index 00000000..b88179a9 --- /dev/null +++ b/crash_printer/common.cpp @@ -0,0 +1,38 @@ +#include "crash_printer/common.hpp" +#include +#include + +static bool create_dir_impl(std::filesystem::path &dirpath) +{ + if (std::filesystem::is_directory(dirpath)) + { + return true; + } + else if (std::filesystem::exists(dirpath)) // a file, we can't do anything + { + return false; + } + + return std::filesystem::create_directories(dirpath); +} + +bool crash_printer::create_dir(const std::string &filepath) +{ + std::filesystem::path dirpath = std::filesystem::path(filepath).parent_path(); + return create_dir_impl(dirpath); +} + +bool crash_printer::create_dir(const std::wstring &filepath) +{ + std::filesystem::path dirpath = std::filesystem::path(filepath).parent_path(); + return create_dir_impl(dirpath); +} + +void crash_printer::write(std::ofstream &file, const std::string &data) +{ + if (!file.is_open()) { + return; + } + + file << data << std::endl; +} diff --git a/crash_printer/crash_printer/common.hpp b/crash_printer/crash_printer/common.hpp new file mode 100644 index 00000000..4e4b93b5 --- /dev/null +++ b/crash_printer/crash_printer/common.hpp @@ -0,0 +1,18 @@ +#ifndef _CRASH_PRINTER_COMMON_H +#define _CRASH_PRINTER_COMMON_H + + +#include +#include + +namespace crash_printer { + +bool create_dir(const std::string &dir); + +bool create_dir(const std::wstring &dir); + +void write(std::ofstream &file, const std::string &data); + +} + +#endif // _CRASH_PRINTER_COMMON_H