diff options
author | fluxgen <fluxgen> | 2002-02-04 23:48:31 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-02-04 23:48:31 (GMT) |
commit | ccb2beb0e1e078a33a8603ec591adadf78ff148a (patch) | |
tree | 7ce7268d6262feda35d4ce395656098cc784681f /src/tests/Resourcetest.cc | |
parent | 26754cd47722ac36380a7a2f8bd56e8a40b951ce (diff) | |
download | fluxbox_pavel-ccb2beb0e1e078a33a8603ec591adadf78ff148a.zip fluxbox_pavel-ccb2beb0e1e078a33a8603ec591adadf78ff148a.tar.bz2 |
initial import
Diffstat (limited to 'src/tests/Resourcetest.cc')
-rw-r--r-- | src/tests/Resourcetest.cc | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/tests/Resourcetest.cc b/src/tests/Resourcetest.cc new file mode 100644 index 0000000..8cfae58 --- /dev/null +++ b/src/tests/Resourcetest.cc | |||
@@ -0,0 +1,139 @@ | |||
1 | // Resourcetest.cc | ||
2 | // Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "Resource.hh" | ||
23 | |||
24 | //use of strcasecmp | ||
25 | #ifndef _GNU_SOURCE | ||
26 | #define _GNU_SOURCE | ||
27 | #endif // _GNU_SOURCE | ||
28 | |||
29 | #include <string> | ||
30 | #include <iostream> | ||
31 | |||
32 | using namespace std; | ||
33 | |||
34 | enum TestEnum{TEST1, THIS_IS_TRUE, USE_LOVE}; | ||
35 | |||
36 | //---------------- | ||
37 | // Manipulators | ||
38 | //----------------- | ||
39 | |||
40 | template<> | ||
41 | void Resource<TestEnum>:: | ||
42 | setFromString(const char *str) { | ||
43 | if (strcasecmp(str, "TEST1")==0) | ||
44 | *this = TEST1; | ||
45 | else if (strcasecmp(str, "THIS_IS_TRUE")==0) | ||
46 | *this = THIS_IS_TRUE; | ||
47 | else if (strcasecmp(str, "USE_LOVE")==0) | ||
48 | *this = USE_LOVE; | ||
49 | } | ||
50 | |||
51 | template<> | ||
52 | void Resource<int>:: | ||
53 | setFromString(const char* strval) { | ||
54 | int val; | ||
55 | if (sscanf(strval, "%d", &val)==1) | ||
56 | *this = val; | ||
57 | } | ||
58 | |||
59 | template<> | ||
60 | void Resource<std::string>:: | ||
61 | setFromString(const char *strval) { | ||
62 | *this = strval; | ||
63 | } | ||
64 | |||
65 | template<> | ||
66 | void Resource<bool>:: | ||
67 | setFromString(char const *strval) { | ||
68 | if (strcasecmp(strval, "true")==0) | ||
69 | *this = true; | ||
70 | else | ||
71 | *this = false; | ||
72 | } | ||
73 | |||
74 | //----------------- | ||
75 | // accessors | ||
76 | //----------------- | ||
77 | template<> | ||
78 | std::string Resource<TestEnum>:: | ||
79 | getString() { | ||
80 | switch (m_value) { | ||
81 | case TEST1: | ||
82 | return string("TEST1"); | ||
83 | case THIS_IS_TRUE: | ||
84 | return string("THIS_IS_TRUE"); | ||
85 | case USE_LOVE: | ||
86 | return string("USE_LOVE"); | ||
87 | } | ||
88 | return string(""); | ||
89 | } | ||
90 | |||
91 | template<> | ||
92 | std::string Resource<bool>:: | ||
93 | getString() { | ||
94 | return std::string(**this == true ? "true" : "false"); | ||
95 | } | ||
96 | |||
97 | template<> | ||
98 | std::string Resource<int>:: | ||
99 | getString() { | ||
100 | char strval[256]; | ||
101 | sprintf(strval, "%d", **this); | ||
102 | return std::string(strval); | ||
103 | } | ||
104 | |||
105 | template<> | ||
106 | std::string Resource<string>:: | ||
107 | getString() { return **this; } | ||
108 | |||
109 | int main(int argc, char **argv) { | ||
110 | |||
111 | ResourceManager rm; | ||
112 | // resources | ||
113 | Resource<int> val(rm, 123, "session.test", "Session.Test"); | ||
114 | Resource<bool> boolval(rm, true, "session.bool", "Session.Bool"); | ||
115 | Resource<string> strval(rm, "none", "string", "String"); | ||
116 | Resource<TestEnum> enumval(rm, TEST1, "enumval", "EnumVal"); | ||
117 | |||
118 | const char *defaultfile_open = "res", | ||
119 | *defaultfile_save = "res_save"; | ||
120 | |||
121 | if (argc>1) { | ||
122 | if(!rm.load(argv[1])) | ||
123 | cerr<<"Faild to load database: "<<argv[1]<<endl; | ||
124 | } else { | ||
125 | if (!rm.load(defaultfile_open)) | ||
126 | cerr<<"Faild to load database: "<<defaultfile_open<<endl; | ||
127 | } | ||
128 | cerr<<"Value="<<*val<<endl; | ||
129 | cerr<<"boolValue="<<boolval.getString()<<endl; | ||
130 | cerr<<"strValue="<<*strval<<endl; | ||
131 | cerr<<"enumValue="<<enumval.getString()<<endl; | ||
132 | |||
133 | if (!rm.save(defaultfile_save)) | ||
134 | cerr<<"Faild to save database to file:"<<defaultfile_save<<endl; | ||
135 | |||
136 | if (!rm.save("I dont exist", "Not me either")) | ||
137 | cerr<<"faild to save and merge database."<<endl; | ||
138 | return 0; | ||
139 | } | ||