git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / console / ConsoleFileReader.cpp
blob143a28d3cb8b76dc1006d474eace176148d25856
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <console/ConsoleImpl.h>
22 #include <console/ConsoleFileReader.h>
23 #include <console/Console.h>
24 #include <common/FileLines.h>
25 #include <common/DefinesString.h>
26 #include <XML/XMLFile.h>
27 #include <XML/XMLParser.h>
28 #include <stdio.h>
30 bool ConsoleFileReader::loadFileIntoConsole(const std::string &fileName,
31 std::string &errorMessage)
33 XMLFile file;
34 if (!file.readFile(fileName))
36 errorMessage = file.getParserError();
37 return false;
39 if (!file.getRootNode()) return true;
41 // Itterate all of the commands in the file
42 std::list<XMLNode *>::iterator childrenItor;
43 for (childrenItor = file.getRootNode()->getChildren().begin();
44 childrenItor != file.getRootNode()->getChildren().end();
45 childrenItor++)
47 XMLNode *currentNode = (*childrenItor);
48 if (strcmp(currentNode->getName(), "command")==0)
50 Console::instance()->addLine(true, currentNode->getContent());
53 return true;
56 void ConsoleFileReader::saveConsoleIntoFile(const std::string &filename)
58 FileLines filelines;
59 filelines.addLine("<commands source=\"Scorched3D\">");
61 std::deque<ConsoleLine *> &lines =
62 ((ConsoleImpl *) Console::instance())->getLines();
63 std::deque<ConsoleLine *>::iterator itor;
64 for (itor = lines.begin();
65 itor != lines.end();
66 itor++)
68 std::string cleanLine;
69 std::string dirtyLine(LangStringUtil::convertFromLang((*itor)->getLine()));
70 XMLNode::removeSpecialChars(dirtyLine, cleanLine);
71 if ((*itor)->getLineType() != ConsoleLine::eNone)
73 filelines.addLine(S3D::formatStringBuffer(" <command>%s</command>",
74 cleanLine.c_str()));
76 else
78 filelines.addLine(S3D::formatStringBuffer(" <!-- %s -->",
79 cleanLine.c_str()));
83 filelines.addLine("</commands>");
84 filelines.writeFile(filename);