diff --git a/cmake/compile_definitions/common.cmake b/cmake/compile_definitions/common.cmake index b3e84b3e..e51dbf56 100644 --- a/cmake/compile_definitions/common.cmake +++ b/cmake/compile_definitions/common.cmake @@ -45,6 +45,8 @@ set(SUNSHINE_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/uuid.h" "${CMAKE_SOURCE_DIR}/src/config.h" "${CMAKE_SOURCE_DIR}/src/config.cpp" + "${CMAKE_SOURCE_DIR}/src/file_handler.cpp" + "${CMAKE_SOURCE_DIR}/src/file_handler.h" "${CMAKE_SOURCE_DIR}/src/logging.cpp" "${CMAKE_SOURCE_DIR}/src/logging.h" "${CMAKE_SOURCE_DIR}/src/main.cpp" diff --git a/docs/source/source_code/src/file_handler.rst b/docs/source/source_code/src/file_handler.rst new file mode 100644 index 00000000..221b8cbd --- /dev/null +++ b/docs/source/source_code/src/file_handler.rst @@ -0,0 +1,5 @@ +file_handler +============ + +.. doxygenfile:: file_handler.h + :allow-dot-graphs: diff --git a/src/config.cpp b/src/config.cpp index 9560c7ce..5cd08cee 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -15,6 +15,7 @@ #include #include "config.h" +#include "file_handler.h" #include "logging.h" #include "main.h" #include "nvhttp.h" @@ -1215,7 +1216,7 @@ namespace config { } // Read config file - auto vars = parse_config(read_file(sunshine.config_file.c_str())); + auto vars = parse_config(file_handler::read_file(sunshine.config_file.c_str())); for (auto &[name, value] : cmd_vars) { vars.insert_or_assign(std::move(name), std::move(value)); diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 37233d6e..e3a5f898 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -29,6 +29,7 @@ #include "config.h" #include "confighttp.h" #include "crypto.h" +#include "file_handler.h" #include "httpcommon.h" #include "logging.h" #include "main.h" @@ -162,7 +163,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "index.html"); + std::string content = file_handler::read_file(WEB_DIR "index.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -174,7 +175,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "pin.html"); + std::string content = file_handler::read_file(WEB_DIR "pin.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -186,7 +187,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "apps.html"); + std::string content = file_handler::read_file(WEB_DIR "apps.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); headers.emplace("Access-Control-Allow-Origin", "https://images.igdb.com/"); @@ -199,7 +200,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "clients.html"); + std::string content = file_handler::read_file(WEB_DIR "clients.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -211,7 +212,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "config.html"); + std::string content = file_handler::read_file(WEB_DIR "config.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -223,7 +224,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "password.html"); + std::string content = file_handler::read_file(WEB_DIR "password.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -236,7 +237,7 @@ namespace confighttp { send_redirect(response, request, "/"); return; } - std::string content = read_file(WEB_DIR "welcome.html"); + std::string content = file_handler::read_file(WEB_DIR "welcome.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -248,7 +249,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(WEB_DIR "troubleshooting.html"); + std::string content = file_handler::read_file(WEB_DIR "troubleshooting.html"); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/html; charset=utf-8"); response->write(content, headers); @@ -324,7 +325,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(config::stream.file_apps.c_str()); + std::string content = file_handler::read_file(config::stream.file_apps.c_str()); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "application/json"); response->write(content, headers); @@ -336,7 +337,7 @@ namespace confighttp { print_req(request); - std::string content = read_file(config::sunshine.log_file.c_str()); + std::string content = file_handler::read_file(config::sunshine.log_file.c_str()); SimpleWeb::CaseInsensitiveMultimap headers; headers.emplace("Content-Type", "text/plain"); response->write(SimpleWeb::StatusCode::success_ok, content, headers); @@ -542,7 +543,7 @@ namespace confighttp { outputTree.put("platform", SUNSHINE_PLATFORM); outputTree.put("version", PROJECT_VER); - auto vars = config::parse_config(read_file(config::sunshine.config_file.c_str())); + auto vars = config::parse_config(file_handler::read_file(config::sunshine.config_file.c_str())); for (auto &[name, value] : vars) { outputTree.put(std::move(name), std::move(value)); @@ -575,7 +576,7 @@ namespace confighttp { configStream << kv.first << " = " << value << std::endl; } - write_file(config::sunshine.config_file.c_str(), configStream.str()); + file_handler::write_file(config::sunshine.config_file.c_str(), configStream.str()); } catch (std::exception &e) { BOOST_LOG(warning) << "SaveConfig: "sv << e.what(); diff --git a/src/file_handler.cpp b/src/file_handler.cpp new file mode 100644 index 00000000..d0783431 --- /dev/null +++ b/src/file_handler.cpp @@ -0,0 +1,69 @@ +/** + * @file file_handler.cpp + * @brief File handling functions. + */ + +// standard includes +#include +#include + +// local includes +#include "file_handler.h" +#include "logging.h" + +namespace file_handler { + + /** + * @brief Read a file to string. + * @param path The path of the file. + * @return `std::string` : The contents of the file. + * + * EXAMPLES: + * ```cpp + * std::string contents = read_file("path/to/file"); + * ``` + */ + std::string + read_file(const char *path) { + if (!std::filesystem::exists(path)) { + BOOST_LOG(debug) << "Missing file: " << path; + return {}; + } + + std::ifstream in(path); + + std::string input; + std::string base64_cert; + + while (!in.eof()) { + std::getline(in, input); + base64_cert += input + '\n'; + } + + return base64_cert; + } + + /** + * @brief Writes a file. + * @param path The path of the file. + * @param contents The contents to write. + * @return `int` : `0` on success, `-1` on failure. + * + * EXAMPLES: + * ```cpp + * int write_status = write_file("path/to/file", "file contents"); + * ``` + */ + int + write_file(const char *path, const std::string_view &contents) { + std::ofstream out(path); + + if (!out.is_open()) { + return -1; + } + + out << contents; + + return 0; + } +} // namespace file_handler diff --git a/src/file_handler.h b/src/file_handler.h new file mode 100644 index 00000000..aa2387f8 --- /dev/null +++ b/src/file_handler.h @@ -0,0 +1,14 @@ +/** + * @file file_handler.h + * @brief Header file for file handling functions. + */ +#pragma once + +#include + +namespace file_handler { + std::string + read_file(const char *path); + int + write_file(const char *path, const std::string_view &contents); +} // namespace file_handler diff --git a/src/httpcommon.cpp b/src/httpcommon.cpp index 849fd81a..aa92b3bd 100644 --- a/src/httpcommon.cpp +++ b/src/httpcommon.cpp @@ -21,9 +21,9 @@ #include "config.h" #include "crypto.h" +#include "file_handler.h" #include "httpcommon.h" #include "logging.h" -#include "main.h" #include "network.h" #include "nvhttp.h" #include "platform/common.h" @@ -161,12 +161,12 @@ namespace http { return -1; } - if (write_file(pkey.c_str(), creds.pkey)) { + if (file_handler::write_file(pkey.c_str(), creds.pkey)) { BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.pkey << ']'; return -1; } - if (write_file(cert.c_str(), creds.x509)) { + if (file_handler::write_file(cert.c_str(), creds.x509)) { BOOST_LOG(error) << "Couldn't open ["sv << config::nvhttp.cert << ']'; return -1; } diff --git a/src/main.cpp b/src/main.cpp index e317e4d6..1d1bb305 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -758,57 +758,3 @@ main(int argc, char *argv[]) { return lifetime::desired_exit_code; } - -/** - * @brief Read a file to string. - * @param path The path of the file. - * @return `std::string` : The contents of the file. - * - * EXAMPLES: - * ```cpp - * std::string contents = read_file("path/to/file"); - * ``` - */ -std::string -read_file(const char *path) { - if (!std::filesystem::exists(path)) { - BOOST_LOG(debug) << "Missing file: " << path; - return {}; - } - - std::ifstream in(path); - - std::string input; - std::string base64_cert; - - while (!in.eof()) { - std::getline(in, input); - base64_cert += input + '\n'; - } - - return base64_cert; -} - -/** - * @brief Writes a file. - * @param path The path of the file. - * @param contents The contents to write. - * @return `int` : `0` on success, `-1` on failure. - * - * EXAMPLES: - * ```cpp - * int write_status = write_file("path/to/file", "file contents"); - * ``` - */ -int -write_file(const char *path, const std::string_view &contents) { - std::ofstream out(path); - - if (!out.is_open()) { - return -1; - } - - out << contents; - - return 0; -} diff --git a/src/main.h b/src/main.h index ba726539..02a21fd3 100644 --- a/src/main.h +++ b/src/main.h @@ -26,10 +26,6 @@ extern bool display_cursor; // functions int main(int argc, char *argv[]); -std::string -read_file(const char *path); -int -write_file(const char *path, const std::string_view &contents); void launch_ui(); void diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 6fb7ab36..312c68fc 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -22,6 +22,7 @@ // local includes #include "config.h" #include "crypto.h" +#include "file_handler.h" #include "httpcommon.h" #include "logging.h" #include "main.h" @@ -1005,8 +1006,8 @@ namespace nvhttp { load_state(); } - conf_intern.pkey = read_file(config::nvhttp.pkey.c_str()); - conf_intern.servercert = read_file(config::nvhttp.cert.c_str()); + conf_intern.pkey = file_handler::read_file(config::nvhttp.pkey.c_str()); + conf_intern.servercert = file_handler::read_file(config::nvhttp.cert.c_str()); crypto::cert_chain_t cert_chain; for (auto &[_, client] : map_id_client) { diff --git a/src/platform/linux/graphics.cpp b/src/platform/linux/graphics.cpp index ce0359a9..b4129889 100644 --- a/src/platform/linux/graphics.cpp +++ b/src/platform/linux/graphics.cpp @@ -3,8 +3,8 @@ * @brief todo */ #include "graphics.h" +#include "src/file_handler.h" #include "src/logging.h" -#include "src/main.h" #include "src/video.h" #include @@ -780,7 +780,7 @@ namespace egl { for (int x = 0; x < count; ++x) { auto &compiled_source = compiled_sources[x]; - compiled_source = gl::shader_t::compile(read_file(sources[x]), shader_type[x % 2]); + compiled_source = gl::shader_t::compile(file_handler::read_file(sources[x]), shader_type[x % 2]); gl_drain_errors; if (compiled_source.has_right()) {