aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-08-24 23:37:37 +0000
committerMike Buland <eichlan@xagasoft.com>2012-08-24 23:37:37 +0000
commit2ff45ffd1a7ef36369c135090b39528860b25684 (patch)
treed5dfc12b29cb684812bd00b72bc61df2ed618488 /src
parent3c38c55a26df5b2d8827b88a99f5bb996c4d13c2 (diff)
downloadlibbu++-2ff45ffd1a7ef36369c135090b39528860b25684.tar.gz
libbu++-2ff45ffd1a7ef36369c135090b39528860b25684.tar.bz2
libbu++-2ff45ffd1a7ef36369c135090b39528860b25684.tar.xz
libbu++-2ff45ffd1a7ef36369c135090b39528860b25684.zip
Ini driver is close, loading isn't working perfectly yet...
Diffstat (limited to 'src')
-rw-r--r--src/unstable/settings.cpp5
-rw-r--r--src/unstable/settingsdriverini.cpp155
-rw-r--r--src/unstable/settingsdriverini.h29
-rw-r--r--src/unstable/settingsdriverregistry.cpp3
4 files changed, 190 insertions, 2 deletions
diff --git a/src/unstable/settings.cpp b/src/unstable/settings.cpp
index 792aff9..b812b60 100644
--- a/src/unstable/settings.cpp
+++ b/src/unstable/settings.cpp
@@ -2,6 +2,7 @@
2 2
3#include "bu/settingsdrivertaf.h" 3#include "bu/settingsdrivertaf.h"
4#include "bu/settingsdriverregistry.h" 4#include "bu/settingsdriverregistry.h"
5#include "bu/settingsdriverini.h"
5 6
6Bu::Settings::Settings( const Bu::UtfString &sCompany, 7Bu::Settings::Settings( const Bu::UtfString &sCompany,
7 const Bu::UtfString &sProduct, Bu::Settings::Driver eDriver ) : 8 const Bu::UtfString &sProduct, Bu::Settings::Driver eDriver ) :
@@ -15,7 +16,7 @@ Bu::Settings::Settings( const Bu::UtfString &sCompany,
15#if defined( WIN32 ) 16#if defined( WIN32 )
16 pDriver = new Bu::SettingsDriverRegistry(); 17 pDriver = new Bu::SettingsDriverRegistry();
17#else 18#else
18 pDriver = new Bu::SettingsDriverTaf(); 19 pDriver = new Bu::SettingsDriverIni();
19#endif 20#endif
20 break; 21 break;
21 22
@@ -24,7 +25,7 @@ Bu::Settings::Settings( const Bu::UtfString &sCompany,
24 break; 25 break;
25 26
26 case DriverIni: 27 case DriverIni:
27 throw Bu::ExceptionBase("Not supported"); 28 pDriver = new Bu::SettingsDriverIni();
28 break; 29 break;
29 } 30 }
30 31
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
diff --git a/src/unstable/settingsdriverini.h b/src/unstable/settingsdriverini.h
new file mode 100644
index 0000000..c3942d6
--- /dev/null
+++ b/src/unstable/settingsdriverini.h
@@ -0,0 +1,29 @@
1#ifndef BU_SETTINGS_DRIVER_INI_H
2#define BU_SETTINGS_DRIVER_INI_H
3
4#include "bu/settingsdriver.h"
5#include "bu/string.h"
6#include "bu/hash.h"
7
8namespace Bu
9{
10 class SettingsDriverIni : public SettingsDriver
11 {
12 public:
13 SettingsDriverIni();
14 virtual ~SettingsDriverIni();
15
16 protected:
17 virtual void init( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct );
18 virtual void set( const Bu::UtfString &sKey, const Bu::UtfString &sValue );
19 virtual Bu::UtfString get( const Bu::UtfString &sKey, const Bu::UtfString &sValue );
20
21 private:
22 Bu::String sPath;
23 typedef Bu::Hash<Bu::UtfString, Bu::UtfString> StrHash;
24 typedef Bu::Hash<Bu::UtfString, StrHash> GroupHash;
25 GroupHash hGroup;
26 };
27};
28
29#endif
diff --git a/src/unstable/settingsdriverregistry.cpp b/src/unstable/settingsdriverregistry.cpp
index 40a08ac..89eda7b 100644
--- a/src/unstable/settingsdriverregistry.cpp
+++ b/src/unstable/settingsdriverregistry.cpp
@@ -1,3 +1,5 @@
1#ifdef WIN32
2
1#include "bu/settingsdriverregistry.h" 3#include "bu/settingsdriverregistry.h"
2#include <windows.h> 4#include <windows.h>
3#include "bu/string.h" 5#include "bu/string.h"
@@ -86,3 +88,4 @@ Bu::UtfString Bu::SettingsDriverRegistry::get( const Bu::UtfString &sKey, const
86 return Bu::UtfString( Bu::String( buf, iRet ) ); 88 return Bu::UtfString( Bu::String( buf, iRet ) );
87} 89}
88 90
91#endif