| 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 "fileformat.h" |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <fstream> |
| 10 |
#include <locale> |
| 11 |
#include <sstream> |
| 12 |
|
| 13 |
namespace Avogadro::Io { |
| 14 |
|
| 15 |
using std::ifstream; |
| 16 |
using std::locale; |
| 17 |
using std::ofstream; |
| 18 |
|
| 19 |
FileFormat::FileFormat() : m_mode(None), m_in(nullptr), m_out(nullptr) {} |
| 20 |
|
| 21 |
FileFormat::~FileFormat() |
| 22 |
{ |
| 23 |
delete m_in; |
| 24 |
delete m_out; |
| 25 |
} |
| 26 |
|
| 27 |
bool FileFormat::validateFileName(const std::string& fileName) |
| 28 |
{ |
| 29 |
bool valid = !fileName.empty(); |
| 30 |
|
| 31 |
if (valid) { |
| 32 |
// check if the filename contains invalid characters |
| 33 |
static std::string forbiddenChars(",^@={}[]~!?:&*\"|#%<>$\"'();`'"); |
| 34 |
valid = fileName.find_first_of(forbiddenChars) == std::string::npos; |
| 35 |
|
| 36 |
// check if the filename contains ".." which we should not allow |
| 37 |
valid = valid && fileName.find("..") == std::string::npos; |
| 38 |
} |
| 39 |
|
| 40 |
// Finally check against Windows names |
| 41 |
// .. we do this on all platforms because CON.cif, for example |
| 42 |
// is problematic to send to a Windows user. |
| 43 |
if (valid) { |
| 44 |
static std::string forbiddenNames( |
| 45 |
"CON PRN AUX NUL COM1 COM2 COM3 COM4 COM5 " |
| 46 |
"COM6 COM7 COM8 COM9 LPT1 LPT2 LPT3 LPT4 " |
| 47 |
"LPT5 LPT6 LPT7 LPT8 LPT9"); |
| 48 |
// case insensitive search, since con.txt is also a problem |
| 49 |
// https://stackoverflow.com/a/19839371/131896 |
| 50 |
auto it = std::search(fileName.begin(), fileName.end(), |
| 51 |
forbiddenNames.begin(), forbiddenNames.end(), |
| 52 |
[](unsigned char ch1, unsigned char ch2) { |
| 53 |
return std::toupper(ch1) == std::toupper(ch2); |
| 54 |
}); |
| 55 |
valid = (it == fileName.end()); |
| 56 |
} |
| 57 |
|
| 58 |
return valid; |
| 59 |
} |
| 60 |
|
| 61 |
bool FileFormat::open(const std::string& fileName_, Operation mode_) |
| 62 |
{ |
| 63 |
close(); |
| 64 |
m_fileName = fileName_; |
| 65 |
m_mode = mode_; |
| 66 |
if (!m_fileName.empty()) { |
| 67 |
// Imbue the standard C locale. |
| 68 |
locale cLocale("C"); |
| 69 |
if (m_mode & Read) { |
| 70 |
auto* file = new ifstream(m_fileName.c_str(), std::ifstream::binary); |
| 71 |
m_in = file; |
| 72 |
if (file->is_open()) { |
| 73 |
m_in->imbue(cLocale); |
| 74 |
return true; |
| 75 |
} else { |
| 76 |
appendError("Error opening file: " + fileName_); |
| 77 |
return false; |
| 78 |
} |
| 79 |
} else if (m_mode & Write) { |
| 80 |
auto* file = new ofstream(m_fileName.c_str(), std::ofstream::binary); |
| 81 |
m_out = file; |
| 82 |
if (file->is_open()) { |
| 83 |
m_out->imbue(cLocale); |
| 84 |
return true; |
| 85 |
} else { |
| 86 |
appendError("Error opening file: " + fileName_); |
| 87 |
return false; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
void FileFormat::close() |
| 95 |
{ |
| 96 |
if (m_in) { |
| 97 |
delete m_in; |
| 98 |
m_in = nullptr; |
| 99 |
} |
| 100 |
if (m_out) { |
| 101 |
delete m_out; |
| 102 |
m_out = nullptr; |
| 103 |
} |
| 104 |
m_mode = None; |
| 105 |
} |
| 106 |
|
| 107 |
bool FileFormat::readMolecule(Core::Molecule& molecule) |
| 108 |
{ |
| 109 |
if (!m_in) |
| 110 |
return false; |
| 111 |
return read(*m_in, molecule); |
| 112 |
} |
| 113 |
|
| 114 |
bool FileFormat::writeMolecule(const Core::Molecule& molecule) |
| 115 |
{ |
| 116 |
if (!m_out) |
| 117 |
return false; |
| 118 |
return write(*m_out, molecule); |
| 119 |
} |
| 120 |
|
| 121 |
bool FileFormat::readFile(const std::string& fileName_, |
| 122 |
Core::Molecule& molecule) |
| 123 |
{ |
| 124 |
bool result = open(fileName_, Read); |
| 125 |
if (!result) |
| 126 |
return false; |
| 127 |
|
| 128 |
result = readMolecule(molecule); |
| 129 |
close(); |
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
bool FileFormat::writeFile(const std::string& fileName_, |
| 134 |
const Core::Molecule& molecule) |
| 135 |
{ |
| 136 |
bool result = open(fileName_, Write); |
| 137 |
if (!result) |
| 138 |
return false; |
| 139 |
|
| 140 |
result = writeMolecule(molecule); |
| 141 |
close(); |
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
bool FileFormat::readString(const std::string& string, Core::Molecule& molecule) |
| 146 |
{ |
| 147 |
std::istringstream stream(string, std::istringstream::in); |
| 148 |
// Imbue the standard C locale. |
| 149 |
locale cLocale("C"); |
| 150 |
stream.imbue(cLocale); |
| 151 |
return read(stream, molecule); |
| 152 |
} |
| 153 |
|
| 154 |
bool FileFormat::writeString(std::string& string, |
| 155 |
const Core::Molecule& molecule) |
| 156 |
{ |
| 157 |
std::ostringstream stream(string, std::ostringstream::out); |
| 158 |
// Imbue the standard C locale. |
| 159 |
locale cLocale("C"); |
| 160 |
stream.imbue(cLocale); |
| 161 |
bool result = write(stream, molecule); |
| 162 |
string = stream.str(); |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
void FileFormat::clear() |
| 167 |
{ |
| 168 |
m_fileName.clear(); |
| 169 |
m_error.clear(); |
| 170 |
} |
| 171 |
|
| 172 |
void FileFormat::appendError(const std::string& errorString, bool newLine) |
| 173 |
{ |
| 174 |
m_error += errorString; |
| 175 |
if (newLine) |
| 176 |
m_error += "\n"; |
| 177 |
} |
| 178 |
|
| 179 |
} // namespace Avogadro::Io |