| 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_FILEFORMAT_H |
| 7 |
#define AVOGADRO_IO_FILEFORMAT_H |
| 8 |
|
| 9 |
#include "avogadroioexport.h" |
| 10 |
#include <avogadro/core/avogadrocore.h> |
| 11 |
|
| 12 |
#include <istream> |
| 13 |
#include <ostream> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
namespace Avogadro { |
| 18 |
|
| 19 |
namespace Core { |
| 20 |
class Molecule; |
| 21 |
} |
| 22 |
|
| 23 |
namespace Io { |
| 24 |
|
| 25 |
/** |
| 26 |
* @class FileFormat fileformat.h <avogadro/io/fileformat.h> |
| 27 |
* @brief General API for file formats. |
| 28 |
* @author Marcus D. Hanwell |
| 29 |
* |
| 30 |
* This serves as the common base class for chemical file formats. Classes |
| 31 |
* deriving from this one override the read and write virtual methods and |
| 32 |
* operate on the given streams. Several other signatures are available for |
| 33 |
* convenience. If there is an error reading or writing a file the string |
| 34 |
* returned by error() will give more details. |
| 35 |
*/ |
| 36 |
|
| 37 |
class AVOGADROIO_EXPORT FileFormat |
| 38 |
{ |
| 39 |
public: |
| 40 |
FileFormat(); |
| 41 |
virtual ~FileFormat(); |
| 42 |
|
| 43 |
/** |
| 44 |
* @brief Flags defining supported operations. |
| 45 |
*/ |
| 46 |
enum Operation |
| 47 |
{ |
| 48 |
None = 0x0, |
| 49 |
Read = 0x1, |
| 50 |
Write = 0x2, |
| 51 |
ReadWrite = Read | Write, |
| 52 |
|
| 53 |
MultiMolecule = 0x4, |
| 54 |
|
| 55 |
Stream = 0x10, |
| 56 |
String = 0x20, |
| 57 |
File = 0x40, |
| 58 |
|
| 59 |
All = ReadWrite | MultiMolecule | Stream | String | File |
| 60 |
}; |
| 61 |
using Operations = int; |
| 62 |
|
| 63 |
/** |
| 64 |
* @return Operation flags defining the capabilities of this format. |
| 65 |
*/ |
| 66 |
virtual Operations supportedOperations() const = 0; |
| 67 |
|
| 68 |
/** |
| 69 |
* @brief Validates the given file name. |
| 70 |
* |
| 71 |
* Checks if the filename contains any invalid characters (e.g. ..) |
| 72 |
* Also checks if the filename contains a restricted name on Windows. |
| 73 |
* e.g., CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, etc. |
| 74 |
* |
| 75 |
* @param fileName The name of the file to be validated. |
| 76 |
* @return true if the file name is valid, false otherwise. |
| 77 |
*/ |
| 78 |
static bool validateFileName(const std::string& fileName); |
| 79 |
|
| 80 |
/** |
| 81 |
* @brief Open the specified file in Read or Write mode. |
| 82 |
* @return True on success, false on failure. |
| 83 |
*/ |
| 84 |
bool open(const std::string& fileName, Operation mode); |
| 85 |
|
| 86 |
/** |
| 87 |
* @brief The mode the format is currently operating in. |
| 88 |
* @return The mode the format is in. |
| 89 |
*/ |
| 90 |
Operation mode() { return m_mode; } |
| 91 |
|
| 92 |
/** |
| 93 |
* @brief Check if the supplied mode(s) is being used. |
| 94 |
* @param isInMode The mode(s) to test against |
| 95 |
* @return True if the format is currently in the supplied mode(s). |
| 96 |
*/ |
| 97 |
bool isMode(Operation isInMode) { return (m_mode & isInMode) != None; } |
| 98 |
|
| 99 |
/** |
| 100 |
* @brief Close any opened file handles. |
| 101 |
*/ |
| 102 |
void close(); |
| 103 |
|
| 104 |
/** |
| 105 |
* @brief Read in a molecule, if there are no molecules to read molecule will |
| 106 |
* be empty. This can be used to read in one or more molecules from a given |
| 107 |
* file using repeated calls for each molecule. |
| 108 |
* @param molecule The molecule the data will be read into. |
| 109 |
* @return True on success, false on failure. |
| 110 |
*/ |
| 111 |
bool readMolecule(Core::Molecule& molecule); |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief Write out a molecule. This can be used to write one or more |
| 115 |
* molecules to a given file using repeated calls for each molecule. |
| 116 |
* @param molecule The molecule the data will be written from. |
| 117 |
* @return True on success, false on failure. |
| 118 |
*/ |
| 119 |
bool writeMolecule(const Core::Molecule& molecule); |
| 120 |
|
| 121 |
/** |
| 122 |
* @brief Read the given @p in stream and load it into @p molecule. |
| 123 |
* @param in The input file stream. |
| 124 |
* @param molecule The molecule the data will be read into. |
| 125 |
* @return True on success, false on failure. |
| 126 |
*/ |
| 127 |
[[nodiscard]] virtual bool read(std::istream& in, |
| 128 |
Core::Molecule& molecule) = 0; |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief Write to the given @p out stream the contents of @p molecule. |
| 132 |
* @param out The output stream to write the data to. |
| 133 |
* @param molecule The contents of this molecule will be written to output. |
| 134 |
* @return True on success, false on failure. |
| 135 |
*/ |
| 136 |
[[nodiscard]] virtual bool write(std::ostream& out, |
| 137 |
const Core::Molecule& molecule) = 0; |
| 138 |
|
| 139 |
/** |
| 140 |
* @brief Read the given @p fileName and load it into @p molecule. |
| 141 |
* @param fileName The full path to the file to be read in. |
| 142 |
* @param molecule The molecule the data will be read into. |
| 143 |
* @return True on success, false on failure. |
| 144 |
*/ |
| 145 |
virtual bool readFile(const std::string& fileName, Core::Molecule& molecule); |
| 146 |
|
| 147 |
/** |
| 148 |
* @brief Write to the given @p fileName the contents of @p molecule. |
| 149 |
* @param fileName The full path to the file to be written. |
| 150 |
* @param molecule The contents of this molecule will be written to the file. |
| 151 |
* @return True on success, false on failure. |
| 152 |
*/ |
| 153 |
virtual bool writeFile(const std::string& fileName, |
| 154 |
const Core::Molecule& molecule); |
| 155 |
|
| 156 |
/** |
| 157 |
* @brief Read the given @p string and load it into @p molecule. |
| 158 |
* @param string The string containing the molecule file contents. |
| 159 |
* @param molecule The molecule the data will be read into. |
| 160 |
* @return True on success, false on failure. |
| 161 |
*/ |
| 162 |
bool readString(const std::string& string, Core::Molecule& molecule); |
| 163 |
|
| 164 |
/** |
| 165 |
* @brief Write to the given @p string the contents of @p molecule. |
| 166 |
* @param string The string to write the contents of the molecule into. |
| 167 |
* @param molecule The contents of this molecule will be written to the |
| 168 |
* string. |
| 169 |
* @return True on success, false on failure. |
| 170 |
*/ |
| 171 |
bool writeString(std::string& string, const Core::Molecule& molecule); |
| 172 |
|
| 173 |
/** |
| 174 |
* @brief Get the error string, contains errors/warnings encountered. |
| 175 |
* @return String containing any errors or warnings encountered. |
| 176 |
*/ |
| 177 |
std::string error() const { return m_error; } |
| 178 |
|
| 179 |
/** |
| 180 |
* @brief Get the file name (if known). |
| 181 |
* @return The full path to the file name as supplied, can be empty. |
| 182 |
*/ |
| 183 |
std::string fileName() const { return m_fileName; } |
| 184 |
|
| 185 |
/** |
| 186 |
* @brief Set options for the file reader. |
| 187 |
* @param options The options, each reader chooses how to use/interpret them. |
| 188 |
*/ |
| 189 |
void setOptions(const std::string& options) { m_options = options; } |
| 190 |
|
| 191 |
/** |
| 192 |
* @brief Get the file format options, can be used to change file IO. |
| 193 |
* @return The options set for the reader (defaults to empty). |
| 194 |
*/ |
| 195 |
std::string options() const { return m_options; } |
| 196 |
|
| 197 |
/** |
| 198 |
* Clear the format and reset all state. |
| 199 |
*/ |
| 200 |
virtual void clear(); |
| 201 |
|
| 202 |
/** |
| 203 |
* Create a new instance of the file format class. Ownership passes to the |
| 204 |
* caller. |
| 205 |
*/ |
| 206 |
virtual FileFormat* newInstance() const = 0; |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief A unique identifier, used to retrieve formats programmatically. |
| 210 |
* CML, XYZ, PDB etc. A runtime warning will be generated if the identifier |
| 211 |
* is not unique. |
| 212 |
*/ |
| 213 |
virtual std::string identifier() const = 0; |
| 214 |
|
| 215 |
/** |
| 216 |
* @brief The name of the format, should be short such as Chemical Markup |
| 217 |
* Language, XYZ format, Protein Databank etc. |
| 218 |
*/ |
| 219 |
virtual std::string name() const = 0; |
| 220 |
|
| 221 |
/** |
| 222 |
* A description of the format, along with any relevant help text for users. |
| 223 |
*/ |
| 224 |
virtual std::string description() const = 0; |
| 225 |
|
| 226 |
/** |
| 227 |
* The URL of the format specification if available (relevant web page/wiki |
| 228 |
* otherwise). |
| 229 |
*/ |
| 230 |
virtual std::string specificationUrl() const = 0; |
| 231 |
|
| 232 |
/** |
| 233 |
* @brief Get the file name extension(s) that the format supports reading. |
| 234 |
* @return A vector containing a list of extensions (in lower case). |
| 235 |
*/ |
| 236 |
virtual std::vector<std::string> fileExtensions() const = 0; |
| 237 |
|
| 238 |
/** |
| 239 |
* @brief Get the MIME type(s) that the format supports reading. |
| 240 |
* @return A vector containing a list of MIME type(s) (in lower case). |
| 241 |
*/ |
| 242 |
virtual std::vector<std::string> mimeTypes() const = 0; |
| 243 |
|
| 244 |
protected: |
| 245 |
/** |
| 246 |
* @brief Append an error to the error string for the format. |
| 247 |
* @param errorString The error to be added. |
| 248 |
* @param newLine Add a new line after the error string? |
| 249 |
*/ |
| 250 |
void appendError(const std::string& errorString, bool newLine = true); |
| 251 |
|
| 252 |
private: |
| 253 |
std::string m_error; |
| 254 |
std::string m_fileName; |
| 255 |
std::string m_options; |
| 256 |
|
| 257 |
// Streams for reading/writing data, especially streaming data in/out. |
| 258 |
Operation m_mode; |
| 259 |
std::istream* m_in; |
| 260 |
std::ostream* m_out; |
| 261 |
}; |
| 262 |
|
| 263 |
inline FileFormat::Operation operator|(FileFormat::Operation a, |
| 264 |
FileFormat::Operation b) |
| 265 |
{ |
| 266 |
return static_cast<FileFormat::Operation>(static_cast<int>(a) | |
| 267 |
static_cast<int>(b)); |
| 268 |
} |
| 269 |
|
| 270 |
} // namespace Io |
| 271 |
} // namespace Avogadro |
| 272 |
|
| 273 |
#endif // AVOGADRO_IO_FILEFORMAT_H |