BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 00005 #ifndef BALL_FORMAT_CIFFILE_H 00006 #define BALL_FORMAT_CIFFILE_H 00007 00008 #ifndef BALL_SYSTEM_FILE_H 00009 # include <BALL/SYSTEM/file.h> 00010 #endif 00011 00012 #ifndef BALL_DATATYPE_STRINGHASHMAP_H 00013 # include <BALL/DATATYPE/stringHashMap.h> 00014 #endif 00015 00016 #ifndef BALL_KERNEL_MOLECULE_H 00017 # include <BALL/KERNEL/molecule.h> 00018 #endif 00019 00020 #include <map> 00021 00022 #define CIFPARSER_LINE_LENGTH 2550 00023 00024 namespace BALL 00025 { 00032 class BALL_EXPORT CIFFile 00033 : public File 00034 { 00035 public: 00036 00038 class Item 00039 { 00040 public: 00041 00042 Item(); 00043 00044 Item(const Item& item); 00045 00046 virtual ~Item(); 00047 00049 void clear(); 00050 void addPair(String key, String value); 00051 void startLoop(); 00052 void addTag(String tag); 00053 void addValue(String value); 00054 00056 Index getKeyIndex(String key); 00057 00058 /* a data item may be a tag-value pair or a table */ 00059 bool is_loop; 00060 00062 //StringHashMap< Index > data; 00064 std::vector<String> keys; 00066 std::vector<std::vector<String> > values; 00067 00069 std::pair<String, String> entry; 00070 00071 std::ostream& operator >> (std::ostream& os) const; 00072 00073 }; 00074 00076 class SaveFrame 00077 { 00078 public: 00079 00080 void clear(); 00081 void start(String name); 00082 void addDataItem(Item item); 00083 void setCategory(String cat) {category = cat;}; 00084 00085 std::ostream& operator >> (std::ostream& os) const; 00086 const String& getCategory() const {return category;}; 00087 00089 const Item& getDataItem(const String& item_name) const; 00090 // we allow manipulation 00091 Item& getDataItem(const String& item_name); 00092 00093 const String& getDataItemValue(const String& item_name) const; 00094 // we allow manipulation 00095 String& getDataItemValue(const String& item_name); 00096 00097 00098 00099 bool hasItem (const String& item_name) const; 00100 String getItemValue(const String& name) const; 00101 00102 00104 String framename; 00105 String category; // always first_item.entry.second 00106 00108 std::vector<Item> items; 00109 StringHashMap<Index> pair_items; 00110 00111 Item dummy_data_item_; 00112 00113 }; 00114 00116 class Datacontent 00117 { 00118 public: 00119 00121 Datacontent(); 00122 Datacontent(SaveFrame new_saveframe); 00123 Datacontent(Item new_item); 00124 00126 bool is_saveframe; 00127 00129 SaveFrame saveframe; 00130 00132 Item dataitem; 00133 00134 std::ostream& operator >> (std::ostream& os) const; 00135 00136 }; 00137 00139 class Datablock 00140 { 00141 public: 00143 void clear(); 00144 00146 void start(String blockname); 00147 00148 void insertDatacontent(const Datacontent& content); 00149 void insertDatacontent(const SaveFrame& new_saveframe); 00150 void insertDatacontent(const Item& item); 00151 00152 const Item& getDataItem(const String& item_name) const; 00153 Item& getDataItem(const String& item_name); 00154 00157 const std::vector<Index> getSaveframeIndicesByCategory(const String& name) const; 00158 std::vector<Index> getSaveframeIndicesByCategory(const String& name); 00159 /* Returns copies of saveframes of category name */ 00160 std::vector<SaveFrame> getSaveframesByCategory(const String& name) const; 00161 00162 00165 const SaveFrame& getSaveframeByName(const String& name) const; 00166 SaveFrame& getSaveframeByName(const String& name); 00167 00168 const SaveFrame& getSaveframeByIndex(const Index index) const; 00169 SaveFrame& getSaveframeByIndex(const Index index); 00170 00171 00174 // 00175 bool hasSaveframeName(const String& name) const; 00176 bool hasSaveframeCategory(const String& name) const; 00177 bool hasItem(const String& name) const; 00178 00180 void setName(String blockname); 00181 00182 std::ostream& operator >> (std::ostream& os) const; 00184 00185 String name; 00186 00188 std::vector<Datacontent> data; 00189 00191 StringHashMap<Index> saveframe_names; 00192 //StringHashMap<std::vector<Index> > saveframe_categories; 00193 std::multimap<String, Index> saveframe_categories; 00194 00196 StringHashMap<Index> item_names; 00197 00198 Item dummy_data_item_; 00199 SaveFrame dummy_saveframe_; 00200 vector<SaveFrame> dummy_saveframes_; 00201 vector<Index> dummy_indices_; 00202 00203 }; 00204 00206 struct State 00207 { 00208 CIFFile* current_parser; 00209 }; 00210 00214 00217 CIFFile(); 00218 00222 CIFFile(const String& filename, File::OpenMode open_mode = std::ios::in); 00223 00226 virtual ~CIFFile(); 00227 00229 00233 00237 virtual bool write(); 00238 00242 virtual bool read(); 00243 00245 00248 void inSafeframe(const char* name); 00249 00250 void inItem(const char* name); 00251 00252 void inDatablock(const char* name); 00253 00254 void insertDatablock(const Datablock& datablock); 00255 00256 const Datablock& getDatablock(const String& name) const; 00257 00258 00259 // we allow manipulation :-) 00260 Datablock& getDatablock(const String& name); 00261 00262 bool hasDatablock(const String& name) const; 00263 00264 void setMolecule(Molecule* molecule); 00265 00266 void clearParameters(); 00268 00269 static State state; 00270 00271 protected: 00272 virtual void initRead_(); 00273 00274 Molecule* molecule_; 00275 00276 String current_datablock_; 00277 String current_saveframe_; 00278 String current_item_; 00279 00280 StringHashMap<Index> datablocks_hash_; 00281 vector<Datablock> datablocks_; 00282 }; 00283 } 00284 #endif // BALL_FORMAT_CIFFILE_H