Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / tools / leveldesign / master / easy_cfg.cpp
blobf8307de88af9727c2222c44b66330f5feca99d16
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdafx.h"
18 #include "easy_cfg.h"
20 #include "nel/misc/config_file.h"
22 using namespace NLMISC;
23 using namespace std;
25 // ---------------------------------------------------------------------------
26 IEasyCFG::IEasyCFG()
28 cf = NULL;
29 f = NULL;
32 // ---------------------------------------------------------------------------
33 IEasyCFG::~IEasyCFG()
35 if (cf != NULL)
36 delete cf;
37 cf = NULL;
38 if (f != NULL)
39 fclose(f);
40 f = NULL;
43 // ---------------------------------------------------------------------------
44 bool IEasyCFG::openRead (const std::string &filename)
46 try
48 FILE *fTemp = fopen (filename.c_str(), "rt");
49 if (fTemp == NULL)
50 return false;
51 else
52 fclose(fTemp);
54 cf = new CConfigFile;
55 cf->load (filename);
58 catch (Exception &)
60 return false;
62 return true;
65 // ---------------------------------------------------------------------------
66 bool IEasyCFG::openWrite (const std::string &filename)
68 f = fopen (filename.c_str(), "wt");
69 if (f == NULL)
70 return false;
71 return true;
74 // ---------------------------------------------------------------------------
75 void IEasyCFG::close ()
77 if (cf != NULL)
78 delete cf;
79 cf = NULL;
80 if (f != NULL)
81 fclose (f);
82 f = NULL;
85 // ---------------------------------------------------------------------------
86 sint32 IEasyCFG::getInt (const string &sVarName)
88 if (cf == NULL)
89 return 0;
90 try
92 CConfigFile::CVar &cv = cf->getVar(sVarName);
93 return cv.asInt();
95 catch (Exception &)
97 return 0;
101 // ---------------------------------------------------------------------------
102 void IEasyCFG::putInt (const string &sVarName, sint32 sVarValue)
104 if (f == NULL)
105 return;
106 fprintf (f, "%s = %d;\n", sVarName.c_str(), sVarValue);
109 // ---------------------------------------------------------------------------
110 string IEasyCFG::getStr (const string &sVarName)
112 if (cf == NULL)
113 return string("");
116 CConfigFile::CVar &cv = cf->getVar(sVarName);
117 return cv.asString();
119 catch (Exception &)
121 return string("");
125 // ---------------------------------------------------------------------------
126 void IEasyCFG::putStr (const string &sVarName, const string &sVarValue)
128 if (f == NULL)
129 return;
130 fprintf (f, "%s = \"%s\";\n", sVarName.c_str(), sVarValue.c_str());
133 // ---------------------------------------------------------------------------
134 bool IEasyCFG::getBool (const string &sVarName)
136 if (cf == NULL)
137 return false;
138 return (getStr (sVarName) == "true" ? true : false);
141 // ---------------------------------------------------------------------------
142 void IEasyCFG::putBool (const string &sVarName, bool sVarValue)
144 if (f == NULL)
145 return;
146 if (sVarValue)
147 fprintf (f, "%s = \"true\";\n", sVarName.c_str());
148 else
149 fprintf (f, "%s = \"false\";\n", sVarName.c_str());
152 // ---------------------------------------------------------------------------
153 void IEasyCFG::putCommentLine (const std::string &sComments)
155 if (f == NULL)
156 return;
157 fprintf (f, "// %s\n", sComments.c_str());