ConsoleFileReader.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <console/ConsoleImpl.h>
00022 #include <console/ConsoleFileReader.h>
00023 #include <console/Console.h>
00024 #include <common/FileLines.h>
00025 #include <common/DefinesString.h>
00026 #include <XML/XMLFile.h>
00027 #include <XML/XMLParser.h>
00028 #include <stdio.h>
00029 
00030 bool ConsoleFileReader::loadFileIntoConsole(const std::string &fileName,
00031                                                                                           std::string &errorMessage)
00032 {
00033         XMLFile file;
00034         if (!file.readFile(fileName))
00035         {
00036                 errorMessage = file.getParserError();
00037                 return false;
00038         }
00039         if (!file.getRootNode()) return true;
00040 
00041         // Itterate all of the commands in the file
00042         std::list<XMLNode *>::iterator childrenItor;
00043         for (childrenItor = file.getRootNode()->getChildren().begin();
00044                 childrenItor != file.getRootNode()->getChildren().end();
00045                 childrenItor++)
00046         {
00047                 XMLNode *currentNode = (*childrenItor);         
00048                 if (strcmp(currentNode->getName(), "command")==0)
00049                 {
00050                         Console::instance()->addLine(true, currentNode->getContent());
00051                 }
00052         }
00053         return true;
00054 }
00055 
00056 void ConsoleFileReader::saveConsoleIntoFile(const std::string &filename)
00057 {
00058         FileLines filelines;
00059         filelines.addLine("<commands source=\"Scorched3D\">");
00060 
00061         std::deque<ConsoleLine *> &lines = 
00062                 ((ConsoleImpl *) Console::instance())->getLines();
00063         std::deque<ConsoleLine *>::iterator itor;
00064         for (itor = lines.begin();
00065                 itor != lines.end();
00066                 itor++)
00067         {
00068                 std::string cleanLine;
00069                 std::string dirtyLine(LangStringUtil::convertFromLang((*itor)->getLine()));
00070                 XMLNode::removeSpecialChars(dirtyLine, cleanLine);
00071                 if ((*itor)->getLineType() != ConsoleLine::eNone)
00072                 {
00073                         filelines.addLine(S3D::formatStringBuffer("  <command>%s</command>",
00074                                 cleanLine.c_str()));
00075                 }
00076                 else
00077                 {
00078                         filelines.addLine(S3D::formatStringBuffer("  <!-- %s -->",
00079                                 cleanLine.c_str()));
00080                 }
00081         }
00082 
00083         filelines.addLine("</commands>");
00084         filelines.writeFile(filename);
00085 }

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3