~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/StelModule.cpp

Tags: upstream-0.9.0
Import upstream version 0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2006 Fabien Chereau
 
4
 * 
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "StelModule.hpp"
 
21
#include "vecmath.h"
 
22
#include "InitParser.hpp"
 
23
#include "StelApp.hpp"
 
24
 
 
25
/*************************************************************************
 
26
 Get the version of the module, default is stellarium main version
 
27
*************************************************************************/
 
28
string StelModule::getModuleVersion() const
 
29
{
 
30
        return PACKAGE_VERSION;
 
31
}
 
32
 
 
33
/*************************************************************************
 
34
 Init itself from a .ini file
 
35
*************************************************************************/
 
36
void StelModule::loadProperties(const string& iniFile)
 
37
{
 
38
        InitParser conf;
 
39
        conf.load(iniFile);
 
40
        
 
41
        std::cout << "Loading properties for module " << conf.get_str(getModuleID()+":version") << std::endl;
 
42
        
 
43
        std::map<string, propertyAccessor>::const_iterator iter;
 
44
        for(iter=properties.begin();iter!=properties.end();++iter)
 
45
        {
 
46
                conf.set_str(getModuleID()+":"+(*iter).first, getPropertyStr((*iter).first));
 
47
        }
 
48
        conf.save(iniFile);     
 
49
}
 
50
 
 
51
//! Save itself into a .ini file for subsequent reload (don't modify the other entries)
 
52
void StelModule::saveProperties(const string& iniFile) const
 
53
{
 
54
        InitParser conf;
 
55
        conf.load(iniFile);
 
56
        
 
57
        conf.set_str(getModuleID()+":version", getModuleVersion());
 
58
        
 
59
        std::map<string, propertyAccessor>::const_iterator iter;
 
60
        for(iter=properties.begin();iter!=properties.end();++iter)
 
61
        {
 
62
                conf.set_str(getModuleID()+":"+(*iter).first, getPropertyStr((*iter).first));
 
63
        }
 
64
        conf.save(iniFile);
 
65
}
 
66
 
 
67
//! Get a property value as string: try to cast the value as a bool, int, double, string, Vec3d
 
68
string StelModule::getPropertyStr(const string& key) const
 
69
{
 
70
        std::ostringstream oss;
 
71
        propertyAccessor p = getAccessor(key);
 
72
        try
 
73
        {
 
74
                if (p.get<bool>())
 
75
                {
 
76
                        oss << "true";
 
77
                }
 
78
                else
 
79
                {
 
80
                        oss << "false";
 
81
                }
 
82
        }
 
83
        catch (boost::bad_any_cast)
 
84
        {
 
85
                try
 
86
                {
 
87
                        oss << p.get<int>();
 
88
                }
 
89
                catch (boost::bad_any_cast)
 
90
                {
 
91
                        try
 
92
                        {
 
93
                                oss << p.get<double>();
 
94
                        }
 
95
                        catch (boost::bad_any_cast)
 
96
                        {
 
97
                                try
 
98
                                {
 
99
                                        oss << p.get<string>();
 
100
                                }
 
101
                                catch (boost::bad_any_cast)
 
102
                                {
 
103
                                        try
 
104
                                        {
 
105
                                                oss << p.get<Vec3d>();
 
106
                                        }
 
107
                                        catch (boost::bad_any_cast)
 
108
                                        {
 
109
                                                std::cerr << "Error while getting property \"" << key << "\", can't find matching type." << std::endl;
 
110
                                                assert(0);
 
111
                                        }
 
112
                                }
 
113
                        }
 
114
                }
 
115
        }
 
116
        return oss.str();
 
117
}
 
118
 
 
119
//! Set a property value from a string: try to cast the value as a bool, int, double, string, Vec3d
 
120
void StelModule::setPropertyStr(const string& key, const string& value)
 
121
{
 
122
        propertyAccessor p = getAccessor(key);
 
123
        try
 
124
        {
 
125
                p.set<bool>(value=="true");
 
126
                if (value!="true" && value!="false")
 
127
                {
 
128
                        std::cerr << "Error while setting property \"" << key << "\" to value \""<< value <<"\", boolean value can be \"true\" or \"false\"." << std::endl;
 
129
                }
 
130
        }
 
131
        catch (boost::bad_any_cast)
 
132
        {
 
133
                try
 
134
                {
 
135
                        p.set<string>(value);
 
136
                }
 
137
                catch (boost::bad_any_cast)
 
138
                {
 
139
                        try
 
140
                        {
 
141
                                istringstream iss(value);
 
142
                                int i;
 
143
                                iss >> i;
 
144
                                p.set<int>(i);
 
145
                        }
 
146
                        catch (boost::bad_any_cast)
 
147
                        {
 
148
                                try
 
149
                                {
 
150
                                        istringstream iss(value);
 
151
                                        double d;
 
152
                                        iss >> d;
 
153
                                        p.set<double>(d);
 
154
                                }
 
155
                                catch (boost::bad_any_cast)
 
156
                                {
 
157
                                        std::cerr << "Error while setting property \"" << key << "\" to value \""<< value <<"\", can't find matching type." << std::endl;
 
158
                                        assert(0);
 
159
                                }
 
160
                        }
 
161
                }
 
162
        }
 
163
}