Skip to content

Instantly share code, notes, and snippets.

@dotnwat
Created September 3, 2019 00:31
Show Gist options
  • Save dotnwat/86137e460c792f279b8e246a096c828f to your computer and use it in GitHub Desktop.
Save dotnwat/86137e460c792f279b8e246a096c828f to your computer and use it in GitHub Desktop.
// clang++ -std=c++17 -D_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR \
// -DNO_CONCEPTS -O0 -gsplit-dwarf -Wall -Werror -Wno-unused-variable
// -Wno-unused-function -Wno-unused-lambda-capture -std=c++17 -U_FORTIFY_SOURCE
// -fsanitize=address -fsanitize=undefined -fno-sanitize=vptr -std=gnu++17 \
// main.cc -o main
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
namespace YAML {
class ostream_wrapper {
public:
ostream_wrapper();
explicit ostream_wrapper(std::ostream& stream);
~ostream_wrapper();
void write(const std::string& str);
void write(const char* str, std::size_t size);
void set_comment() { m_comment = true; }
const char* str() const {
if (m_pStream) {
return 0;
} else {
m_buffer[m_pos] = '\0';
return &m_buffer[0];
}
}
std::size_t row() const { return m_row; }
std::size_t col() const { return m_col; }
std::size_t pos() const { return m_pos; }
bool comment() const { return m_comment; }
private:
void update_pos(char ch);
private:
mutable std::vector<char> m_buffer;
std::ostream* const m_pStream;
std::size_t m_pos;
std::size_t m_row, m_col;
bool m_comment;
};
template <std::size_t N>
inline ostream_wrapper& operator<<(ostream_wrapper& stream,
const char(&str)[N]) {
stream.write(str, N - 1);
return stream;
}
inline ostream_wrapper& operator<<(ostream_wrapper& stream,
const std::string& str) {
stream.write(str);
return stream;
}
inline ostream_wrapper& operator<<(ostream_wrapper& stream, char ch) {
stream.write(&ch, 1);
return stream;
}
}
namespace YAML {
ostream_wrapper::ostream_wrapper()
: m_buffer(1, '\0'),
m_pStream(0),
m_pos(0),
m_row(0),
m_col(0),
m_comment(false) {}
ostream_wrapper::ostream_wrapper(std::ostream& stream)
: m_pStream(&stream), m_pos(0), m_row(0), m_col(0), m_comment(false) {}
ostream_wrapper::~ostream_wrapper() {}
void ostream_wrapper::write(const std::string& str) {
if (m_pStream) {
m_pStream->write(str.c_str(), str.size());
} else {
m_buffer.resize(std::max(m_buffer.size(), m_pos + str.size() + 1));
std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos);
}
for (std::size_t i = 0; i < str.size(); i++) {
update_pos(str[i]);
}
}
void ostream_wrapper::write(const char* str, std::size_t size) {
if (m_pStream) {
m_pStream->write(str, size);
} else {
m_buffer.resize(std::max(m_buffer.size(), m_pos + size + 1));
std::copy(str, str + size, m_buffer.begin() + m_pos);
}
for (std::size_t i = 0; i < size; i++) {
update_pos(str[i]);
}
}
void ostream_wrapper::update_pos(char ch) {
m_pos++;
m_col++;
if (ch == '\n') {
m_row++;
m_col = 0;
m_comment = false;
}
}
}
int main(int argc, char** argv, char** env) {
auto f = YAML::ostream_wrapper();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment