aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/settingsdrivertaf.cpp
blob: 3fd61b317f59b4bb966a351890df0c622094ea56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "bu/settingsdrivertaf.h"

#include "bu/file.h"
#include "bu/taf.h"

#include <stdlib.h>

Bu::SettingsDriverTaf::SettingsDriverTaf() :
    pRoot( NULL )
{
}

Bu::SettingsDriverTaf::~SettingsDriverTaf()
{
    if( !pRoot )
        return;

    Bu::File fOut( sPath, Bu::File::WriteNew );
    Bu::TafWriter tw( fOut );
    tw.writeGroup( pRoot );
    delete pRoot;
}

void Bu::SettingsDriverTaf::init( const Bu::UtfString &sCompany,
        const Bu::UtfString &sProduct )
{
    Bu::UtfString us( getenv("HOME") );
    us += "/.config/";
    us += sCompany;
    us += "/";
    us += sProduct;
    sPath = us.get();
    try
    {
        Bu::File fIn( sPath, Bu::File::Read|Bu::File::Create );
        Bu::TafReader tr( fIn );
        pRoot = tr.readGroup();
    }
    catch(...)
    {
    }
    if( !pRoot )
    {
        pRoot = new Bu::TafGroup( sProduct.get() );
    }
}

void Bu::SettingsDriverTaf::set( const Bu::UtfString &sKey,
        const Bu::UtfString &sValue )
{
    Bu::StringList lPath = sKey.get().split('/');
    Bu::StringList::iterator i = lPath.begin();
    Bu::StringList::iterator in;
    Bu::TafGroup *pGrp = pRoot;
    for(; i;)
    {
        in = i;
        in++;
        if( in )
        {
            if( pGrp->hasChild( *i ) )
                pGrp = (Bu::TafGroup *)pGrp->getChild( *i );
            else
                pGrp = pGrp->addGroup( *i );
        }
        else
        {
            pGrp->addProperty( *i, sValue.get() );
        }
        i = in;
    }
}

Bu::UtfString Bu::SettingsDriverTaf::get( const Bu::UtfString &sKey,
        const Bu::UtfString &sValue )
{
    Bu::StringList lPath = sKey.get().split('/');
    Bu::StringList::iterator i = lPath.begin();
    Bu::StringList::iterator in;
    Bu::TafGroup *pGrp = pRoot;
    for(; i;)
    {
        in = i;
        in++;
        if( in )
        {
            if( pGrp->hasChild( *i ) )
                pGrp = (Bu::TafGroup *)pGrp->getChild( *i );
            else
                return sValue;
        }
        else
        {
            if( pGrp->hasProperty( *i ) )
                return pGrp->getProperty( *i );
            else
                return sValue;
        }
        i = in;
    }
}