aboutsummaryrefslogtreecommitdiff
path: root/src/tests/Resourcetest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/Resourcetest.cc')
-rw-r--r--src/tests/Resourcetest.cc140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/tests/Resourcetest.cc b/src/tests/Resourcetest.cc
new file mode 100644
index 0000000..9379663
--- /dev/null
+++ b/src/tests/Resourcetest.cc
@@ -0,0 +1,140 @@
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#include <cstdio>
32
33using namespace std;
34
35enum TestEnum{TEST1, THIS_IS_TRUE, USE_LOVE};
36
37//----------------
38// Manipulators
39//-----------------
40
41template<>
42void Resource<TestEnum>::
43setFromString(const char *str) {
44 if (strcasecmp(str, "TEST1")==0)
45 *this = TEST1;
46 else if (strcasecmp(str, "THIS_IS_TRUE")==0)
47 *this = THIS_IS_TRUE;
48 else if (strcasecmp(str, "USE_LOVE")==0)
49 *this = USE_LOVE;
50}
51
52template<>
53void Resource<int>::
54setFromString(const char* strval) {
55 int val;
56 if (sscanf(strval, "%d", &val)==1)
57 *this = val;
58}
59
60template<>
61void Resource<std::string>::
62setFromString(const char *strval) {
63 *this = strval;
64}
65
66template<>
67void Resource<bool>::
68setFromString(char const *strval) {
69 if (strcasecmp(strval, "true")==0)
70 *this = true;
71 else
72 *this = false;
73}
74
75//-----------------
76// accessors
77//-----------------
78template<>
79std::string Resource<TestEnum>::
80getString() {
81 switch (m_value) {
82 case TEST1:
83 return string("TEST1");
84 case THIS_IS_TRUE:
85 return string("THIS_IS_TRUE");
86 case USE_LOVE:
87 return string("USE_LOVE");
88 }
89 return string("");
90}
91
92template<>
93std::string Resource<bool>::
94getString() {
95 return std::string(**this == true ? "true" : "false");
96}
97
98template<>
99std::string Resource<int>::
100getString() {
101 char strval[256];
102 sprintf(strval, "%d", **this);
103 return std::string(strval);
104}
105
106template<>
107std::string Resource<string>::
108getString() { return **this; }
109
110int main(int argc, char **argv) {
111
112 ResourceManager rm;
113 // resources
114 Resource<int> val(rm, 123, "session.test", "Session.Test");
115 Resource<bool> boolval(rm, true, "session.bool", "Session.Bool");
116 Resource<string> strval(rm, "none", "string", "String");
117 Resource<TestEnum> enumval(rm, TEST1, "enumval", "EnumVal");
118
119 const char *defaultfile_open = "res",
120 *defaultfile_save = "res_save";
121
122 if (argc>1) {
123 if(!rm.load(argv[1]))
124 cerr<<"Faild to load database: "<<argv[1]<<endl;
125 } else {
126 if (!rm.load(defaultfile_open))
127 cerr<<"Faild to load database: "<<defaultfile_open<<endl;
128 }
129 cerr<<"Value="<<*val<<endl;
130 cerr<<"boolValue="<<boolval.getString()<<endl;
131 cerr<<"strValue="<<*strval<<endl;
132 cerr<<"enumValue="<<enumval.getString()<<endl;
133
134 if (!rm.save(defaultfile_save))
135 cerr<<"Faild to save database to file:"<<defaultfile_save<<endl;
136
137 if (!rm.save("I dont exist", "Not me either"))
138 cerr<<"faild to save and merge database."<<endl;
139 return 0;
140}