blob: 080fe1d281f7c61733ea450a9f7d608828d2d115 [file] [log] [blame]
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP 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 TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <linux/input.h>
20#include <pthread.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
26#include <sys/stat.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <unistd.h>
31#include <stdlib.h>
32
33#include <string>
34#include <utility>
35#include <map>
36#include <fstream>
37#include <sstream>
38
39#include "infomanager.hpp"
40#include "twcommon.h"
41#include "partitions.hpp"
42
43using namespace std;
44
45InfoManager::InfoManager(const string filename) {
46 File = filename;
47}
48
49InfoManager::~InfoManager(void) {
50 mValues.clear();
51}
52
53int InfoManager::LoadValues(void) {
54 string str;
55
56 // Read in the file, if possible
57 FILE* in = fopen(File.c_str(), "rb");
58 if (!in) {
59 LOGINFO("InfoManager file '%s' not found.\n", File.c_str());
60 return -1;
61 } else {
62 LOGINFO("InfoManager loading from '%s'.\n", File.c_str());
63 }
64
65 while (!feof(in)) {
66 string Name;
67 string Value;
68 unsigned short length;
69 char array[512];
70
71 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
72 if (length >= 512) goto error;
73 if (fread(array, 1, length, in) != length) goto error;
74 Name = array;
75
76 if (fread(&length, 1, sizeof(unsigned short), in) != sizeof(unsigned short)) goto error;
77 if (length >= 512) goto error;
78 if (fread(array, 1, length, in) != length) goto error;
79 Value = array;
80
81 map<string, string>::iterator pos;
82
83 pos = mValues.find(Name);
84 if (pos != mValues.end()) {
85 pos->second = Value;
86 } else {
87 mValues.insert(make_pair(Name, Value));
88 }
89 }
90error:
91 fclose(in);
92 return 0;
93}
94
95int InfoManager::SaveValues(void) {
96 if (File.empty())
97 return -1;
98
99 PartitionManager.Mount_By_Path(File, true);
100LOGINFO("InfoManager saving '%s'\n", File.c_str());
101 FILE* out = fopen(File.c_str(), "wb");
102 if (!out)
103 return -1;
104
105 map<string, string>::iterator iter;
106 for (iter = mValues.begin(); iter != mValues.end(); ++iter) {
107 unsigned short length = (unsigned short) iter->first.length() + 1;
108 fwrite(&length, 1, sizeof(unsigned short), out);
109 fwrite(iter->first.c_str(), 1, length, out);
110 length = (unsigned short) iter->second.length() + 1;
111 fwrite(&length, 1, sizeof(unsigned short), out);
112 fwrite(iter->second.c_str(), 1, length, out);
113 }
114 fclose(out);
115 return 0;
116}
117
118int InfoManager::GetValue(const string varName, string& value) {
119 string localStr = varName;
120
121 map<string, string>::iterator pos;
122 pos = mValues.find(localStr);
123 if (pos == mValues.end())
124 return -1;
125
126 value = pos->second;
127 return 0;
128}
129
130int InfoManager::GetValue(const string varName, int& value) {
131 string data;
132
133 if (GetValue(varName,data) != 0)
134 return -1;
135
136 value = atoi(data.c_str());
137 return 0;
138}
139
140int InfoManager::GetValue(const string varName, float& value) {
141 string data;
142
143 if (GetValue(varName,data) != 0)
144 return -1;
145
146 value = atof(data.c_str());
147 return 0;
148}
149
150unsigned long long InfoManager::GetValue(const string varName, unsigned long long& value) {
151 string data;
152
153 if (GetValue(varName,data) != 0)
154 return -1;
155
156 value = strtoull(data.c_str(), NULL, 10);
157 return 0;
158}
159
160// This function will return an empty string if the value doesn't exist
161string InfoManager::GetStrValue(const string varName) {
162 string retVal;
163
164 GetValue(varName, retVal);
165 return retVal;
166}
167
168// This function will return 0 if the value doesn't exist
169int InfoManager::GetIntValue(const string varName) {
170 string retVal;
171 GetValue(varName, retVal);
172 return atoi(retVal.c_str());
173}
174
175int InfoManager::SetValue(const string varName, string value) {
176 // Don't allow empty values or numerical starting values
177 if (varName.empty() || (varName[0] >= '0' && varName[0] <= '9'))
178 return -1;
179
180 map<string, string>::iterator pos;
181 pos = mValues.find(varName);
182 if (pos == mValues.end())
183 mValues.insert(make_pair(varName, value));
184 else
185 pos->second = value;
186
187 return 0;
188}
189
190int InfoManager::SetValue(const string varName, int value) {
191 ostringstream valStr;
192 valStr << value;
193 return SetValue(varName, valStr.str());
194}
195
196int InfoManager::SetValue(const string varName, float value) {
197 ostringstream valStr;
198 valStr << value;
199 return SetValue(varName, valStr.str());
200}
201
202int InfoManager::SetValue(const string varName, unsigned long long value) {
203 ostringstream valStr;
204 valStr << value;
205 return SetValue(varName, valStr.str());
206}