| 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 |
#ifndef AVOGADRO_IO_FILEFORMATMANAGER_H |
| 7 |
#define AVOGADRO_IO_FILEFORMATMANAGER_H |
| 8 |
|
| 9 |
#include "avogadroioexport.h" |
| 10 |
|
| 11 |
#include "fileformat.h" // For FileFormat::Operation enum. |
| 12 |
|
| 13 |
#include <map> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace Avogadro { |
| 18 |
namespace Core { |
| 19 |
class Molecule; |
| 20 |
} |
| 21 |
namespace Io { |
| 22 |
|
| 23 |
/** |
| 24 |
* @struct CaseInsensitiveComparator fileformatmanager.h |
| 25 |
* <avogadro/io/fileformatmanager.h> |
| 26 |
* @brief Class to handle case-insensitive comparisons of file extensions. |
| 27 |
* Adapted from https://stackoverflow.com/a/3009806/131896 |
| 28 |
**/ |
| 29 |
struct CaseInsensitiveComparator |
| 30 |
{ |
| 31 |
// case-independent (ci) compare_less binary function |
| 32 |
struct lowerCaseCompare |
| 33 |
{ |
| 34 |
bool operator()(const unsigned char& c1, const unsigned char& c2) const |
| 35 |
{ |
| 36 |
return tolower(c1) < tolower(c2); |
| 37 |
} |
| 38 |
}; |
| 39 |
bool operator()(const std::string& s1, const std::string& s2) const noexcept |
| 40 |
{ |
| 41 |
return std::lexicographical_compare(s1.begin(), s1.end(), // source range |
| 42 |
s2.begin(), s2.end(), // dest range |
| 43 |
lowerCaseCompare()); // comparison |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* @class FileFormatManager fileformatmanager.h |
| 49 |
* <avogadro/io/fileformatmanager.h> |
| 50 |
* @brief Class to manage registration, searching and creation of file formats. |
| 51 |
* @author Marcus D. Hanwell |
| 52 |
* |
| 53 |
* The file format manager is a singleton class that handles the runtime |
| 54 |
* registration, search, creation and eventual destruction of file formats. It |
| 55 |
* can be used to gain a listing of available formats, register new formats and |
| 56 |
* retrieve the correct format to facilitate file IO. |
| 57 |
* |
| 58 |
* All files IO can take place independent of this manager, but for automated |
| 59 |
* registration and look up this is the preferred API. It is possible to use |
| 60 |
* the convenience API without ever dealing directly with a format class. |
| 61 |
*/ |
| 62 |
|
| 63 |
class AVOGADROIO_EXPORT FileFormatManager |
| 64 |
{ |
| 65 |
public: |
| 66 |
/** |
| 67 |
* Get the singleton instance of the file format manager. This instance should |
| 68 |
* not be deleted. |
| 69 |
*/ |
| 70 |
static FileFormatManager& instance(); |
| 71 |
|
| 72 |
/** |
| 73 |
* Load @p molecule with the @p fileName contents supplied, inferring the |
| 74 |
* @p fileExtension if it is empty. The @p options can be used to modify |
| 75 |
* the behavior of the file format. |
| 76 |
* @return True on success, false on failure. |
| 77 |
*/ |
| 78 |
bool readFile(Core::Molecule& molecule, const std::string& fileName, |
| 79 |
const std::string& fileExtension = std::string(), |
| 80 |
const std::string& options = std::string()) const; |
| 81 |
|
| 82 |
/** |
| 83 |
* Write @p molecule to the @p fileName supplied, inferring the |
| 84 |
* @p fileExtension if it is empty. The @p options can be used to modify |
| 85 |
* the behavior of the file format. |
| 86 |
* @return True on success, false on failure. |
| 87 |
*/ |
| 88 |
bool writeFile(const Core::Molecule& molecule, const std::string& fileName, |
| 89 |
const std::string& fileExtension = std::string(), |
| 90 |
const std::string& options = std::string()) const; |
| 91 |
|
| 92 |
/** |
| 93 |
* Load @p molecule with the contents of @p string, using the supplied |
| 94 |
* @p fileExtension to determine the format. The @p options can be used to |
| 95 |
* modify the behavior of the file format. |
| 96 |
* @return True on success, false on failure. |
| 97 |
*/ |
| 98 |
bool readString(Core::Molecule& molecule, const std::string& string, |
| 99 |
const std::string& fileExtension, |
| 100 |
const std::string& options = std::string()) const; |
| 101 |
|
| 102 |
/** |
| 103 |
* Write @p molecule to the @p string, using the supplied @p fileExtension |
| 104 |
* to determine the format. The @p options can be used to modify the behavior |
| 105 |
* of the file format. |
| 106 |
* @return True on success, false on failure. |
| 107 |
*/ |
| 108 |
bool writeString(const Core::Molecule& molecule, std::string& string, |
| 109 |
const std::string& fileExtension, |
| 110 |
const std::string& options = std::string()) const; |
| 111 |
|
| 112 |
/** |
| 113 |
* @brief Register a new file format with the format manager. |
| 114 |
* @param format An instance of the format to manage, the manager assumes |
| 115 |
* ownership of the object passed in. |
| 116 |
* @return True on success, false on failure. |
| 117 |
*/ |
| 118 |
static bool registerFormat(FileFormat* format); |
| 119 |
|
| 120 |
/** |
| 121 |
* @brief Unregister a file format from the format manager. |
| 122 |
* @param identifier The identifier for the format to remove. |
| 123 |
* @return True on success, false on failure. |
| 124 |
*/ |
| 125 |
static bool unregisterFormat(const std::string& identifier); |
| 126 |
|
| 127 |
/** |
| 128 |
* Add the supplied @p format to the manager, registering its ID, MIME type, |
| 129 |
* file extension and other relevant data for later lookup. The manager |
| 130 |
* assumes ownership of the supplied object. |
| 131 |
* @return True on success, false on failure. |
| 132 |
*/ |
| 133 |
bool addFormat(FileFormat* format); |
| 134 |
|
| 135 |
/** |
| 136 |
* Remove the format with the identifier @a identifier from the manager. |
| 137 |
* @return True on success, false on failure. |
| 138 |
*/ |
| 139 |
bool removeFormat(const std::string& identifier); |
| 140 |
|
| 141 |
/** |
| 142 |
* New instance of the format for the specified @p identifier. Ownership |
| 143 |
* is passed to the caller. |
| 144 |
* @param identifier The unique identifier of the format. |
| 145 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 146 |
* represents the minimum required capabilities. |
| 147 |
* @return Instance of the format, nullptr if not found. Ownership passes to |
| 148 |
* the |
| 149 |
* caller. |
| 150 |
*/ |
| 151 |
FileFormat* newFormatFromIdentifier( |
| 152 |
const std::string& identifier, |
| 153 |
FileFormat::Operations filter = FileFormat::None) const; |
| 154 |
|
| 155 |
/** |
| 156 |
* New instance of the format for the specified @p mimeType. Ownership |
| 157 |
* is passed to the caller. |
| 158 |
* @param mimeType The MIME type (in lower case). |
| 159 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 160 |
* represents the minimum required capabilities. |
| 161 |
* @return Instance of the format, nullptr if not found. Ownership passes to |
| 162 |
* the |
| 163 |
* caller. |
| 164 |
*/ |
| 165 |
FileFormat* newFormatFromMimeType( |
| 166 |
const std::string& mimeType, |
| 167 |
FileFormat::Operations filter = FileFormat::None) const; |
| 168 |
|
| 169 |
/** |
| 170 |
* New instance of the format for the specified file @p extension. Ownership |
| 171 |
* is passed to the caller. |
| 172 |
* @param extension The file extension (in lower case). |
| 173 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 174 |
* represents the minimum required capabilities. |
| 175 |
* @return Instance of the format, nullptr if not found. Ownership passes to |
| 176 |
* the |
| 177 |
* caller. |
| 178 |
*/ |
| 179 |
FileFormat* newFormatFromFileExtension( |
| 180 |
const std::string& extension, |
| 181 |
FileFormat::Operations filter = FileFormat::None) const; |
| 182 |
|
| 183 |
/** |
| 184 |
* Get a list of all loaded identifiers, optionally matching the specified |
| 185 |
* filter. |
| 186 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 187 |
* represents the minimum required capabilities. |
| 188 |
*/ |
| 189 |
std::vector<std::string> identifiers( |
| 190 |
FileFormat::Operations filter = FileFormat::None) const; |
| 191 |
|
| 192 |
/** |
| 193 |
* Get a list of all loaded MIME types, optionally matching the specified |
| 194 |
* filter. |
| 195 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 196 |
* represents the minimum required capabilities. |
| 197 |
*/ |
| 198 |
std::vector<std::string> mimeTypes( |
| 199 |
FileFormat::Operations filter = FileFormat::None) const; |
| 200 |
|
| 201 |
/** |
| 202 |
* Get a list of the file extensions supported, optionally matching the |
| 203 |
* specified filter. |
| 204 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 205 |
* represents the minimum required capabilities. |
| 206 |
*/ |
| 207 |
std::vector<std::string> fileExtensions( |
| 208 |
FileFormat::Operations filter = FileFormat::None) const; |
| 209 |
|
| 210 |
/** |
| 211 |
* Get a list of known FileFormat objects, optionally matching the |
| 212 |
* specified filter. |
| 213 |
* @warning The objects in the returned list are owned by the |
| 214 |
* FileFormatManager and cannot be modified. Use FileFormat::newInstance() |
| 215 |
* to create mutable copies. |
| 216 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 217 |
* represents the minimum required capabilities. |
| 218 |
*/ |
| 219 |
std::vector<const FileFormat*> fileFormats( |
| 220 |
FileFormat::Operations filter = FileFormat::None) const; |
| 221 |
|
| 222 |
/** |
| 223 |
* Get a list of known FileFormat objects that handle the specified MIME type, |
| 224 |
* optionally matching a filter. |
| 225 |
* @warning The objects in the returned list are owned by the |
| 226 |
* FileFormatManager and cannot be modified. Use FileFormat::newInstance() |
| 227 |
* to create mutable copies. |
| 228 |
* @param mimeType MIME type. |
| 229 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 230 |
* represents the minimum required capabilities. |
| 231 |
*/ |
| 232 |
std::vector<const FileFormat*> fileFormatsFromMimeType( |
| 233 |
const std::string& mimeType, |
| 234 |
FileFormat::Operations filter = FileFormat::None) const; |
| 235 |
|
| 236 |
/** |
| 237 |
* Get a list of known FileFormat objects that handle the specified file |
| 238 |
* extension, optionally matching a filter. |
| 239 |
* @warning The objects in the returned list are owned by the |
| 240 |
* FileFormatManager and cannot be modified. Use FileFormat::newInstance() |
| 241 |
* to create mutable copies. |
| 242 |
* @param extension File extension. |
| 243 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 244 |
* represents the minimum required capabilities. |
| 245 |
*/ |
| 246 |
std::vector<const FileFormat*> fileFormatsFromFileExtension( |
| 247 |
const std::string& extension, |
| 248 |
FileFormat::Operations filter = FileFormat::None) const; |
| 249 |
|
| 250 |
/** |
| 251 |
* Get any errors that have been logged when loading formats. |
| 252 |
*/ |
| 253 |
std::string error() const; |
| 254 |
|
| 255 |
private: |
| 256 |
using FormatIdVector = std::vector<size_t>; |
| 257 |
using FormatIdMap = |
| 258 |
std::map<std::string, FormatIdVector, CaseInsensitiveComparator>; |
| 259 |
|
| 260 |
FileFormatManager(); |
| 261 |
~FileFormatManager(); |
| 262 |
|
| 263 |
FileFormatManager(const FileFormatManager&); // Not implemented. |
| 264 |
FileFormatManager& operator=(const FileFormatManager&); // Not implemented. |
| 265 |
|
| 266 |
/** |
| 267 |
* @brief Return keys from a map that have formats matching the supplied |
| 268 |
* operation filter. |
| 269 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 270 |
* represents the minimum required capabilities. |
| 271 |
* @param fmap The FormatMap to operate on. |
| 272 |
*/ |
| 273 |
std::vector<std::string> filteredKeysFromFormatMap( |
| 274 |
FileFormat::Operations filter, const FormatIdMap& fmap) const; |
| 275 |
|
| 276 |
/** |
| 277 |
* @brief Return formats from a map that match the supplied key and operation |
| 278 |
* filter. |
| 279 |
* @note Ownership of the format filter(s) remains with the FileFormatManager. |
| 280 |
* Use FileFormat::newInstance to clone each format before use. |
| 281 |
* @param key The map key. |
| 282 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 283 |
* represents the minimum required capabilities. |
| 284 |
* @param fmap The FormatIdMap to operate on. |
| 285 |
*/ |
| 286 |
std::vector<FileFormat*> filteredFormatsFromFormatMap( |
| 287 |
const std::string& key, FileFormat::Operations filter, |
| 288 |
const FormatIdMap& fmap) const; |
| 289 |
|
| 290 |
/** |
| 291 |
* @brief Return a format from a map that matches the supplied key and |
| 292 |
* operation filter. |
| 293 |
* @note Ownership of the format filter(s) remains with the FileFormatManager. |
| 294 |
* Use FileFormat::newInstance to clone each format before use. |
| 295 |
* @param key The map key. |
| 296 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 297 |
* represents the minimum required capabilities. |
| 298 |
* @param fmap The FormatIdMap to operate on. |
| 299 |
*/ |
| 300 |
FileFormat* filteredFormatFromFormatMap(const std::string& key, |
| 301 |
FileFormat::Operations filter, |
| 302 |
const FormatIdMap& fmap) const; |
| 303 |
|
| 304 |
/** |
| 305 |
* @brief Return formats from a vector that match the supplied operation |
| 306 |
* filter. |
| 307 |
* @note Ownership of the format filter(s) remains with the FileFormatManager. |
| 308 |
* Use FileFormat::newInstance to clone each format before use. |
| 309 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 310 |
* represents the minimum required capabilities. |
| 311 |
* @param fvec The FormatIdVector to operate on. |
| 312 |
*/ |
| 313 |
std::vector<FileFormat*> filteredFormatsFromFormatVector( |
| 314 |
FileFormat::Operations filter, const FormatIdVector& fvec) const; |
| 315 |
|
| 316 |
/** |
| 317 |
* @brief Return the first format from a vector that matches the supplied |
| 318 |
* operation filter. |
| 319 |
* @note Ownership of the format filter(s) remains with the FileFormatManager. |
| 320 |
* Use FileFormat::newInstance to clone each format before use. |
| 321 |
* @param filter Bitwise combination of FileFormat::Operation values that |
| 322 |
* represents the minimum required capabilities. |
| 323 |
* @param fvec The FormatIdVector to operate on. |
| 324 |
*/ |
| 325 |
FileFormat* filteredFormatFromFormatVector(FileFormat::Operations filter, |
| 326 |
const FormatIdVector& fvec) const; |
| 327 |
|
| 328 |
/** |
| 329 |
* @brief Append warnings/errors to the error message string. |
| 330 |
* @param errorMessage The error message to append. |
| 331 |
*/ |
| 332 |
void appendError(const std::string& errorMessage); |
| 333 |
|
| 334 |
std::vector<FileFormat*> m_formats; |
| 335 |
|
| 336 |
FormatIdMap m_identifiers; |
| 337 |
FormatIdMap m_mimeTypes; |
| 338 |
FormatIdMap m_fileExtensions; |
| 339 |
|
| 340 |
std::string m_error; |
| 341 |
}; |
| 342 |
|
| 343 |
} // namespace Io |
| 344 |
} // namespace Avogadro |
| 345 |
|
| 346 |
#endif // AVOGADRO_IO_FILEFORMATMANAGER_H |