Retourner au contenu associé (journal : Requête aux devs de logiciels libres)
Posté par lolop (site web personnel) le 05 janvier 2010 à 13:58. En réponse au journal Requête aux devs de logiciels libres. Évalué à 2.
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
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
[^] # Re: Vous devez entrer un sujet et un commentaire
Posté par lolop (site web personnel) . En réponse au journal Requête aux devs de logiciels libres. Évalué à 2.
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