#pragma once #include "defines.hpp" #include template class CMaxLengthCString { public: CMaxLengthCString() { m_str[0] = '\0'; } void operator+=(char const* rhs) { write(rhs, strlen(rhs)); } void write(char const* data, size_t len) { if (m_boundsExceeded || m_strPos + len >= N) { m_boundsExceeded = true; return; } memcpy(m_str + m_strPos, data, len); m_strPos += len; m_str[m_strPos] = '\0'; } void write(char c) { if (m_boundsExceeded || m_strPos + 1 >= N) { m_boundsExceeded = true; return; } m_str[m_strPos] = c; m_strPos++; } void writeNum(size_t num) { size_t d = 1; while (num / 10 >= d) d *= 10; while (num > 0) { char c = '0' + (num / d); write(c); num %= d; d /= 10; } } char const* getStr() { return m_str; }; bool boundsExceeded() { return m_boundsExceeded; }; private: char m_str[N]; size_t m_strPos = 0; bool m_boundsExceeded = false; }; template class CBufFileWriter { public: CBufFileWriter(int fd_) : m_fd(fd_) {} ~CBufFileWriter() { flush(); } void write(char const* data, size_t len) { while (len > 0) { size_t to_add = std::min(len, (size_t)BUFSIZE - m_writeBufPos); memcpy(m_writeBuf + m_writeBufPos, data, to_add); data += to_add; len -= to_add; m_writeBufPos += to_add; if (m_writeBufPos == BUFSIZE) flush(); } } void write(char c) { if (m_writeBufPos == BUFSIZE) flush(); m_writeBuf[m_writeBufPos] = c; m_writeBufPos++; } void operator+=(char const* str) { write(str, strlen(str)); } void operator+=(std::string_view str) { write(str.data(), str.size()); } void operator+=(char c) { write(c); } void writeNum(size_t num) { size_t d = 1; while (num / 10 >= d) d *= 10; while (num > 0) { char c = '0' + (num / d); write(c); num %= d; d /= 10; } } void writeCmdOutput(const char* cmd) { int pipefd[2]; if (pipe(pipefd) < 0) { *this += " failmsg(pipefd[1]); failmsg += " 0) { write(readbuf, len); } if (len < 0) { *this += "