aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/settingsdriverini.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/unstable/settingsdriverini.cpp')
-rw-r--r--src/unstable/settingsdriverini.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/unstable/settingsdriverini.cpp b/src/unstable/settingsdriverini.cpp
new file mode 100644
index 0000000..46a40c6
--- /dev/null
+++ b/src/unstable/settingsdriverini.cpp
@@ -0,0 +1,155 @@
1#include "bu/settingsdriverini.h"
2
3#include "bu/file.h"
4#include "bu/buffer.h"
5
6#include "bu/sio.h"
7
8#include <stdlib.h>
9
10using namespace Bu;
11
12Bu::SettingsDriverIni::SettingsDriverIni()
13{
14}
15
16Bu::SettingsDriverIni::~SettingsDriverIni()
17{
18 Bu::File fOut( sPath, Bu::File::WriteNew );
19 Bu::Buffer bOut( fOut );
20 Bu::Formatter f( bOut );
21
22 for( GroupHash::iterator i = hGroup.begin(); i; i++ )
23 {
24 f << "[" << i.getKey().get() << "]" << f.nl;
25 for( StrHash::iterator k = (*i).begin(); k; k++ )
26 {
27 f << k.getKey().get() << "=" << k.getValue().get() << f.nl;
28 }
29 f << f.nl;
30 }
31}
32
33void Bu::SettingsDriverIni::init( const Bu::UtfString &sCompany,
34 const Bu::UtfString &sProduct )
35{
36 Bu::UtfString us( getenv("HOME") );
37 us += "/.config/";
38 us += sCompany;
39 us += "/";
40 us += sProduct;
41 sPath = us.get();
42 try
43 {
44 Bu::File fIn( sPath, Bu::File::Read|Bu::File::Create );
45 Bu::Buffer bIn( fIn );
46 StrHash hKeys;
47 Bu::String sGroup;
48 while( !bIn.isEos() )
49 {
50 Bu::String sIn = bIn.readLine();
51 if( sIn[0] == '[' )
52 {
53 if( !sGroup.isEmpty() )
54 {
55 hGroup.insert( sGroup, hKeys );
56 hKeys.clear();
57 }
58 sGroup = Bu::String( sIn.begin()+1, sIn.find(']') );
59 }
60 else
61 {
62 Bu::String::iterator i = sIn.find('=');
63 if( !i )
64 continue;
65
66 hKeys.insert( Bu::String( sIn.begin(), i ),
67 Bu::String( i+1, sIn.end() ) );
68 }
69 }
70 }
71 catch(...)
72 {
73 }
74}
75
76void Bu::SettingsDriverIni::set( const Bu::UtfString &sKey, const Bu::UtfString &sValue )
77{
78 Bu::String suKey = sKey.get();
79 Bu::String::iterator i = suKey.find('/');
80 Bu::UtfString sGrp;
81 Bu::UtfString sId;
82 if( !i )
83 {
84 sGrp = "";
85 sId = sKey;
86 }
87 else
88 {
89 Bu::String::iterator in;
90 for(;;)
91 {
92 in = i;
93 i = (in + 1).find('/');
94 if( !i )
95 break;
96 }
97
98 sGrp.set( Bu::String( suKey.begin(), in ) );
99 sId.set( Bu::String( in+1, suKey.end() ) );
100 }
101
102// sio << "Group: " << sGrp.get() << sio.nl
103// << "Key: " << sId.get() << sio.nl;
104
105 if( !hGroup.has( sGrp ) )
106 {
107 StrHash hVal;
108 hVal.insert( sId, sValue );
109 hGroup.insert( sGrp, hVal );
110 }
111 else
112 {
113 hGroup.get( sGrp ).insert( sId, sValue );
114 }
115}
116
117Bu::UtfString Bu::SettingsDriverIni::get( const Bu::UtfString &sKey, const Bu::UtfString &sValue )
118{
119 Bu::String suKey = sKey.get();
120 Bu::String::iterator i = suKey.find('/');
121 Bu::UtfString sGrp;
122 Bu::UtfString sId;
123 if( !i )
124 {
125 sGrp = "";
126 sId = sKey;
127 }
128 else
129 {
130 Bu::String::iterator in;
131 for(;;)
132 {
133 in = i;
134 i = (in + 1).find('/');
135 if( !i )
136 break;
137 }
138
139 sGrp.set( Bu::String( suKey.begin(), in ) );
140 sId.set( Bu::String( in+1, suKey.end() ) );
141 }
142
143 sio << "Group: " << sGrp.get() << sio.nl
144 << "Key: " << sId.get() << sio.nl;
145
146 try
147 {
148 return hGroup.get( sGrp ).get( sId );
149 }
150 catch(...)
151 {
152 return sValue;
153 }
154}
155