Retourner au contenu associé (entrée de forum : parser un fichier de config)
Posté par lolop (site web personnel) le 07 novembre 2006 à 14:31. En réponse au message parser un fichier de config. Évalué à 3.
Votez les 30 juin et 7 juillet, en connaissance de cause. http://www.pointal.net/VotesDeputesRN
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
# Solution C++
Posté par lolop (site web personnel) . En réponse au message parser un fichier de config. Évalué à 3.
http://www.limsi.fr/Individu/pointal/cpp.html#sources
C'est même tellement court, que je te le poste ici:
typedef map<string,string> ConfigMap;
struct ConfigError
{
string message ;
inline ConfigError(const string& msg)
{ message = msg ; } ;
} ;
class Config
{
private:
ConfigMap m_pairs ;
public:
Config(const char* filepath) ;
void get(const char* key, string& value, const char* defval=NULL) const ;
} ;
/*=============================================================================
LECTURE DE CONFIGURATION
=============================================================================*/
Config::Config(const char* filepath)
{
string s ;
ifstream configfile(filepath) ;
while (configfile)
{
getline(configfile,s) ;
if (not s.size() || s[0]=='#') continue ; // Ignore lignes vides et commentaires.
int offset = s.find('=') ;
string key(s.substr(0,offset)) ;
string value(s.substr(offset+1,s.size())) ;
m_pairs[key] = value ;
}
}
void Config::get(const char* key, string& value, const char* defval) const
{
ConfigMap::const_iterator found = m_pairs.find(key) ;
if (found == m_pairs.end()) // Retourne default.
if (defval != NULL)
value = defval ;
else
throw ConfigError((string("Unknown key: ")+key)) ;
else
value = (*found).second ;
}
Votez les 30 juin et 7 juillet, en connaissance de cause. http://www.pointal.net/VotesDeputesRN