Debian Codegraph @

avogadro/quantumio/genericoutput.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "genericoutput.h"
7
8 #include <avogadro/io/fileformat.h>
9 #include <avogadro/io/fileformatmanager.h>
10 #include <avogadro/io/xyzformat.h>
11
12 #include "gamessus.h"
13 #include "molden.h"
14 #include "nwchemlog.h"
15 #include "orca.h"
16
17 #include <iostream>
18
19 namespace Avogadro::QuantumIO {
20
21 GenericOutput::GenericOutput() {}
22
23 GenericOutput::~GenericOutput() {}
24
25 std::vector<std::string> GenericOutput::fileExtensions() const
26 {
27 std::vector<std::string> extensions;
28 extensions.emplace_back("out");
29 extensions.emplace_back("output");
30 extensions.emplace_back("log");
31 return extensions;
32 }
33
34 std::vector<std::string> GenericOutput::mimeTypes() const
35 {
36 return std::vector<std::string>();
37 }
38
39 bool GenericOutput::read(std::istream& in, Core::Molecule& molecule)
40 {
41 // check the stream line-by-line until we see the program name
42 FileFormat* reader = nullptr;
43
44 std::string line;
45 while (std::getline(in, line)) {
46 if (line.find("Northwest Computational Chemistry Package") !=
47 std::string::npos) {
48 // NWChem
49 reader = new NWChemLog;
50 break;
51 } else if (line.find("GAMESS VERSION") != std::string::npos) {
52 // GAMESS-US .. don't know if we can read Firefly or GAMESS-UK
53 reader = new GAMESSUSOutput;
54 break;
55 } else if (line.find("[Molden Format]") != std::string::npos) {
56 // molden with .out extension
57 reader = new MoldenFile;
58 break;
59 } else if (line.find("O R C A") != std::string::npos) {
60 // ORCA reader
61 reader = new ORCAOutput;
62 break;
63 } else if (line.find("xtb:") != std::string::npos) {
64 // xtb reader
65 reader = new Io::XyzFormat;
66 break;
67 }
68 }
69
70 // if we didn't find a program name, check for cclib or OpenBabel
71 // prefer cclib if it's available
72 if (reader == nullptr) {
73 // check what output is available
74 std::vector<const FileFormat*> readers =
75 Io::FileFormatManager::instance().fileFormatsFromFileExtension(
76 "out", FileFormat::File | FileFormat::Read);
77
78 // loop through writers to check for "cclib" or "Open Babel"
79 for (const FileFormat* r : readers) {
80 if (r->name() == "cclib") {
81 reader = r->newInstance();
82 break;
83 } else if (r->identifier().compare(0, 9, "OpenBabel") == 0) {
84 reader = r->newInstance();
85 break;
86 }
87 }
88 }
89
90 // rewind the stream
91 in.seekg(0, std::ios::beg);
92 in.clear();
93
94 if (reader) {
95 bool success = reader->readFile(fileName(), molecule);
96 delete reader;
97 return success;
98 } else {
99 appendError(
100 "Could not determine the program used to generate this output file.");
101 delete reader;
102 return false;
103 }
104 }
105
106 } // namespace Avogadro::QuantumIO