42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <iosfwd>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
#include <unordered_map>
|
|
|
|
#include "sedpp/Options.h"
|
|
|
|
namespace sedpp {
|
|
|
|
// Centralizes delimiter-aware writes. Most commands emit either pattern space
|
|
// plus its original delimiter bit, or synthetic lines that always end in the
|
|
// active delimiter.
|
|
class Output {
|
|
public:
|
|
Output(const Options &options, std::ostream &stream);
|
|
|
|
void write(const std::string &text);
|
|
void writeDelimiter(bool hadDelimiter);
|
|
void writeLine(const std::string &text);
|
|
void writePattern(const std::string &pattern, bool hadDelimiter);
|
|
void writeEscaped(const std::string &pattern, bool hadDelimiter, int width);
|
|
void writeFile(const std::string &path, const std::string &text,
|
|
bool hadDelimiter);
|
|
|
|
private:
|
|
// Pending delimiters defer the trailing separator for unterminated records
|
|
// until another write proves that a separator is needed between outputs.
|
|
void writePendingDelimiter(std::ostream &target, bool &pending);
|
|
char delimiter() const;
|
|
|
|
const Options &options_;
|
|
std::ostream &stream_;
|
|
bool pendingDelimiter_ = false;
|
|
std::unordered_map<std::string, bool> pendingFileDelimiter_;
|
|
std::unordered_set<std::string> openedWriteFiles_;
|
|
};
|
|
|
|
} // namespace sedpp
|