• # Solution C++

    Posté par (site web personnel) . En réponse au message parser un fichier de config. Évalué à 3.

    Si tu n'es pas complètement bloqué 'C', j'avais fait un tout petit outil qui me faisait ça tout seul en C++.

    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