1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-17 22:56:16 +02:00

Fix setting default values for configuration entries.

Previously, one would check the value in &confInt["someKey"] by passing a
reference to it to evalIntConf. However, because this passing of the reference
went through std::unordered_map::operator[], it created a slot with the named
key and initialised its value with the default constructor of int, which
placed 0 there, if it didn't exist. If the value of 0 was acceptable for the
setting, then 0 as the value selected by the user was indistinguishable from
a slot that had been just created and had to be set to its default.

Now, the std::unordered_map is passed along with the key so that evalIntConf
can check whether the key exists and, if it doesn't exist, set the value to
its default.

Include utilities.h in gmenu2x.h instead of the reverse. One type definition
used by utilities.cpp is moved there (ConfIntHash) and for consistency
ConfStrHash is moved there as well.
This commit is contained in:
Nebuleon Fumika 2014-07-16 02:15:10 +00:00 committed by Maarten ter Huurne
parent 0908aa7bb7
commit 98a63e2940
4 changed files with 22 additions and 24 deletions

View File

@ -490,15 +490,15 @@ void GMenu2X::readConfig(string conffile) {
if (confStr["skin"].empty() || SurfaceCollection::getSkinPath(confStr["skin"]).empty()) if (confStr["skin"].empty() || SurfaceCollection::getSkinPath(confStr["skin"]).empty())
confStr["skin"] = "Default"; confStr["skin"] = "Default";
evalIntConf( &confInt["outputLogs"], 0, 0,1 ); evalIntConf( confInt, "outputLogs", 0, 0,1 );
#ifdef ENABLE_CPUFREQ #ifdef ENABLE_CPUFREQ
evalIntConf( &confInt["maxClock"], evalIntConf( confInt, "maxClock",
cpuFreqSafeMax, cpuFreqMin, cpuFreqMax ); cpuFreqSafeMax, cpuFreqMin, cpuFreqMax );
evalIntConf( &confInt["menuClock"], evalIntConf( confInt, "menuClock",
cpuFreqMenuDefault, cpuFreqMin, cpuFreqSafeMax ); cpuFreqMenuDefault, cpuFreqMin, cpuFreqSafeMax );
#endif #endif
evalIntConf( &confInt["backlightTimeout"], 15, 0,120 ); evalIntConf( confInt, "backlightTimeout", 15, 0,120 );
evalIntConf( &confInt["videoBpp"], 32, 16, 32 ); evalIntConf( confInt, "videoBpp", 32, 16, 32 );
if (confStr["tvoutEncoding"] != "PAL") confStr["tvoutEncoding"] = "NTSC"; if (confStr["tvoutEncoding"] != "PAL") confStr["tvoutEncoding"] = "NTSC";
resX = constrain( confInt["resolutionX"], 320,1920 ); resX = constrain( confInt["resolutionX"], 320,1920 );
@ -846,10 +846,10 @@ void GMenu2X::setSkin(const string &skin, bool setWallpaper) {
} }
} }
evalIntConf(&skinConfInt["topBarHeight"], 50, 32, 120); evalIntConf(skinConfInt, "topBarHeight", 50, 32, 120);
evalIntConf(&skinConfInt["bottomBarHeight"], 20, 20, 120); evalIntConf(skinConfInt, "bottomBarHeight", 20, 20, 120);
evalIntConf(&skinConfInt["linkHeight"], 50, 32, 120); evalIntConf(skinConfInt, "linkHeight", 50, 32, 120);
evalIntConf(&skinConfInt["linkWidth"], 80, 32, 120); evalIntConf(skinConfInt, "linkWidth", 80, 32, 120);
if (menu != NULL) menu->skinUpdated(); if (menu != NULL) menu->skinUpdated();

View File

@ -27,11 +27,11 @@
#include "touchscreen.h" #include "touchscreen.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "surface.h" #include "surface.h"
#include "utilities.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
class Button; class Button;
@ -68,9 +68,6 @@ enum color {
NUM_COLORS, NUM_COLORS,
}; };
typedef std::unordered_map<std::string, std::string, std::hash<std::string> > ConfStrHash;
typedef std::unordered_map<std::string, int, std::hash<std::string> > ConfIntHash;
class GMenu2X { class GMenu2X {
private: private:
Touchscreen ts; Touchscreen ts;

View File

@ -93,15 +93,13 @@ int constrain(int x, int imin, int imax) {
} }
//Configuration parsing utilities //Configuration parsing utilities
int evalIntConf (int val, int def, int imin, int imax) { int evalIntConf (ConfIntHash& hash, const std::string &key, int def, int imin, int imax) {
if (val==0 && (val<imin || val>imax)) auto it = hash.find(key);
return def; if (it == hash.end()) {
val = constrain(val, imin, imax); return hash[key] = def;
return val; } else {
return it->second = constrain(it->second, imin, imax);
} }
int evalIntConf (int *val, int def, int imin, int imax) {
*val = evalIntConf(*val, def, imin, imax);
return *val;
} }
bool split (vector<string> &vec, const string &str, const string &delim, bool destructive) { bool split (vector<string> &vec, const string &str, const string &delim, bool destructive) {

View File

@ -23,9 +23,13 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include "inputmanager.h" #include "inputmanager.h"
typedef std::unordered_map<std::string, std::string, std::hash<std::string> > ConfStrHash;
typedef std::unordered_map<std::string, int, std::hash<std::string> > ConfIntHash;
class case_less { class case_less {
public: public:
bool operator()(const std::string &left, const std::string &right) const; bool operator()(const std::string &left, const std::string &right) const;
@ -40,8 +44,7 @@ bool rmtree(std::string path);
int constrain(int x, int imin, int imax); int constrain(int x, int imin, int imax);
int evalIntConf(int val, int def, int imin, int imax); int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);
int evalIntConf(int *val, int def, int imin, int imax);
bool split(std::vector<std::string> &vec, const std::string &str, bool split(std::vector<std::string> &vec, const std::string &str,
const std::string &delim, bool destructive=true); const std::string &delim, bool destructive=true);