6#ifndef NURI_FMT_BASE_H_
7#define NURI_FMT_BASE_H_
20#include <absl/base/attributes.h>
21#include <absl/base/optimization.h>
22#include <absl/log/absl_log.h>
32template <
class Reader = MoleculeReader>
50 if (!reader_->getnext(block_)) {
54 mol_ = reader_->parse(block_);
76 std::vector<std::string> block_;
80template <
class Stream>
82 if (stream.advance()) {
83 mol = std::move(stream.current());
102 std::vector<std::
string>
next() {
103 std::vector<std::string> block;
122 ABSL_MUST_USE_RESULT
virtual bool
145template <auto parser>
152 return parser(block);
205 const std::vector<std::
string> &names);
223 register_for_name(
this, alias);
228 std::string_view name);
231template <
class ReaderFactoryImpl>
234 std::make_unique<ReaderFactoryImpl>(), names);
237template <
class MoleculeReaderImpl>
240 std::unique_ptr<MoleculeReader>
from_stream(std::istream &is)
const final {
241 return std::make_unique<MoleculeReaderImpl>(is);
245template <
class SourceStream,
class Reader = MoleculeReader>
248 template <
class... Args>
250 : is_(std::forward<Args>(args)...) {
254 if (ABSL_PREDICT_FALSE(factory ==
nullptr)) {
255 ABSL_LOG(WARNING) <<
"No factory found for " << fmt;
259 reader_ = static_unique_ptr_cast<Reader>(factory->
from_stream(is_));
267 std::vector<std::string>
next() {
return reader_->next(); }
277 bool getnext(std::vector<std::string> &block) {
278 return reader_->getnext(block);
288 return reader_->parse(block);
293 operator bool()
const {
return is_ && reader_; }
297 std::unique_ptr<Reader> reader_;
300template <
class Reader>
304 const std::filesystem::path full_ext = path.extension();
305 const std::string_view ext = extension_no_dot(full_ext);
310 if (ABSL_PREDICT_FALSE(factory ==
nullptr)) {
311 ABSL_LOG(WARNING) <<
"No factory found for " << ext;
315 reader_ = static_unique_ptr_cast<Reader>(factory->
from_stream(is_));
323 if (ABSL_PREDICT_FALSE(factory ==
nullptr)) {
324 ABSL_LOG(WARNING) <<
"No factory found for " << fmt;
328 reader_ = static_unique_ptr_cast<Reader>(factory->
from_stream(is_));
336 std::vector<std::string>
next() {
return reader_->next(); }
346 bool getnext(std::vector<std::string> &block) {
347 return reader_->getnext(block);
357 return reader_->parse(block);
362 operator bool()
const {
return is_ && reader_; }
366 std::unique_ptr<Reader> reader_;
369template <
class Reader = MoleculeReader>
372template <
class Reader = MoleculeReader>
378 : is_(&is), delim_(delim), buf_(bufsz) {
392 internal::DumbBuffer<char> buf_;
403 extern std::string ascii_safe(std::string_view str);
412 extern std::string ascii_newline_safe(std::string_view str);
std::unique_ptr< MoleculeReader > from_stream(std::istream &is) const final
Create a new reader from the given istream object.
Definition base.h:240
DefaultReaderImpl(std::istream &is)
Definition base.h:149
DefaultReaderImpl()=default
Molecule parse(const std::vector< std::string > &block) const final
Parse the current block and return the molecule.
Definition base.h:151
std::istream * is_
Definition base.h:157
static bool register_factory(std::unique_ptr< MoleculeReaderFactory > factory, const std::vector< std::string > &names)
Register the factory for the given format name(s).
MoleculeReaderFactory(const MoleculeReaderFactory &)=default
virtual std::unique_ptr< MoleculeReader > from_stream(std::istream &is) const =0
Create a new reader from the given istream object.
MoleculeReaderFactory(MoleculeReaderFactory &&) noexcept=default
MoleculeReaderFactory()=default
MoleculeReaderFactory & operator=(const MoleculeReaderFactory &)=default
void register_for(std::string_view alias) const
Register this factory for the given alias name.
Definition base.h:222
static const MoleculeReaderFactory * find_factory(std::string_view name)
Find the factory for the given format name.
MoleculeReaderWrapper(const std::filesystem::path &path)
Definition base.h:303
MoleculeStream< Reader > stream()
Definition base.h:360
Molecule parse(const std::vector< std::string > &block) const
Parse the current block and return the molecule.
Definition base.h:356
std::vector< std::string > next()
Advance the stream to the next molecule.
Definition base.h:336
MoleculeReaderWrapper(std::string_view fmt, const std::filesystem::path &path)
Definition base.h:318
bool getnext(std::vector< std::string > &block)
Advance the reader to the next molecule.
Definition base.h:346
std::vector< std::string > next()
Advance the stream to the next molecule.
Definition base.h:267
MoleculeReaderWrapper(std::string_view fmt, Args &&...args)
Definition base.h:249
bool getnext(std::vector< std::string > &block)
Advance the reader to the next molecule.
Definition base.h:277
Molecule parse(const std::vector< std::string > &block) const
Parse the current block and return the molecule.
Definition base.h:287
MoleculeStream< Reader > stream()
Definition base.h:291
MoleculeReader(const MoleculeReader &)=delete
MoleculeReader & operator=(const MoleculeReader &)=delete
std::vector< std::string > next()
Advance the reader to the next molecule.
Definition base.h:102
virtual ABSL_MUST_USE_RESULT bool getnext(std::vector< std::string > &block)=0
Advance the reader to the next molecule.
virtual bool bond_valid() const =0
Test whether the reader implementation can provide valid bond information.
MoleculeReader(MoleculeReader &&) noexcept=default
virtual Molecule parse(const std::vector< std::string > &block) const =0
Parse the current block and return the molecule.
MoleculeStream< MoleculeReader > stream()
Convert the reader to a stream object.
Definition base.h:142
const Molecule & current() const
Get the current molecule.
Definition base.h:72
MoleculeStream(const MoleculeStream &)=delete
MoleculeStream(MoleculeStream &&) noexcept=default
Molecule & current()
Get the current molecule.
Definition base.h:64
ABSL_MUST_USE_RESULT bool advance()
Advance the stream to the next molecule.
Definition base.h:49
MoleculeStream & operator=(const MoleculeStream &)=delete
MoleculeStream(Reader &reader)
Definition base.h:35
Read-only molecule class.
Definition molecule.h:943
ReversedStream(std::istream &is, char delim='\n', std::size_t bufsz=4096)
Definition base.h:377
bool getline(std::string &line)
bool register_reader_factory(const std::vector< std::string > &names)
Definition base.h:232
MoleculeReaderWrapper< std::ifstream, Reader > FileMoleculeReader
Definition base.h:370
Stream & operator>>(Stream &stream, Molecule &mol)
Definition base.h:81
MoleculeReaderWrapper< std::istringstream, Reader > StringMoleculeReader
Definition base.h:373