12std::string
read_file(
const std::string& file_path) {
13 std::ifstream file(file_path);
14 if (!file.is_open()) {
15 throw std::runtime_error(
"Failed to open file: " + file_path);
17 return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
24void write_file(
const std::string& file_path,
const std::string& content) {
25 std::ofstream file(file_path);
26 if (!file.is_open()) {
27 throw std::runtime_error(
"Failed to write to file: " + file_path);
35 const std::string input_file =
"test_input.json";
36 const std::string output_file_base =
"test_output";
42 for (
bool with_whitespace : {
false,
true}) {
43 for (
bool preserve_newlines : {
false,
true}) {
48 std::string output_file = output_file_base +
49 (with_whitespace ?
"_whitespace" :
"_no_whitespace") +
50 (preserve_newlines ?
"_preserve_newlines" :
"_no_newlines") +
57 std::cout <<
"Processed with "
58 <<
"with_whitespace=" << with_whitespace <<
", "
59 <<
"preserve_newlines=" << preserve_newlines <<
"\n"
60 <<
"Result saved to: " << output_file << std::endl;
64 std::cout <<
"All tests completed successfully!" << std::endl;
66 }
catch (
const std::exception& e) {
67 std::cerr <<
"Error: " << e.what() << std::endl;
Utilities for working with JSON strings, including removing comments.
*The resulting JSON string may optionally retain whitespace or newlines *where the comments were removed **param json_string The JSON string to process *param with_whitespace If comments are replaced with equivalent whitespace *If comments are removed without leaving whitespace *param preserve_newlines If true and with_whitespace is newline characters *in comments are preserved all characters in the *comments are replaced with whitespace *return A JSON string with comments removed *std::string strip_json_comments(const std::string &json_string, bool with_whitespace=false, bool preserve_newlines=true)
std::string resolve_exec_path(const std::string &relative_path)
Resolves a relative path to absolute, based on executable location.
Utilities for working with file and directory paths, including resolving paths relative to the execut...