diff --git a/avogadro/quantumio/genericoutput.cpp b/avogadro/quantumio/genericoutput.cpp new file mode 100644 index 0000000000..812c5cca1e --- /dev/null +++ b/avogadro/quantumio/genericoutput.cpp @@ -0,0 +1,96 @@ +/****************************************************************************** + This source file is part of the Avogadro project. + This source code is released under the 3-Clause BSD License, (see "LICENSE"). +******************************************************************************/ + +#include "genericoutput.h" + +#include +#include + +#include "gamessus.h" +#include "nwchemlog.h" +#include "orca.h" + +#include + +namespace Avogadro::QuantumIO { + +GenericOutput::GenericOutput() {} + +GenericOutput::~GenericOutput() {} + +std::vector GenericOutput::fileExtensions() const +{ + std::vector extensions; + extensions.emplace_back("out"); + extensions.emplace_back("output"); + extensions.emplace_back("log"); + return extensions; +} + +std::vector GenericOutput::mimeTypes() const +{ + return std::vector(); +} + +bool GenericOutput::read(std::istream& in, Core::Molecule& molecule) +{ + // check the stream line-by-line until we see the program name + FileFormat* reader = nullptr; + + std::string line; + while (std::getline(in, line)) { + if (line.find("Northwest Computational Chemistry Package") != + std::string::npos) { + // NWChem + reader = new NWChemLog; + break; + } else if (line.find("GAMESS VERSION") != std::string::npos) { + // GAMESS-US .. don't know if we can read Firefly or GAMESS-UK + reader = new GAMESSUSOutput; + break; + } else if (line.find("O R C A") != std::string::npos) { + // ORCA reader + reader = new ORCAOutput; + break; + } + } + + // if we didn't find a program name, check for cclib or OpenBabel + // prefer cclib if it's available + if (reader == nullptr) { + // check what output is available + std::vector readers = + Io::FileFormatManager::instance().fileFormatsFromFileExtension( + "out", FileFormat::File | FileFormat::Read); + + // loop through writers to check for "cclib" or "Open Babel" + for (const FileFormat* r : readers) { + if (r->name() == "cclib") { + reader = r->newInstance(); + break; + if (r->identifier().compare(0, 9, "OpenBabel") == 0) { + reader = r->newInstance(); + break; + } + } + } + + // rewind the stream + in.seekg(0, std::ios::beg); + in.clear(); + + if (reader) { + bool success = reader->readFile(fileName(), molecule); + delete reader; + return success; + } else { + appendError( + "Could not determine the program used to generate this output file."); + delete reader; + return false; + } + } + +} // namespace Avogadro::QuantumIO diff --git a/avogadro/quantumio/genericoutput.h b/avogadro/quantumio/genericoutput.h new file mode 100644 index 0000000000..86463db8ae --- /dev/null +++ b/avogadro/quantumio/genericoutput.h @@ -0,0 +1,50 @@ +/****************************************************************************** + This source file is part of the Avogadro project. + This source code is released under the 3-Clause BSD License, (see "LICENSE"). +******************************************************************************/ + +#ifndef AVOGADRO_QUANTUMIO_GENERICOUTPUT_H +#define AVOGADRO_QUANTUMIO_GENERICOUTPUT_H + +#include "avogadroquantumioexport.h" +#include + +#include +#include + +namespace Avogadro { +namespace QuantumIO { + +class AVOGADROQUANTUMIO_EXPORT GenericOutput : public Io::FileFormat +{ +public: + GenericOutput(); + ~GenericOutput() override; + + Operations supportedOperations() const override + { + return Read | File | Stream | String; + } + + FileFormat* newInstance() const override { return new GenericOutput; } + std::string identifier() const override { return "Avogadro: Generic Output"; } + std::string name() const override { return "Generic Output"; } + std::string description() const override { return "Generic output format."; } + + std::string specificationUrl() const override { return ""; } + + std::vector fileExtensions() const override; + std::vector mimeTypes() const override; + + bool read(std::istream& in, Core::Molecule& molecule) override; + bool write(std::ostream&, const Core::Molecule&) override + { + // Empty, as we do not write output files. + return false; + } +}; + +} // namespace QuantumIO +} // namespace Avogadro + +#endif