00001
00002
00003
00004
00005
00006
00007 #ifndef BALL_FORMAT_CIFFILE_H
00008 #define BALL_FORMAT_CIFFILE_H
00009
00010 #ifndef BALL_SYSTEM_FILE_H
00011 # include <BALL/SYSTEM/file.h>
00012 #endif
00013
00014 #ifndef BALL_DATATYPE_STRINGHASHMAP_H
00015 # include <BALL/DATATYPE/stringHashMap.h>
00016 #endif
00017
00018 #ifndef BALL_KERNEL_MOLECULE_H
00019 # include <BALL/KERNEL/molecule.h>
00020 #endif
00021
00022 #include <map>
00023
00024 #define CIFPARSER_LINE_LENGTH 2550
00025
00026 namespace BALL
00027 {
00034 class BALL_EXPORT CIFFile
00035 : public File
00036 {
00037 public:
00038 BALL_CREATE(CIFFile)
00039
00040
00041 class Item
00042 {
00043 public:
00044
00045 Item();
00046
00047 Item(const Item& item);
00048
00049 virtual ~Item();
00050
00052 void clear();
00053 void addPair(String key, String value);
00054 void startLoop();
00055 void addTag(String tag);
00056 void addValue(String value);
00057
00059 Index getKeyIndex(String key);
00060
00061
00062 bool is_loop;
00063
00065
00067 std::vector<String> keys;
00069 std::vector<std::vector<String> > values;
00070
00072 std::pair<String, String> entry;
00073
00074 std::ostream& operator >> (std::ostream& os) const;
00075
00076 };
00077
00079 class SaveFrame
00080 {
00081 public:
00082
00083 void clear();
00084 void start(String name);
00085 void addDataItem(Item item);
00086 void setCategory(String cat) {category = cat;};
00087
00088 std::ostream& operator >> (std::ostream& os) const;
00089 const String& getCategory() const {return category;};
00090
00092 const Item& getDataItem(const String& item_name) const;
00093
00094 Item& getDataItem(const String& item_name);
00095
00096 const String& getDataItemValue(const String& item_name) const;
00097
00098 String& getDataItemValue(const String& item_name);
00099
00100
00101
00102 bool hasItem (const String& item_name) const;
00103 String getItemValue(const String& name) const;
00104
00105
00107 String framename;
00108 String category;
00109
00111 std::vector<Item> items;
00112 StringHashMap<Index> pair_items;
00113
00114 Item dummy_data_item_;
00115
00116 };
00117
00119 class Datacontent
00120 {
00121 public:
00122
00124 Datacontent();
00125 Datacontent(SaveFrame new_saveframe);
00126 Datacontent(Item new_item);
00127
00129 bool is_saveframe;
00130
00132 SaveFrame saveframe;
00133
00135 Item dataitem;
00136
00137 std::ostream& operator >> (std::ostream& os) const;
00138
00139 };
00140
00142 class Datablock
00143 {
00144 public:
00146 void clear();
00147
00149 void start(String blockname);
00150
00151 void insertDatacontent(const Datacontent& content);
00152 void insertDatacontent(const SaveFrame& new_saveframe);
00153 void insertDatacontent(const Item& item);
00154
00155 const Item& getDataItem(const String& item_name) const;
00156 Item& getDataItem(const String& item_name);
00157
00160 const std::vector<Index> getSaveframeIndicesByCategory(const String& name) const;
00161 std::vector<Index> getSaveframeIndicesByCategory(const String& name);
00162
00163 std::vector<SaveFrame> getSaveframesByCategory(const String& name) const;
00164
00165
00168 const SaveFrame& getSaveframeByName(const String& name) const;
00169 SaveFrame& getSaveframeByName(const String& name);
00170
00171 const SaveFrame& getSaveframeByIndex(const Index index) const;
00172 SaveFrame& getSaveframeByIndex(const Index index);
00173
00174
00177
00178 bool hasSaveframeName(const String& name) const;
00179 bool hasSaveframeCategory(const String& name) const;
00180 bool hasItem(const String& name) const;
00181
00183 void setName(String blockname);
00184
00185 std::ostream& operator >> (std::ostream& os) const;
00187
00188 String name;
00189
00191 std::vector<Datacontent> data;
00192
00194 StringHashMap<Index> saveframe_names;
00195
00196 std::multimap<String, Index> saveframe_categories;
00197
00199 StringHashMap<Index> item_names;
00200
00201 Item dummy_data_item_;
00202 SaveFrame dummy_saveframe_;
00203 vector<SaveFrame> dummy_saveframes_;
00204 vector<Index> dummy_indices_;
00205
00206 };
00207
00209 struct State
00210 {
00211 CIFFile* current_parser;
00212 };
00213
00217
00220 CIFFile();
00221
00224 CIFFile(const CIFFile& file)
00225 throw(Exception::FileNotFound);
00226
00229 CIFFile(const String& filename, File::OpenMode open_mode = std::ios::in)
00230 throw(Exception::FileNotFound);
00231
00234 virtual ~CIFFile();
00235
00237
00240
00244 const CIFFile& operator = (const CIFFile& rhs)
00245 throw(Exception::FileNotFound);
00246
00248
00252
00255 virtual bool write()
00256 throw(File::CannotWrite);
00257
00260 virtual bool read()
00261 throw(Exception::ParseError);
00262
00264
00267 void inSafeframe(const char* name);
00268
00269 void inItem(const char* name);
00270
00271 void inDatablock(const char* name);
00272
00273 void insertDatablock(const Datablock& datablock);
00274
00275 const Datablock& getDatablock(const String& name) const;
00276
00277
00278
00279 Datablock& getDatablock(const String& name);
00280
00281 bool hasDatablock(const String& name) const;
00282
00283 void setMolecule(Molecule* molecule);
00284
00285 void clearParameters();
00287
00288 static State state;
00289
00290 protected:
00291 virtual void initRead_();
00292
00293 Molecule* molecule_;
00294
00295 String current_datablock_;
00296 String current_saveframe_;
00297 String current_item_;
00298
00299 StringHashMap<Index> datablocks_hash_;
00300 vector<Datablock> datablocks_;
00301 };
00302 }
00303 #endif // BALL_FORMAT_CIFFILE_H