From fe91611cb1c484429f2fdc958732337a5fc38fa8 Mon Sep 17 00:00:00 2001 From: Ethan Yonker Date: Mon, 14 Mar 2016 14:54:37 -0500 Subject: DataManager Updates The goal of this change is to make DataManager use InfoManager to reduce code duplication. Change-Id: Ia4f4c4324453a192995e0f442db0a03628c13e46 --- infomanager.cpp | 80 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 29 deletions(-) (limited to 'infomanager.cpp') diff --git a/infomanager.cpp b/infomanager.cpp index 2a4c3c892..275c70c6e 100644 --- a/infomanager.cpp +++ b/infomanager.cpp @@ -16,22 +16,7 @@ along with TWRP. If not, see . */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include -#include #include #include #include @@ -43,11 +28,34 @@ using namespace std; -InfoManager::InfoManager(const string filename) { - File = filename; +InfoManager::InfoManager() { + file_version = 0; + is_const = false; +} + +InfoManager::InfoManager(const string& filename) { + file_version = 0; + is_const = false; + SetFile(filename); } InfoManager::~InfoManager(void) { + Clear(); +} + +void InfoManager::SetFile(const string& filename) { + File = filename; +} + +void InfoManager::SetFileVersion(int version) { + file_version = version; +} + +void InfoManager::SetConst(void) { + is_const = true; +} + +void InfoManager::Clear(void) { mValues.clear(); } @@ -63,6 +71,16 @@ int InfoManager::LoadValues(void) { LOGINFO("InfoManager loading from '%s'.\n", File.c_str()); } + if (file_version) { + int read_file_version; + if (fread(&read_file_version, 1, sizeof(int), in) != sizeof(int)) + goto error; + if (read_file_version != file_version) { + LOGINFO("InfoManager file version has changed, not reading file\n"); + goto error; + } + } + while (!feof(in)) { string Name; string Value; @@ -105,6 +123,10 @@ int InfoManager::SaveValues(void) { if (!out) return -1; + if (file_version) { + fwrite(&file_version, 1, sizeof(int), out); + } + map::iterator iter; for (iter = mValues.begin(); iter != mValues.end(); ++iter) { unsigned short length = (unsigned short) iter->first.length() + 1; @@ -119,7 +141,7 @@ int InfoManager::SaveValues(void) { return 0; } -int InfoManager::GetValue(const string varName, string& value) { +int InfoManager::GetValue(const string& varName, string& value) { string localStr = varName; map::iterator pos; @@ -131,7 +153,7 @@ int InfoManager::GetValue(const string varName, string& value) { return 0; } -int InfoManager::GetValue(const string varName, int& value) { +int InfoManager::GetValue(const string& varName, int& value) { string data; if (GetValue(varName,data) != 0) @@ -141,7 +163,7 @@ int InfoManager::GetValue(const string varName, int& value) { return 0; } -int InfoManager::GetValue(const string varName, float& value) { +int InfoManager::GetValue(const string& varName, float& value) { string data; if (GetValue(varName,data) != 0) @@ -151,7 +173,7 @@ int InfoManager::GetValue(const string varName, float& value) { return 0; } -unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) { +unsigned long long InfoManager::GetValue(const string& varName, unsigned long long& value) { string data; if (GetValue(varName,data) != 0) @@ -162,7 +184,7 @@ unsigned long long InfoManager::GetValue(const string varName, unsigned long lon } // This function will return an empty string if the value doesn't exist -string InfoManager::GetStrValue(const string varName) { +string InfoManager::GetStrValue(const string& varName) { string retVal; GetValue(varName, retVal); @@ -170,14 +192,14 @@ string InfoManager::GetStrValue(const string varName) { } // This function will return 0 if the value doesn't exist -int InfoManager::GetIntValue(const string varName) { +int InfoManager::GetIntValue(const string& varName) { string retVal; GetValue(varName, retVal); return atoi(retVal.c_str()); } -int InfoManager::SetValue(const string varName, string value) { - // Don't allow empty values or numerical starting values +int InfoManager::SetValue(const string& varName, const string& value) { + // Don't allow empty names or numerical starting values if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9')) return -1; @@ -185,25 +207,25 @@ int InfoManager::SetValue(const string varName, string value) { pos = mValues.find(varName); if (pos == mValues.end()) mValues.insert(make_pair(varName, value)); - else + else if (!is_const) pos->second = value; return 0; } -int InfoManager::SetValue(const string varName, int value) { +int InfoManager::SetValue(const string& varName, const int value) { ostringstream valStr; valStr << value; return SetValue(varName, valStr.str()); } -int InfoManager::SetValue(const string varName, float value) { +int InfoManager::SetValue(const string& varName, const float value) { ostringstream valStr; valStr << value; return SetValue(varName, valStr.str()); } -int InfoManager::SetValue(const string varName, unsigned long long value) { +int InfoManager::SetValue(const string& varName, const unsigned long long& value) { ostringstream valStr; valStr << value; return SetValue(varName, valStr.str()); -- cgit v1.2.3