MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ insert_include() [2/2]

bool MayaFlux::Reflect::Tool::insert_include ( const std::filesystem::path & header,
unsigned last_marker,
std::string_view suffix,
bool & inserted,
std::string & error )
nodiscard

insert_include() for any marker, given the header, the line of its last marker and the sidecar suffix.

Parameters
last_markerThe line after which the include may appear.

Definition at line 50 of file Sidecar.cpp.

52{
53 inserted = false;
54 const std::string file = sidecar_path(header, suffix).filename().string();
55 auto text = read_text(header);
56 if (!text) {
57 error = "cannot read " + header.string();
58 return false;
59 }
60 if (!include_lines(*text, file + "\"").empty()) {
61 return true;
62 }
63
64 const std::string newline = text->find("\r\n") != std::string::npos ? "\r\n" : "\n";
65 const std::string line = "#include \"" + file + "\"" + newline;
66
67 std::size_t offset = 0;
68 for (unsigned i = 0; i < last_marker && offset != std::string::npos; ++i) {
69 const auto next = text->find('\n', offset);
70 offset = next == std::string::npos ? std::string::npos : next + 1;
71 }
72 if (offset == std::string::npos) {
73 *text += newline + line;
74 } else {
75 text->insert(offset, line);
76 }
77
78 if (!write_if_changed(header, *text, error)) {
79 return false;
80 }
81 inserted = true;
82 return true;
83}
std::optional< std::string > read_text(const std::filesystem::path &path)
The whole content of path, or nullopt when it cannot be read.
Definition Sidecar.cpp:30
std::filesystem::path sidecar_path(const std::filesystem::path &header)
Path of the sidecar for header: Name.hpp becomes Name.describe.hpp beside it.
Definition Sidecar.cpp:85
bool write_if_changed(const std::filesystem::path &path, const std::string &content, std::string &error)
Replaces path with content only when the content differs.
Definition Sidecar.cpp:137

References read_text(), sidecar_path(), and write_if_changed().

Here is the call graph for this function: