00001
00002
00003
00004
00005
00006
00007
00009
00017 #include <fstream>
00018
00019 #include "utils.h"
00020
00021 #include "script.h"
00022 #include "script_write.h"
00023
00024 using namespace std;
00025
00026 using namespace hoa_utils;
00027
00028 namespace hoa_script {
00029
00030 WriteScriptDescriptor::~WriteScriptDescriptor() {
00031 if (IsFileOpen()) {
00032 if (SCRIPT_DEBUG)
00033 cerr << "SCRIPT WARNING: WriteScriptDescriptor destructor was called when file was still open: "
00034 << _filename << endl;
00035 CloseFile();
00036 }
00037
00038 _filename = "";
00039 _access_mode = SCRIPT_CLOSED;
00040 _error_messages.clear();
00041 _open_tables.clear();
00042 }
00043
00044
00045
00046
00047
00048 bool WriteScriptDescriptor::OpenFile(const string& file_name) {
00049 if (ScriptManager->IsFileOpen(file_name) == true) {
00050 if (SCRIPT_DEBUG)
00051 cerr << "SCRIPT WARNING: WriteScriptDescriptor::OpenFile() attempted to open file that is already opened: "
00052 << file_name << endl;
00053 return false;
00054 }
00055
00056
00057 if (SCRIPT_DEBUG) {
00058 ifstream temp_file;
00059 temp_file.open(file_name.c_str());
00060 if (temp_file) {
00061 cerr << "SCRIPT WARNING: In WriteScriptDescriptor::OpenFile(), the file to be opened "
00062 << "already exists and will be overwritten: " << file_name << endl;
00063 }
00064 temp_file.close();
00065 }
00066
00067 _outfile.open(file_name.c_str());
00068 if (!_outfile) {
00069 cerr << "SCRIPT ERROR: WriteScriptDescriptor::OpenFile() failed to open the file "
00070 << file_name << " for writing." << endl;
00071 _access_mode = SCRIPT_CLOSED;
00072 return false;
00073 }
00074
00075 _filename = file_name;
00076 _access_mode = SCRIPT_WRITE;
00077 ScriptManager->_AddOpenFile(this);
00078 return true;
00079 }
00080
00081
00082
00083 bool WriteScriptDescriptor::OpenFile() {
00084 if (_filename == "") {
00085 if (SCRIPT_DEBUG)
00086 cerr << "SCRIPT ERROR: WriteScriptDescriptor::OpenFile() could not open file "
00087 << "because of an invalid file name (empty string)." << endl;
00088 return false;
00089 }
00090
00091 return OpenFile(_filename);
00092 }
00093
00094
00095
00096 void WriteScriptDescriptor::CloseFile() {
00097 if (IsFileOpen() == false) {
00098 if (SCRIPT_DEBUG)
00099 cerr << "SCRIPT ERROR: in WriteScriptDescriptor::CloseFile(), could not close the "
00100 << "file because it was not open." << endl;
00101 return;
00102 }
00103
00104 if (SCRIPT_DEBUG && IsErrorDetected()) {
00105 cerr << "SCRIPT WARNING: In WriteScriptDescriptor::CloseFile(), the file " << _filename
00106 << " had error messages remaining. They are as follows:" << endl;
00107 cerr << _error_messages.str() << endl;
00108 }
00109
00110 _outfile.close();
00111 _error_messages.clear();
00112 _open_tables.clear();
00113 _access_mode = SCRIPT_CLOSED;
00114 ScriptManager->_RemoveOpenFile(this);
00115 }
00116
00117
00118
00119
00120
00121 void WriteScriptDescriptor::InsertNewLine() {
00122 _outfile << endl;
00123 }
00124
00125
00126
00127 void WriteScriptDescriptor::WriteComment(const string& comment) {
00128 _outfile << "-- " << comment << endl;
00129 }
00130
00131
00132
00133 void WriteScriptDescriptor::BeginCommentBlock() {
00134 if (_inside_comment_block == true) {
00135 _error_messages << "* WriteScriptDescriptor::BeginCommentBlock() was already "
00136 << "inside a comment block when it was called" << endl;
00137 return;
00138 }
00139
00140 _inside_comment_block = true;
00141 _outfile << "--[[" << endl;
00142 }
00143
00144
00145
00146 void WriteScriptDescriptor::EndCommentBlock() {
00147 if (_inside_comment_block == false) {
00148 _error_messages << "* WriteScriptDescriptor::EndCommentBlock() was not "
00149 << "inside a comment block when it was called" << endl;
00150 return;
00151 }
00152
00153 _inside_comment_block = false;
00154 _outfile << "--]]" << endl;
00155 }
00156
00157
00158
00159 void WriteScriptDescriptor::WriteLine(const string& comment, bool new_line) {
00160 _outfile << comment;
00161 if (new_line)
00162 _outfile << endl;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 void WriteScriptDescriptor::WriteBool(const string& key, bool value) {
00172 if (_open_tables.size() == 0) {
00173 _outfile << key << " = ";
00174 if (value)
00175 _outfile << "true" << endl;
00176 else
00177 _outfile << "false" << endl;
00178 }
00179 else {
00180 _WriteTablePath();
00181 _outfile << '[' << key << ']' << " = ";
00182 if (value)
00183 _outfile << "true" << endl;
00184 else
00185 _outfile << "false" << endl;
00186 }
00187 }
00188
00189
00190
00191 void WriteScriptDescriptor::WriteBool(const int32 key, bool value) {
00192 if (_open_tables.empty()) {
00193 _error_messages << "* WriteScriptDescriptor::WriteBool() failed because there were no "
00194 << "tables open when attempting to write the key/value: " << key << " = " << value << std::endl;
00195 return;
00196 }
00197
00198 _WriteTablePath();
00199 _outfile << '[' << key << ']' << " = ";
00200 if (value)
00201 _outfile << "true" << endl;
00202 else
00203 _outfile << "false" << endl;
00204 }
00205
00206
00207
00208
00209 void WriteScriptDescriptor::WriteString(const string& key, const string& value) {
00210 if (_open_tables.size() == 0) {
00211 _outfile << key << " = \"" << value << "\"" << endl;
00212 }
00213 else {
00214 _WriteTablePath();
00215 _outfile << '[' << key << ']' << " = \"" << value << "\"" << endl;
00216 }
00217 }
00218
00219
00220
00221 void WriteScriptDescriptor::WriteString(const int32 key, const string& value) {
00222 if (_open_tables.empty()) {
00223 _error_messages << "* WriteScriptDescriptor::WriteString() failed because there were no "
00224 << "tables open when attempting to write the key/value: [" << key << "] = " << value << std::endl;
00225 return;
00226 }
00227
00228 _WriteTablePath();
00229 _outfile << '[' << key << ']' << " = \"" << value << "\"" << endl;
00230 }
00231
00232
00233
00234
00235 void WriteScriptDescriptor::WriteUString(const string& key, const string& value) {
00236 WriteString(key, value);
00237 }
00238
00239
00240
00241 void WriteScriptDescriptor::WriteUString(const int32 key, const string& value) {
00242 WriteString(key, value);
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 void WriteScriptDescriptor::WriteBoolVector(const string& key, std::vector<bool>& vect) {
00252 if (vect.empty()) {
00253 _error_messages << "* WriteScriptDescriptor::WriteBoolVector() failed because "
00254 << "the vector argument was empty for key name: " << key << endl;
00255 return;
00256 }
00257
00258 if (_open_tables.size() == 0) {
00259 _outfile << key << " = { ";
00260 }
00261 else {
00262 _WriteTablePath();
00263 _outfile << '[' << key << "] = { ";
00264 }
00265
00266 if (vect[0])
00267 _outfile << "true";
00268 else
00269 _outfile << "false";
00270 for (uint32 i = 1; i < vect.size(); i++) {
00271 if (vect[i])
00272 _outfile << ", true";
00273 else
00274 _outfile << ", false";
00275 }
00276 _outfile << " }" << endl;
00277 }
00278
00279
00280
00281 void WriteScriptDescriptor::WriteBoolVector(const int32 key, std::vector<bool>& vect) {
00282 if (_open_tables.empty()) {
00283 _error_messages << "* WriteScriptDescriptor::WriteBoolVector() failed because there were no "
00284 << "tables open when attempting for key name: " << key << endl;
00285 return;
00286 }
00287
00288 _WriteTablePath();
00289 _outfile << '[' << key << "] = { ";
00290
00291 if (vect[0])
00292 _outfile << "true";
00293 else
00294 _outfile << "false";
00295 for (uint32 i = 1; i < vect.size(); i++) {
00296 if (vect[i])
00297 _outfile << ", true";
00298 else
00299 _outfile << ", false";
00300 }
00301 _outfile << " }" << endl;
00302 }
00303
00304
00305
00306
00307 void WriteScriptDescriptor::WriteStringVector(const string &key, std::vector<string>& vect) {
00308 if (vect.empty()) {
00309 _error_messages << "* WriteScriptDescriptor::WriteStringVector() failed because there were no "
00310 << "tables open when attempting for key name: " << key << endl;
00311 return;
00312 }
00313
00314 if (_open_tables.size() == 0) {
00315 _outfile << key << " = { ";
00316 }
00317 else {
00318 _WriteTablePath();
00319 _outfile << '[' << key << "] = { ";
00320 }
00321
00322 _outfile << "\"" << vect[0] << "\"";
00323 for (uint32 i = 1; i < vect.size(); i++) {
00324 _outfile << ", \"" << vect[i] << "\"";
00325 }
00326 _outfile << " }" << endl;
00327 }
00328
00329
00330
00331 void WriteScriptDescriptor::WriteStringVector(const int32 key, std::vector<string>& vect) {
00332 if (vect.empty()) {
00333 _error_messages << "* WriteScriptDescriptor::WriteStringVector() failed because there were no "
00334 << "tables open when attempting for key name: " << key << endl;
00335 return;
00336 }
00337 if (_open_tables.empty()) {
00338 _error_messages << "* WriteScriptDescriptor::WriteSringVector() failed because there were no "
00339 << "tables open when attempting for key name: " << key << endl;
00340 return;
00341 }
00342
00343 _WriteTablePath();
00344 _outfile << '[' << key << "] = { ";
00345
00346 _outfile << "\"" << vect[0] << "\"";
00347 for (uint32 i = 1; i < vect.size(); i++) {
00348 _outfile << ", \"" << vect[i] << "\"";
00349 }
00350 _outfile << " }" << endl;
00351 }
00352
00353
00354
00355
00356
00357
00358 void WriteScriptDescriptor::WriteUStringVector(const string& key, std::vector<string>& vect) {
00359 WriteStringVector(key, vect);
00360 }
00361
00362
00363
00364 void WriteScriptDescriptor::WriteUStringVector(const int32 key, std::vector<string>& vect) {
00365 WriteStringVector(key, vect);
00366 }
00367
00368
00369
00370
00371
00372 void WriteScriptDescriptor::BeginTable(const string &key) {
00373 if (_open_tables.size() == 0) {
00374 _outfile << key << " = {}" << endl;
00375 }
00376 else {
00377 _WriteTablePath();
00378 _outfile << '[' << key << "] = {}" << endl;
00379 }
00380
00381 _open_tables.push_back(key);
00382 }
00383
00384
00385
00386 void WriteScriptDescriptor::BeginTable(int32 key) {
00387 if (_open_tables.size() == 0)
00388 _outfile << key << " = {}" << endl;
00389 else {
00390 _WriteTablePath();
00391 _outfile << '[' << key << "] = {}" << endl;
00392 }
00393
00394 _open_tables.push_back(NumberToString<int32>(key));
00395 }
00396
00397
00398
00399 void WriteScriptDescriptor::EndTable() {
00400 if (_open_tables.empty()) {
00401 _error_messages << "* WriteScriptDescriptor::EndTable() failed because no tables were open" << endl;
00402 }
00403 else {
00404 _open_tables.pop_back();
00405 }
00406 }
00407
00408
00409
00410
00411
00412
00413 void WriteScriptDescriptor::_WriteTablePath() {
00414 if (_open_tables.empty()) {
00415 _error_messages << "* WriteScriptDescriptor::_WriteTablePath() failed because there were no "
00416 << "tables open" << endl;
00417 return;
00418 }
00419
00420 _outfile << _open_tables[0];
00421 for (uint32 i = 1; i < _open_tables.size(); i++) {
00422 _outfile << '[' << _open_tables[i] << ']';
00423 }
00424 }
00425
00426 }