4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Profile/ProfileMap.hpp"
25 #include "IO/KeyValueFileWriter.hpp"
26 #include "Util/StringUtil.hpp"
27 #include "Util/NumberParser.hpp"
28 #include "Util/Macros.hpp"
38 typedef std::map
<std::string
, std::string
> map_t
;
51 Profile::SetModified(bool _modified
)
57 Profile::Get(const char *key
, const char *default_value
)
59 map_t::const_iterator it
= map
.find(key
);
63 return it
->second
.c_str();
67 Profile::Get(const char *key
, TCHAR
*value
, size_t max_size
)
69 map_t::const_iterator it
= map
.find(key
);
70 if (it
== map
.end()) {
75 const char *src
= it
->second
.c_str();
78 int result
= MultiByteToWideChar(CP_UTF8
, 0, src
, -1,
82 if (!ValidateUTF8(src
))
85 CopyString(value
, src
, max_size
);
91 Profile::Get(const char *key
, int &value
)
93 // Try to read the profile map
94 const char *str
= Get(key
);
98 // Parse the string for a number
100 int tmp
= ParseInt(str
, &endptr
, 0);
104 // Save parsed value to output parameter value and return success
110 Profile::Get(const char *key
, short &value
)
112 // Try to read the profile map
113 const char *str
= Get(key
);
117 // Parse the string for a number
119 short tmp
= ParseInt(str
, &endptr
, 0);
123 // Save parsed value to output parameter value and return success
129 Profile::Get(const char *key
, bool &value
)
131 // Try to read the profile map
132 const char *str
= Get(key
);
136 // Save value to output parameter value and return success
137 value
= (str
[0] != '0');
142 Profile::Get(const char *key
, unsigned &value
)
144 // Try to read the profile map
145 const char *str
= Get(key
);
149 // Parse the string for a unsigned number
151 unsigned tmp
= ParseUnsigned(str
, &endptr
, 0);
155 // Save parsed value to output parameter value and return success
161 Profile::Get(const char *key
, uint16_t &value
)
164 if (!Get(key
, value32
) || value32
>= 0x10000)
167 value
= (uint16_t)value32
;
172 Profile::Get(const char *key
, uint8_t &value
)
175 if (!Get(key
, value32
) || value32
>= 0x100)
178 value
= (uint8_t)value32
;
183 Profile::Get(const char *key
, fixed
&value
)
185 // Try to read the profile map
186 const char *str
= Get(key
);
190 // Parse the string for a floating point number
192 double tmp
= ParseDouble(str
, &endptr
);
196 // Save parsed value to output parameter value and return success
202 Profile::Set(const char *key
, const char *value
)
204 auto i
= map
.insert(std::make_pair(key
, value
));
208 if (i
.first
->second
.compare(value
) == 0)
209 /* not modified, don't set the "modified" flag */
212 i
.first
->second
.assign(value
);
221 Profile::Set(const char *key
, const TCHAR
*value
)
223 char buffer
[MAX_PATH
];
224 int length
= WideCharToMultiByte(CP_UTF8
, 0, value
, -1,
225 buffer
, ARRAY_SIZE(buffer
),
236 Profile::Set(const char *key
, int value
)
239 sprintf(tmp
, "%d", value
);
240 return Set(key
, tmp
);
244 Profile::Set(const char *key
, long value
)
247 sprintf(tmp
, "%ld", value
);
248 return Set(key
, tmp
);
252 Profile::Set(const char *key
, unsigned value
)
255 sprintf(tmp
, "%u", value
);
256 return Set(key
, tmp
);
260 Profile::Set(const char *key
, fixed value
)
263 sprintf(tmp
, "%f", (double)value
);
264 return Set(key
, tmp
);
268 Profile::Exists(const char *key
)
270 return map
.find(key
) != map
.end();
274 Profile::Export(KeyValueFileWriter
&writer
)
276 // Iterate through the profile maps
277 for (auto it_str
= map
.begin(); it_str
!= map
.end(); it_str
++)
278 /* ignore the "Vega*" values; the Vega driver abuses the profile
279 to pass messages between the driver and the user interface */
280 if (strncmp(it_str
->first
.c_str(), "Vega", 4) != 0)
281 writer
.Write(it_str
->first
.c_str(), it_str
->second
.c_str());