script_write.h

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00017 #ifndef __SCRIPT_WRITE_HEADER__
00018 #define __SCRIPT_WRITE_HEADER__
00019 
00020 #include "utils.h"
00021 #include "defs.h"
00022 
00023 #include "script.h"
00024 
00025 namespace hoa_script {
00026 
00052 class WriteScriptDescriptor : public ScriptDescriptor {
00053   friend class GameScript;
00054 public:
00055   ~WriteScriptDescriptor();
00056 
00062   bool OpenFile(const std::string& file_name);
00063   bool OpenFile();
00064   void CloseFile();
00066 
00072 
00073   void InsertNewLine();
00074 
00078   void WriteComment(const std::string& comment);
00079 
00081   void BeginCommentBlock();
00082 
00084   void EndCommentBlock();
00085 
00093   void WriteLine(const std::string& comment, bool new_line = true);
00095 
00104   void WriteBool(const std::string& key, bool value);
00105   void WriteBool(const int32 key, bool value);
00106 
00107   void WriteInt(const std::string& key, int32 value)
00108     { _WriteData(key, value); }
00109 
00110   void WriteInt(const int32 key, int32 value)
00111     { _WriteData(key, value); }
00112 
00113   void WriteUInt(const std::string& key, uint32 value)
00114     { _WriteData(key, value); }
00115 
00116   void WriteUInt(const int32 key, uint32 value)
00117     { _WriteData(key, value); }
00118 
00119   void WriteFloat(const std::string& key, float value)
00120     { _WriteData(key, value); }
00121 
00122   void WriteFloat(const int32 key, float value)
00123     { _WriteData(key, value); }
00124 
00125   void WriteString(const std::string& key, const std::string& value);
00126   void WriteString(const int32 key, const std::string& value);
00127 
00133   void WriteUString(const std::string& key, const std::string& value);
00134   void WriteUString(const int32 key, const std::string& value);
00136 
00143   void WriteBoolVector(const std::string &key, std::vector<bool> &vect);
00144   void WriteBoolVector(const int32 key, std::vector<bool> &vect);
00145 
00146   void WriteIntVector(const std::string &key, std::vector<int32> &vect)
00147     { _WriteDataVector(key, vect); }
00148 
00149   void WriteIntVector(const int32 key, std::vector<int32> &vect)
00150     { _WriteDataVector(key, vect); }
00151 
00152   void WriteUIntVector(const std::string &key, std::vector<int32> &vect)
00153     { _WriteDataVector(key, vect); }
00154 
00155   void WriteUIntVector(const int32 key, std::vector<int32> &vect)
00156     { _WriteDataVector(key, vect); }
00157 
00158   void WriteFloatVector(const std::string &key, std::vector<float> &vect)
00159     { _WriteDataVector(key, vect); }
00160 
00161   void WriteFloatVector(const int32 key, std::vector<float> &vect)
00162     { _WriteDataVector(key, vect); }
00163 
00164   void WriteStringVector(const std::string &key, std::vector<std::string> &vect);
00165   void WriteStringVector(const int32 key, std::vector<std::string> &vect);
00166   void WriteUStringVector(const std::string &key, std::vector<std::string> &vect);
00167   void WriteUStringVector(const int32 key, std::vector<std::string> &vect);
00169 
00178   void BeginTable(const std::string &key);
00179   void BeginTable(int32 key);
00180   void EndTable();
00182 
00183 private:
00185   std::ofstream _outfile;
00186 
00188   bool _inside_comment_block;
00189 
00197   template <class T> void _WriteData(const std::string& key, T value);
00198   template <class T> void _WriteData(const int32 key, T value);
00200 
00208   template <class T> void _WriteDataVector(const std::string& key, std::vector<T> &vect);
00209   template <class T> void _WriteDataVector(const int32 key, std::vector<T> &vect);
00211 
00213   void _WriteTablePath();
00214 }; // class WriteScriptDescriptor : public ScriptDescriptor
00215 
00216 //-----------------------------------------------------------------------------
00217 // Template Function Definitions
00218 //-----------------------------------------------------------------------------
00219 
00220 template <class T> void WriteScriptDescriptor::_WriteData(const std::string &key, T value) {
00221   if (_open_tables.size() == 0) {
00222     _outfile << key << " = " << value << std::endl;
00223   }
00224   else {
00225     _WriteTablePath();
00226     _outfile << '[' << key << ']' << " = " << value << std::endl;
00227   }
00228 }
00229 
00230 
00231 template <class T> void WriteScriptDescriptor::_WriteData(const int32 key, T value) {
00232   if (_open_tables.empty()) {
00233     _error_messages << "* WriteScriptDescriptor::_WriteData() failed because there were no "
00234       << "tables open when attempting to write the key/value: [" << key << "] = " << value << std::endl;
00235     return;
00236   }
00237 
00238   _WriteTablePath();
00239   _outfile << '[' << key << ']' << " = " << value << std::endl;
00240 }
00241 
00242 
00243 
00244 template <class T> void WriteScriptDescriptor::_WriteDataVector(const std::string& key, std::vector<T> &vect) {
00245   if (vect.empty()) {
00246     _error_messages << "* WriteScriptDescriptor::_WriteDataVector() failed because "
00247       << "the vector argument was empty for key name: " << key << std::endl;
00248     return;
00249   }
00250 
00251   if (_open_tables.size() == 0) {
00252     _outfile << key << " = { ";
00253   }
00254   else {
00255     _WriteTablePath();
00256     _outfile << '[' << key << "] = { ";
00257   }
00258 
00259   _outfile << vect[0];
00260   for (uint32 i = 1; i < vect.size(); i++) {
00261     _outfile << ", " << vect[i];
00262   }
00263   _outfile << " }" << std::endl;
00264 }
00265 
00266 
00267 
00268 template <class T> void WriteScriptDescriptor::_WriteDataVector(const int32 key, std::vector<T>& vect) {
00269   if (_open_tables.empty()) {
00270     _error_messages << "* WriteScriptDescriptor::_WriteDataVector() failed because there were no "
00271       << "tables open when attempting for key name: " << key << std::endl;
00272     return;
00273   }
00274 
00275   _WriteTablePath();
00276   _outfile << '[' << key << "] = { ";
00277 
00278   _outfile << vect[0];
00279   for (uint32 i = 1; i < vect.size(); i++) {
00280     _outfile << ", " << vect[i];
00281   }
00282   _outfile << " }" << std::endl;
00283 }
00284 
00285 } // namespace hoa_script
00286 
00287 #endif // __SCRIPT_WRITE_HEADER_

Generated on Fri Jul 6 23:11:22 2007 for Hero of Allacrost by  doxygen 1.5.1