| 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 |
#include <avogadro/core/molecule.h> |
| 6 |
#include <avogadro/core/version.h> |
| 7 |
#include <avogadro/io/fileformatmanager.h> |
| 8 |
|
| 9 |
#include <iostream> |
| 10 |
#include <sstream> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
using Avogadro::Core::Molecule; |
| 14 |
using Avogadro::Io::FileFormatManager; |
| 15 |
using std::cin; |
| 16 |
using std::cout; |
| 17 |
using std::endl; |
| 18 |
using std::ostringstream; |
| 19 |
using std::string; |
| 20 |
|
| 21 |
void printHelp(); |
| 22 |
|
| 23 |
int main(int argc, char* argv[]) |
| 24 |
{ |
| 25 |
// Process the command line arguments, see what has been requested. |
| 26 |
string inFormat; |
| 27 |
string outFormat; |
| 28 |
string inFile; |
| 29 |
string outFile; |
| 30 |
for (int i = 1; i < argc; ++i) { |
| 31 |
string current(argv[i]); |
| 32 |
if (current == "--help" || current == "-h") { |
| 33 |
printHelp(); |
| 34 |
return 0; |
| 35 |
} else if (current == "--version" || current == "-v") { |
| 36 |
cout << "Version: " << Avogadro::version() << endl; |
| 37 |
return 0; |
| 38 |
} else if (current == "-i" && i + 1 < argc) { |
| 39 |
inFormat = argv[++i]; |
| 40 |
cout << "input format " << inFormat << endl; |
| 41 |
} else if (current == "-o" && i + 1 < argc) { |
| 42 |
outFormat = argv[++i]; |
| 43 |
cout << "output format " << outFormat << endl; |
| 44 |
} else if (inFile.empty()) { |
| 45 |
inFile = argv[i]; |
| 46 |
} else if (outFile.empty()) { |
| 47 |
outFile = argv[i]; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// Now read/write the molecule, if possible. Otherwise output errors. |
| 52 |
FileFormatManager& mgr = FileFormatManager::instance(); |
| 53 |
Molecule mol; |
| 54 |
if (!inFile.empty()) { |
| 55 |
if (!mgr.readFile(mol, inFile, inFormat)) { |
| 56 |
cout << "Failed to read " << inFile << " (" << inFormat << ")" << endl; |
| 57 |
return 1; |
| 58 |
} |
| 59 |
} else if (!inFormat.empty()) { |
| 60 |
ostringstream inFileString; |
| 61 |
string line; |
| 62 |
while (getline(cin, line)) |
| 63 |
inFileString << line; |
| 64 |
if (!inFileString.str().empty()) { |
| 65 |
if (!mgr.readString(mol, inFileString.str(), inFormat)) { |
| 66 |
cout << "Failed to read input stream: " << inFileString.str() << endl; |
| 67 |
return 1; |
| 68 |
} |
| 69 |
} |
| 70 |
} else { |
| 71 |
cout << "Error, no input file or stream supplied with format." << endl; |
| 72 |
} |
| 73 |
|
| 74 |
if (!outFile.empty()) { |
| 75 |
if (!mgr.writeFile(mol, outFile, outFormat)) { |
| 76 |
cout << "Failed to write " << outFile << " (" << outFormat << ")" << endl; |
| 77 |
return 1; |
| 78 |
} |
| 79 |
} else { |
| 80 |
if (outFormat.empty()) |
| 81 |
outFormat = "cjson"; |
| 82 |
string out; |
| 83 |
mgr.writeString(mol, out, outFormat); |
| 84 |
cout << out << endl; |
| 85 |
} |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
void printHelp() |
| 91 |
{ |
| 92 |
cout << "Usage: avobabel [-i <input-type>] <infilename> [-o <output-type>] " |
| 93 |
"<outfilename>\n" |
| 94 |
<< endl; |
| 95 |
} |