Files
2026-06-09 00:22:22 -05:00

43 lines
1.2 KiB
C++

#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include "sedpp/Options.h"
namespace sedpp {
// A loaded input record is sed's starting pattern space for one cycle. The
// delimiter bit is carried separately so files without a trailing newline remain
// distinguishable all the way to output.
struct Record {
std::string text;
std::string fileName;
bool hadDelimiter = false;
bool lastInFile = false;
bool lastOverall = false;
std::uint64_t lineNumber = 0;
std::uint64_t fileLineNumber = 0;
};
// Reads stdin or input files eagerly into records. Eager loading simplifies GNU
// sed's $ address and in-place rewrite behavior, both of which need to know the
// last record before the runner starts emitting final output.
class InputLoader {
public:
explicit InputLoader(const Options &options);
[[nodiscard]] std::vector<Record> loadAll() const;
[[nodiscard]] std::vector<Record> loadFile(const std::string &fileName) const;
private:
[[nodiscard]] std::vector<Record> splitBuffer(const std::string &buffer,
const std::string &fileName,
std::uint64_t &globalLine) const;
const Options &options_;
};
} // namespace sedpp