-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 gucc: move some functions into library
- Loading branch information
1 parent
70773e8
commit 6a20590
Showing
29 changed files
with
431 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
cmake_minimum_required(VERSION 3.6) | ||
|
||
## | ||
## PROJECT | ||
## name and version | ||
## | ||
project(gucc | ||
VERSION 0.0.1 | ||
LANGUAGES CXX) | ||
|
||
add_library(${PROJECT_NAME} SHARED | ||
#src/utils.cpp src/utils.hpp | ||
src/string_utils.cpp src/string_utils.hpp | ||
src/file_utils.cpp src/file_utils.hpp | ||
src/cpu.cpp src/cpu.hpp | ||
src/pacmanconf_repo.cpp src/pacmanconf_repo.hpp | ||
src/initcpio.cpp src/initcpio.hpp | ||
#src/chwd_profiles.cpp src/chwd_profiles.hpp | ||
#src/disk.cpp src/disk.hpp | ||
) | ||
|
||
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) | ||
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_DIR}/src) | ||
target_link_libraries(${PROJECT_NAME} PUBLIC project_warnings project_options spdlog::spdlog fmt::fmt range-v3::range-v3) | ||
|
||
if(COS_INSTALLER_BUILD_TESTS) | ||
add_subdirectory(tests) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# what |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "file_utils.hpp" | ||
|
||
#include <cerrno> // for errno, strerror | ||
#include <cstdio> // for feof, fgets, pclose, perror, popen | ||
#include <cstdlib> // for exit, WIFEXITED, WIFSIGNALED | ||
|
||
#include <fstream> // for ofstream | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
namespace gucc::file_utils { | ||
|
||
auto read_whole_file(const std::string_view& filepath) noexcept -> std::string { | ||
// Use std::fopen because it's faster than std::ifstream | ||
auto* file = std::fopen(filepath.data(), "rb"); | ||
if (file == nullptr) { | ||
spdlog::error("[READWHOLEFILE] '{}' read failed: {}", filepath, std::strerror(errno)); | ||
return {}; | ||
} | ||
|
||
std::fseek(file, 0u, SEEK_END); | ||
const auto size = static_cast<std::size_t>(std::ftell(file)); | ||
std::fseek(file, 0u, SEEK_SET); | ||
|
||
std::string buf; | ||
buf.resize(size); | ||
|
||
const std::size_t read = std::fread(buf.data(), sizeof(char), size, file); | ||
if (read != size) { | ||
spdlog::error("[READWHOLEFILE] '{}' read failed: {}", filepath, std::strerror(errno)); | ||
return {}; | ||
} | ||
std::fclose(file); | ||
|
||
return buf; | ||
} | ||
|
||
bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept { | ||
std::ofstream file{filepath.data()}; | ||
if (!file.is_open()) { | ||
spdlog::error("[WRITE_TO_FILE] '{}' open failed: {}", filepath, std::strerror(errno)); | ||
return false; | ||
} | ||
file << data; | ||
return true; | ||
} | ||
|
||
} // namespace gucc::file_utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef FILE_UTILS_HPP | ||
#define FILE_UTILS_HPP | ||
|
||
#include <string> // for string | ||
#include <string_view> // for string_view | ||
|
||
namespace gucc::file_utils { | ||
|
||
auto read_whole_file(const std::string_view& filepath) noexcept -> std::string; | ||
bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept; | ||
|
||
} // namespace gucc::file_utils | ||
|
||
#endif // FILE_UTILS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "string_utils.hpp" | ||
|
||
#if defined(__clang__) | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wold-style-cast" | ||
#elif defined(__GNUC__) | ||
#pragma GCC diagnostic push | ||
#pragma GCC diagnostic ignored "-Wuseless-cast" | ||
#pragma GCC diagnostic ignored "-Wold-style-cast" | ||
#endif | ||
|
||
#include <range/v3/algorithm/for_each.hpp> | ||
#include <range/v3/algorithm/reverse.hpp> | ||
#include <range/v3/range/conversion.hpp> | ||
#include <range/v3/view/join.hpp> | ||
|
||
#if defined(__clang__) | ||
#pragma clang diagnostic pop | ||
#elif defined(__GNUC__) | ||
#pragma GCC diagnostic pop | ||
#endif | ||
|
||
namespace gucc::utils { | ||
|
||
auto make_multiline(std::string_view str, bool reverse, char delim) noexcept -> std::vector<std::string> { | ||
std::vector<std::string> lines{}; | ||
ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); }); | ||
if (reverse) { | ||
ranges::reverse(lines); | ||
} | ||
return lines; | ||
} | ||
|
||
auto make_multiline_view(std::string_view str, bool reverse, char delim) noexcept -> std::vector<std::string_view> { | ||
std::vector<std::string_view> lines{}; | ||
ranges::for_each(utils::make_split_view(str, delim), [&](auto&& rng) { lines.emplace_back(rng); }); | ||
if (reverse) { | ||
ranges::reverse(lines); | ||
} | ||
return lines; | ||
} | ||
|
||
auto make_multiline(const std::vector<std::string>& multiline, bool reverse, std::string_view delim) noexcept -> std::string { | ||
std::string res{}; | ||
for (const auto& line : multiline) { | ||
res += line; | ||
res += delim.data(); | ||
} | ||
|
||
if (reverse) { | ||
ranges::reverse(res); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
auto join(const std::vector<std::string>& lines, std::string_view delim) noexcept -> std::string { | ||
return lines | ranges::views::join(delim) | ranges::to<std::string>(); | ||
} | ||
|
||
} // namespace gucc::utils |
Oops, something went wrong.