| 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 "fileformatmanager.h" |
| 7 |
|
| 8 |
#include "fileformat.h" |
| 9 |
|
| 10 |
#include "cjsonformat.h" |
| 11 |
#include "cmlformat.h" |
| 12 |
#include "cmsgpackformat.h" |
| 13 |
#include "dcdformat.h" |
| 14 |
#include "gromacsformat.h" |
| 15 |
#include "lammpsformat.h" |
| 16 |
#include "mdlformat.h" |
| 17 |
#include "pdbformat.h" |
| 18 |
#include "sdfformat.h" |
| 19 |
#include "trrformat.h" |
| 20 |
#include "turbomoleformat.h" |
| 21 |
#include "vaspformat.h" |
| 22 |
#include "xyzformat.h" |
| 23 |
|
| 24 |
#ifdef AVO_USE_MMTF |
| 25 |
#include "mmtfformat.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <memory> |
| 30 |
|
| 31 |
using std::unique_ptr; |
| 32 |
|
| 33 |
namespace Avogadro::Io { |
| 34 |
|
| 35 |
FileFormatManager& FileFormatManager::instance() |
| 36 |
{ |
| 37 |
static FileFormatManager instance; |
| 38 |
return instance; |
| 39 |
} |
| 40 |
|
| 41 |
bool FileFormatManager::readFile(Core::Molecule& molecule, |
| 42 |
const std::string& fileName, |
| 43 |
const std::string& fileExtension, |
| 44 |
const std::string& options) const |
| 45 |
{ |
| 46 |
FileFormat* format(nullptr); |
| 47 |
if (fileExtension.empty()) { |
| 48 |
// We need to guess the file extension. |
| 49 |
size_t pos = fileName.find_last_of('.'); |
| 50 |
format = filteredFormatFromFormatMap(fileName.substr(pos + 1), |
| 51 |
FileFormat::Read | FileFormat::File, |
| 52 |
m_fileExtensions); |
| 53 |
} else { |
| 54 |
format = filteredFormatFromFormatMap( |
| 55 |
fileExtension, FileFormat::Read | FileFormat::File, m_fileExtensions); |
| 56 |
} |
| 57 |
if (!format) |
| 58 |
return false; |
| 59 |
|
| 60 |
unique_ptr<FileFormat> formatInstance(format->newInstance()); |
| 61 |
formatInstance->setOptions(options); |
| 62 |
return formatInstance->readFile(fileName, molecule); |
| 63 |
} |
| 64 |
|
| 65 |
bool FileFormatManager::writeFile(const Core::Molecule& molecule, |
| 66 |
const std::string& fileName, |
| 67 |
const std::string& fileExtension, |
| 68 |
const std::string& options) const |
| 69 |
{ |
| 70 |
FileFormat* format(nullptr); |
| 71 |
if (fileExtension.empty()) { |
| 72 |
// We need to guess the file extension. |
| 73 |
size_t pos = fileName.find_last_of('.'); |
| 74 |
format = filteredFormatFromFormatMap(fileName.substr(pos + 1), |
| 75 |
FileFormat::Write | FileFormat::File, |
| 76 |
m_fileExtensions); |
| 77 |
} else { |
| 78 |
format = filteredFormatFromFormatMap( |
| 79 |
fileExtension, FileFormat::Write | FileFormat::File, m_fileExtensions); |
| 80 |
} |
| 81 |
if (!format) |
| 82 |
return false; |
| 83 |
|
| 84 |
unique_ptr<FileFormat> formatInstance(format->newInstance()); |
| 85 |
formatInstance->setOptions(options); |
| 86 |
return formatInstance->writeFile(fileName, molecule); |
| 87 |
} |
| 88 |
|
| 89 |
bool FileFormatManager::readString(Core::Molecule& molecule, |
| 90 |
const std::string& string, |
| 91 |
const std::string& fileExtension, |
| 92 |
const std::string& options) const |
| 93 |
{ |
| 94 |
FileFormat* format(filteredFormatFromFormatMap( |
| 95 |
fileExtension, FileFormat::Read | FileFormat::String, m_fileExtensions)); |
| 96 |
if (!format) |
| 97 |
return false; |
| 98 |
|
| 99 |
unique_ptr<FileFormat> formatInstance(format->newInstance()); |
| 100 |
formatInstance->setOptions(options); |
| 101 |
return formatInstance->readString(string, molecule); |
| 102 |
} |
| 103 |
|
| 104 |
bool FileFormatManager::writeString(const Core::Molecule& molecule, |
| 105 |
std::string& string, |
| 106 |
const std::string& fileExtension, |
| 107 |
const std::string& options) const |
| 108 |
{ |
| 109 |
FileFormat* format(filteredFormatFromFormatMap( |
| 110 |
fileExtension, FileFormat::Write | FileFormat::String, m_fileExtensions)); |
| 111 |
if (!format) |
| 112 |
return false; |
| 113 |
|
| 114 |
unique_ptr<FileFormat> formatInstance(format->newInstance()); |
| 115 |
formatInstance->setOptions(options); |
| 116 |
return formatInstance->writeString(string, molecule); |
| 117 |
} |
| 118 |
|
| 119 |
bool FileFormatManager::registerFormat(FileFormat* format) |
| 120 |
{ |
| 121 |
return instance().addFormat(format); |
| 122 |
} |
| 123 |
|
| 124 |
bool FileFormatManager::unregisterFormat(const std::string& identifier) |
| 125 |
{ |
| 126 |
return instance().removeFormat(identifier); |
| 127 |
} |
| 128 |
|
| 129 |
bool FileFormatManager::addFormat(FileFormat* format) |
| 130 |
{ |
| 131 |
if (!format) { |
| 132 |
appendError("Supplied format was null."); |
| 133 |
return false; |
| 134 |
} |
| 135 |
if (m_identifiers.count(format->identifier()) > 0) { |
| 136 |
appendError("Format " + format->identifier() + " already loaded."); |
| 137 |
return false; |
| 138 |
} |
| 139 |
for (auto& m_format : m_formats) { |
| 140 |
if (m_format == format) { |
| 141 |
appendError("The format object was already loaded."); |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// If we got here then the format is unique enough to be added. |
| 147 |
size_t index = m_formats.size(); |
| 148 |
m_formats.push_back(format); |
| 149 |
m_identifiers[format->identifier()].push_back(index); |
| 150 |
std::vector<std::string> mimes = format->mimeTypes(); |
| 151 |
for (auto& mime : mimes) { |
| 152 |
m_mimeTypes[mime].push_back(index); |
| 153 |
} |
| 154 |
std::vector<std::string> extensions = format->fileExtensions(); |
| 155 |
for (auto& extension : extensions) { |
| 156 |
m_fileExtensions[extension].push_back(index); |
| 157 |
} |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
namespace { |
| 163 |
// Lookup each key from "keys" in "map", and remove "val" from the Map's |
| 164 |
// data value (which is a vector of ValueType) |
| 165 |
template <typename Map, typename VectorOfKeys, typename ValueType> |
| 166 |
void removeFromMap(Map& map, const VectorOfKeys& keys, const ValueType& val) |
| 167 |
{ |
| 168 |
for (auto key = keys.begin(), keyEnd = keys.end(); key != keyEnd; ++key) { |
| 169 |
auto mapMatch = map.find(*key); |
| 170 |
if (mapMatch == map.end()) |
| 171 |
continue; |
| 172 |
typename Map::mapped_type& vec = mapMatch->second; |
| 173 |
if (vec.size() <= 1) { |
| 174 |
map.erase(*key); |
| 175 |
} else { |
| 176 |
auto newEnd = std::remove(vec.begin(), vec.end(), val); |
| 177 |
vec.resize(newEnd - vec.begin()); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} // namespace |
| 182 |
|
| 183 |
bool FileFormatManager::removeFormat(const std::string& identifier) |
| 184 |
{ |
| 185 |
FormatIdVector ids = m_identifiers[identifier]; |
| 186 |
m_identifiers.erase(identifier); |
| 187 |
|
| 188 |
if (ids.empty()) |
| 189 |
return false; |
| 190 |
|
| 191 |
for (size_t id : ids) { |
| 192 |
FileFormat* fmt = m_formats[id]; |
| 193 |
|
| 194 |
if (fmt == nullptr) |
| 195 |
continue; |
| 196 |
|
| 197 |
removeFromMap(m_mimeTypes, fmt->mimeTypes(), id); |
| 198 |
removeFromMap(m_fileExtensions, fmt->fileExtensions(), id); |
| 199 |
|
| 200 |
m_formats[id] = nullptr; |
| 201 |
delete fmt; |
| 202 |
} |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
FileFormat* FileFormatManager::newFormatFromIdentifier( |
| 208 |
const std::string& id, FileFormat::Operations filter) const |
| 209 |
{ |
| 210 |
FileFormat* format(filteredFormatFromFormatMap(id, filter, m_identifiers)); |
| 211 |
return format ? format->newInstance() : nullptr; |
| 212 |
} |
| 213 |
|
| 214 |
FileFormat* FileFormatManager::newFormatFromMimeType( |
| 215 |
const std::string& mime, FileFormat::Operations filter) const |
| 216 |
{ |
| 217 |
FileFormat* format(filteredFormatFromFormatMap(mime, filter, m_mimeTypes)); |
| 218 |
return format ? format->newInstance() : nullptr; |
| 219 |
} |
| 220 |
|
| 221 |
FileFormat* FileFormatManager::newFormatFromFileExtension( |
| 222 |
const std::string& extension, FileFormat::Operations filter) const |
| 223 |
{ |
| 224 |
FileFormat* format( |
| 225 |
filteredFormatFromFormatMap(extension, filter, m_fileExtensions)); |
| 226 |
return format ? format->newInstance() : nullptr; |
| 227 |
} |
| 228 |
|
| 229 |
std::vector<std::string> FileFormatManager::identifiers( |
| 230 |
FileFormat::Operations filter) const |
| 231 |
{ |
| 232 |
return filteredKeysFromFormatMap(filter, m_identifiers); |
| 233 |
} |
| 234 |
|
| 235 |
std::vector<std::string> FileFormatManager::mimeTypes( |
| 236 |
FileFormat::Operations filter) const |
| 237 |
{ |
| 238 |
return filteredKeysFromFormatMap(filter, m_mimeTypes); |
| 239 |
} |
| 240 |
|
| 241 |
std::vector<std::string> FileFormatManager::fileExtensions( |
| 242 |
FileFormat::Operations filter) const |
| 243 |
{ |
| 244 |
return filteredKeysFromFormatMap(filter, m_fileExtensions); |
| 245 |
} |
| 246 |
|
| 247 |
std::vector<const FileFormat*> FileFormatManager::fileFormats( |
| 248 |
FileFormat::Operations filter) const |
| 249 |
{ |
| 250 |
std::vector<const FileFormat*> result; |
| 251 |
|
| 252 |
for (auto m_format : m_formats) { |
| 253 |
if (filter == FileFormat::None || |
| 254 |
(filter & m_format->supportedOperations()) == filter) { |
| 255 |
result.push_back(m_format); |
| 256 |
} |
| 257 |
} |
| 258 |
return result; |
| 259 |
} |
| 260 |
|
| 261 |
std::vector<const FileFormat*> FileFormatManager::fileFormatsFromMimeType( |
| 262 |
const std::string& mimeType, FileFormat::Operations filter) const |
| 263 |
{ |
| 264 |
std::vector<FileFormat*> matches = |
| 265 |
filteredFormatsFromFormatMap(mimeType, filter, m_mimeTypes); |
| 266 |
|
| 267 |
return std::vector<const FileFormat*>(matches.begin(), matches.end()); |
| 268 |
} |
| 269 |
|
| 270 |
std::vector<const FileFormat*> FileFormatManager::fileFormatsFromFileExtension( |
| 271 |
const std::string& extension, FileFormat::Operations filter) const |
| 272 |
{ |
| 273 |
std::vector<FileFormat*> matches = |
| 274 |
filteredFormatsFromFormatMap(extension, filter, m_fileExtensions); |
| 275 |
|
| 276 |
return std::vector<const FileFormat*>(matches.begin(), matches.end()); |
| 277 |
} |
| 278 |
|
| 279 |
std::string FileFormatManager::error() const |
| 280 |
{ |
| 281 |
return m_error; |
| 282 |
} |
| 283 |
|
| 284 |
FileFormatManager::FileFormatManager() |
| 285 |
{ |
| 286 |
addFormat(new CmlFormat); |
| 287 |
addFormat(new CjsonFormat); |
| 288 |
addFormat(new CMsgPackFormat); |
| 289 |
addFormat(new DcdFormat); |
| 290 |
addFormat(new GromacsFormat); |
| 291 |
addFormat(new LammpsTrajectoryFormat); |
| 292 |
addFormat(new LammpsDataFormat); |
| 293 |
addFormat(new MdlFormat); |
| 294 |
addFormat(new OutcarFormat); |
| 295 |
addFormat(new PdbFormat); |
| 296 |
addFormat(new PoscarFormat); |
| 297 |
addFormat(new SdfFormat); |
| 298 |
addFormat(new TrrFormat); |
| 299 |
addFormat(new TurbomoleFormat); |
| 300 |
addFormat(new XyzFormat); |
| 301 |
#ifdef AVO_USE_MMTF |
| 302 |
addFormat(new MMTFFormat); |
| 303 |
#endif |
| 304 |
} |
| 305 |
|
| 306 |
FileFormatManager::~FileFormatManager() |
| 307 |
{ |
| 308 |
// Delete the file formats that were loaded. |
| 309 |
for (auto& m_format : m_formats) { |
| 310 |
delete m_format; |
| 311 |
} |
| 312 |
m_formats.clear(); |
| 313 |
} |
| 314 |
|
| 315 |
std::vector<std::string> FileFormatManager::filteredKeysFromFormatMap( |
| 316 |
FileFormat::Operations filter, |
| 317 |
const FileFormatManager::FormatIdMap& fmap) const |
| 318 |
{ |
| 319 |
std::vector<std::string> result; |
| 320 |
for (const auto& it : fmap) { |
| 321 |
for (auto formatIt = it.second.begin(); formatIt != it.second.end(); |
| 322 |
++formatIt) { |
| 323 |
if (filter == FileFormat::None || |
| 324 |
(m_formats[*formatIt]->supportedOperations() & filter) == filter) { |
| 325 |
result.push_back(it.first); |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
return result; |
| 331 |
} |
| 332 |
|
| 333 |
std::vector<FileFormat*> FileFormatManager::filteredFormatsFromFormatMap( |
| 334 |
const std::string& key, FileFormat::Operations filter, |
| 335 |
const FileFormatManager::FormatIdMap& fmap) const |
| 336 |
{ |
| 337 |
std::vector<FileFormat*> result; |
| 338 |
|
| 339 |
auto it = fmap.find(key); |
| 340 |
if (it != fmap.end()) |
| 341 |
result = filteredFormatsFromFormatVector(filter, it->second); |
| 342 |
|
| 343 |
return result; |
| 344 |
} |
| 345 |
|
| 346 |
FileFormat* FileFormatManager::filteredFormatFromFormatMap( |
| 347 |
const std::string& key, FileFormat::Operations filter, |
| 348 |
const FileFormatManager::FormatIdMap& fmap) const |
| 349 |
{ |
| 350 |
auto it = fmap.find(key); |
| 351 |
if (it != fmap.end()) |
| 352 |
return filteredFormatFromFormatVector(filter, it->second); |
| 353 |
|
| 354 |
return nullptr; |
| 355 |
} |
| 356 |
|
| 357 |
std::vector<FileFormat*> FileFormatManager::filteredFormatsFromFormatVector( |
| 358 |
FileFormat::Operations filter, |
| 359 |
const FileFormatManager::FormatIdVector& v) const |
| 360 |
{ |
| 361 |
std::vector<FileFormat*> result; |
| 362 |
for (unsigned long it : v) { |
| 363 |
if (filter == FileFormat::None || |
| 364 |
(m_formats[it]->supportedOperations() & filter) == filter) { |
| 365 |
result.push_back(m_formats[it]); |
| 366 |
} |
| 367 |
} |
| 368 |
return result; |
| 369 |
} |
| 370 |
|
| 371 |
FileFormat* FileFormatManager::filteredFormatFromFormatVector( |
| 372 |
FileFormat::Operations filter, |
| 373 |
const FileFormatManager::FormatIdVector& v) const |
| 374 |
{ |
| 375 |
for (unsigned long it : v) { |
| 376 |
if (filter == FileFormat::None || |
| 377 |
(m_formats[it]->supportedOperations() & filter) == filter) { |
| 378 |
return m_formats[it]; |
| 379 |
} |
| 380 |
} |
| 381 |
return nullptr; |
| 382 |
} |
| 383 |
|
| 384 |
void FileFormatManager::appendError(const std::string& errorMessage) |
| 385 |
{ |
| 386 |
m_error += errorMessage + "\n"; |
| 387 |
} |
| 388 |
|
| 389 |
} // namespace Avogadro::Io |