1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
20 #include "nel/misc/common.h"
21 #include "nel/misc/i18n.h"
22 #include "nel/misc/path.h"
32 bool CConfig::create(const std::string
&configFileName
, const std::string
&defaultFileName
)
34 NLMISC::CFile::createDirectoryTree(NLMISC::CFile::getPath(configFileName
));
36 // create the basic .cfg
37 FILE *fp
= NLMISC::nlfopen(configFileName
, "w");
39 if (fp
== NULL
) return false;
41 // store full path to default config file
42 fprintf(fp
, "RootConfigFilename = \"%s\";\n", defaultFileName
.c_str());
45 std::string lang
= NLMISC::CI18N::getSystemLanguageCode();
47 const std::vector
<std::string
> &languages
= NLMISC::CI18N::getLanguageCodes();
49 // search if current locale is defined in language codes
50 for(uint i
= 0; i
< languages
.size(); ++i
)
52 if (lang
== languages
[i
])
54 // store the language code in the config file
55 fprintf(fp
, "LanguageCode = \"%s\";\n", lang
.c_str());
65 bool CConfig::load(const std::string
&fileName
)
71 std::string def
= getString("RootConfigFilename");
75 catch (const NLMISC::Exception
&e
)
77 nlwarning( "%s", e
.what() );
84 bool CConfig::reload()
91 catch (const NLMISC::Exception
&e
)
93 nlwarning( "%s", e
.what() );
99 void CConfig::revertToDefault()
101 // If there's no default config, all we can do is revert the current changes
108 // If there is a default config, we can however revert to the default!
109 // Code taken from the original config tool
110 uint32 count
= cf
.getNumVar();
112 for( i
= 0; i
< count
; i
++ )
114 NLMISC::CConfigFile::CVar
*dst
= cf
.getVar( i
);
116 // Comment from the original
117 // Temp: avoid changing this variable (debug: binded to the actual texture set installed)
118 if( dst
->Name
.compare( "HDTextureInstalled" ) == 0 )
121 NLMISC::CConfigFile::CVar
*src
= dcf
.getVarPtr( dst
->Name
);
122 if( ( src
!= NULL
) && !dst
->Root
&&
123 ( (( src
->Type
== NLMISC::CConfigFile::CVar::T_INT
) && ( dst
->Type
== NLMISC::CConfigFile::CVar::T_INT
)) ||
124 (( src
->Type
== NLMISC::CConfigFile::CVar::T_REAL
) && ( dst
->Type
== NLMISC::CConfigFile::CVar::T_INT
)) ||
125 (( src
->Type
== NLMISC::CConfigFile::CVar::T_INT
) && ( dst
->Type
== NLMISC::CConfigFile::CVar::T_REAL
)) ||
126 (( src
->Type
== NLMISC::CConfigFile::CVar::T_REAL
) && ( dst
->Type
== NLMISC::CConfigFile::CVar::T_REAL
)) ||
127 (( src
->Type
== NLMISC::CConfigFile::CVar::T_STRING
) && ( dst
->Type
== NLMISC::CConfigFile::CVar::T_STRING
)) ) )
130 if( src
->Type
== NLMISC::CConfigFile::CVar::T_INT
)
132 setInt( src
->Name
.c_str(), src
->asInt() );
134 else if( src
->Type
== NLMISC::CConfigFile::CVar::T_REAL
)
136 setFloat( src
->Name
.c_str(), src
->asFloat() );
138 else if( src
->Type
== NLMISC::CConfigFile::CVar::T_STRING
)
140 setString( src
->Name
.c_str(), src
->asString() );
152 catch (const NLMISC::Exception
&e
)
154 nlwarning( "%s", e
.what() );
160 bool CConfig::getBool( const char *key
)
162 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
166 return var
->asBool();
170 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
175 sint32
CConfig::getInt( const char *key
)
177 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
185 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
190 float CConfig::getFloat( const char *key
)
192 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
196 return var
->asFloat();
200 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
205 std::string
CConfig::getString( const char *key
)
207 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
211 return var
->asString();
215 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
220 void CConfig::setBool( const char *key
, bool value
)
222 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
226 if( var
->Type
== NLMISC::CConfigFile::CVar::T_BOOL
)
229 var
->setAsString( "true" );
231 var
->setAsString( "false" );
235 nlwarning( "Key %s in %s is not a boolean.", key
, cf
.getFilename().c_str() );
240 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
244 void CConfig::setInt(const char *key
, sint32 value
)
246 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
250 if( var
->Type
== NLMISC::CConfigFile::CVar::T_INT
)
251 var
->setAsInt( value
);
253 nlwarning( "Key %s in %s is not an integer.", key
, cf
.getFilename().c_str() );
257 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
261 void CConfig::setFloat( const char *key
, float value
)
263 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
267 if( var
->Type
== NLMISC::CConfigFile::CVar::T_REAL
)
268 var
->setAsFloat( value
);
270 nlwarning( "Key %s in %s is not a float.", key
, cf
.getFilename().c_str() );
274 nlwarning( "Couldn't find key %s in %s.", key
, cf
.getFilename().c_str() );
278 void CConfig::setString( const char *key
, const std::string
&value
)
280 NLMISC::CConfigFile::CVar
*var
= cf
.getVarPtr( key
);
284 if( var
->Type
== NLMISC::CConfigFile::CVar::T_STRING
)
285 var
->setAsString( value
);
287 nlwarning( "Key %s in %s is not a string.", key
, cf
.getFilename().c_str() );
291 NLMISC::CConfigFile::CVar var
;
292 var
.forceAsString(value
);
293 cf
.insertVar(key
, var
);