Re: [Vala] ConfigParser



El 22/10/11 10:44, "Luis L. Rodríguez Oro" escribió:
hello.

I need save the config of my app. Something like configParser of python. exists any similar for Vala?

Answer my self:

class Ini {
    private string file;
    KeyFile kf = new KeyFile();

    public Ini(string file){
        this.setFile(file);
    }

    public void parse(){
        try {
            kf.load_from_file(this.file, KeyFileFlags.NONE);
        } catch (FileError err) {
            warning (err.message);
        }
    }

public void add(string seccion, string[] arrayKey, string[] arrayValue){
        int c = arrayKey.length;
        string k; string v;

        for (int i = 0;i < c;i++ ){
            k = arrayKey[i];
            v = arrayValue[i];

            kf.set_string (seccion, k, v);
        }
    }

    public void save(){
        var str = kf.to_data (null);

        try {
            FileUtils.set_contents (this.file, str, str.length);
        } catch (FileError err) {
            warning (err.message);
        }
    }

    public string getValue(string seccion, string key){
        return _getValue(seccion, key);
    }

    public bool getBool(string seccion, string key){
        string v = _getValue(seccion, key);

        if (v.up() == "TRUE"){
            return true;
        }else{
            return false;
        }
    }

    public int getInt(string seccion, string key){
        string v = _getValue(seccion, key);

        return v.to_int();
    }

    public string _getValue(string seccion, string key){
        string v = "";
        try {
            v = kf.get_value (seccion, key);
        } catch (KeyFileError err) {
            warning (err.message);
        }

        return v;
    }

    private void setFile(string param){this.file = param;}
}

void main () {
    Ini a = new Ini("test.ini");

   //escribir
    string[] k = {"a","b"};
    string[] v = {"100","200"};

    a.add("Options",k,v);
    a.add("Config",k,v);
    a.save();

    //Leer
    a.parse();
    string v = a.getValue("Options","a");
    bool b = a.getBool("Options","exitoninternetfinish");
    int k = a.getInt("Options","a");

}

//day 4 study Vala.
Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES 
DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]