HatchKeeper  0.90
The Free Open-Source Egg Incubation Software
HK_KeyValue.cpp
Go to the documentation of this file.
1 /*******************************************************************/
10 #include "../headers/KeyValue.h"
11 
14 
16 int HK_KeyValue::CheckSelection(int Selection)
17 {
18  if(Selection < 0 || Selection > GetCount())
19  return GetCount();
20  else
21  return Selection;
22 }
23 
24 
27 {
28  //1 Less Than size() Because Of Error At End
29  return HK_SettingKey.size() - 1;
30 }
31 
33 void HK_KeyValue::Add(string Key, string Value)
34 {
35  HK_SettingKey[GetCount()] = Key;
36  HK_SettingValue[GetCount()] = Value;
37  HK_Update[GetCount()] = false;
38  SetError();
39 }
40 
43 {
44  for(int A = 0; A < KeyValue.GetCount(); A++)
45  Add(KeyValue.GetKey(A), KeyValue.GetValue(A));
46 }
47 
49 int HK_KeyValue::GetInt(int Selection)
50 {
51  return stoi(HK_SettingValue[CheckSelection(Selection)]);
52 }
53 
55 int HK_KeyValue::GetInt(string Key)
56 {
57  return stoi(HK_SettingValue[FindSelection(Key)]);
58 }
59 
61 string HK_KeyValue::GetKey(int Selection)
62 {
63  return HK_SettingKey[CheckSelection(Selection)];
64 }
65 
67 string HK_KeyValue::GetValue(int Selection)
68 {
69  return HK_SettingValue[CheckSelection(Selection)];
70 }
71 
73 string HK_KeyValue::GetValue(string Key)
74 {
75  return HK_SettingValue[FindSelection(Key)];
76 }
77 
79 bool HK_KeyValue::IsDirty(int Selection){return HK_Update[CheckSelection(Selection)];}
81 bool HK_KeyValue::IsDirty(string Key){return HK_Update[FindSelection(Key)];}
82 
85 {
86  HK_KeyValue Results;
87  string Text;
88 
89  transform(Value.begin(),Value.end(),Value.begin(), ::tolower);
90 
91  for(int A = 0; A < GetCount(); A++) {
92  Text = HK_SettingValue[A];
93  transform(Text.begin(),Text.end(),Text.begin(), ::tolower);
94 
95  if(Text.find(Value) != string::npos)
96  Results.Add("Setting " + HK_SettingKey[A] + "'s Value", HK_SettingValue[A]);
97  }
98 
99  return Results;
100 }
101 
103 void HK_KeyValue::SetClean(int Selection){HK_Update[CheckSelection(Selection)] = false;}
105 void HK_KeyValue::SetClean(string Key){HK_Update[FindSelection(Key)] = false;}
106 
115 void HK_KeyValue::SetDirty(int Selection){HK_Update[CheckSelection(Selection)] = true;}
117 void HK_KeyValue::SetDirty(string Key){HK_Update[FindSelection(Key)] = true;}
118 
121 {
122  HK_SettingKey.push_back("Error!");
123  HK_SettingValue.push_back("Error!");
124  HK_Update.push_back(false);
125 }
126 
128 void HK_KeyValue::SetValue(int Selection,string Value){
129  HK_SettingValue[CheckSelection(Selection)] = Value;
130  HK_Update[CheckSelection(Selection)] = true;
131 }
132 
134 void HK_KeyValue::SetValue(string Key,string Value){
135  HK_SettingValue[FindSelection(Key)] = Value;
136  HK_Update[FindSelection(Key)] = true;
137 }
138 
140 vector<string> HK_KeyValue::GetArray(int Selection){
141  return SplitStrings(HK_SettingValue[CheckSelection(Selection)],",");
142 }
143 
145 vector<string> HK_KeyValue::GetArray(string Key){
146  return SplitStrings(HK_SettingValue[FindSelection(Key)],",");
147 }
148 
150 vector<int> HK_KeyValue::GetIntArray(int Selection){
151  vector<int> IntArray;
152  vector<string> StringArray = SplitStrings(HK_SettingValue[CheckSelection(Selection)],",");
153 
154  for(unsigned int A = 0;A < StringArray.size();A++)
155  IntArray.push_back(stoi(StringArray[A]));
156 
157  StringArray.clear();
158  StringArray.shrink_to_fit();
159 
160  return IntArray;
161 }
162 
164 vector<int> HK_KeyValue::GetIntArray(string Key){
165  vector<int> IntArray;
166  vector<string> StringArray = SplitStrings(HK_SettingValue[FindSelection(Key)],",");
167 
168  for(unsigned int A = 0;A < StringArray.size();A++)
169  IntArray.push_back(stoi(StringArray[A]));
170 
171  StringArray.clear();
172  StringArray.shrink_to_fit();
173 
174  return IntArray;
175 }
176 
179  for(int A = 0; A < GetCount(); A++){
180  if(HK_SettingKey[A] == Key)
181  return A;
182  }
183  //Not Found
184  return GetCount();
185 }
186 
193 vector<string> HK_KeyValue::SplitStrings(string In, string Split){
194  string Hold, Empty;
195  vector<string> OutArray;
196  int B = In.length();
197  for(int A = 0; A < B; A++){
198  if(In[A] == Split[0]){
199  OutArray.push_back(Hold);
200  Hold = Empty;
201  continue;
202  }
203  Hold += In[A];
204  }
205 
206  OutArray.push_back(Hold);
207  return OutArray;
208 }
209 
212  HK_SettingKey.clear();
213  HK_SettingValue.clear();
214  HK_Update.clear();
215  HK_SettingValue.shrink_to_fit();
216  HK_SettingKey.shrink_to_fit();
217  HK_Update.shrink_to_fit();
218  SetError();
219 }
220 
222 vector<string> HK_KeyValue::GetUpdate(string TableName)
223 {
224  vector<string> SQL;
225 
226  for(int A = 0; A < GetCount(); A++) {
227  if(IsDirty(A)) {
228  SQL.push_back("UPDATE " + TableName + " SET Value = '" + GetValue(A) + "' WHERE Key = '" + GetKey(A) + "';");
229  SetClean(A);
230  }
231  }
232  return SQL;
233 }
A Key and Value Type Storage.
Definition: KeyValue.h:25
vector< bool > HK_Update
Definition: KeyValue.h:29
vector< string > HK_SettingKey
Definition: KeyValue.h:27
HK_KeyValue()
Constructor.
Definition: HK_KeyValue.cpp:13
void SetDirty(int Selection)
vector< string > GetArray(int Selection)
Returns The Selected Value As An Array Of Strings.
void Clear()
Clears All Internal Data And Shrinks The Storage.
vector< string > HK_SettingValue
Definition: KeyValue.h:28
int FindSelection(string Key)
Finds Selection By Matching The Input To A Key.
string GetKey(int Selection)
Returns The Selected Key As A String.
Definition: HK_KeyValue.cpp:61
void Add(string Key, string Value)
Adds A Key And Value;.
Definition: HK_KeyValue.cpp:33
void SetError()
Pushes An Error To The The End Of Vectors For A Return Value If Nothing Is Found.
void SetValue(int Selection, string Value)
Sets Value For Selection. Sets Dirty As Well.
vector< int > GetIntArray(int Selection)
Returns The Selected Value As An Array Of Integers.
int GetInt(int Selection)
Returns The Selected Setting Value As An Integer.
Definition: HK_KeyValue.cpp:49
vector< string > SplitStrings(string In, string Split)
Converts A String Into An Array.
bool IsDirty(int Selection)
Returns True If Selected Setting Is Dirty.
Definition: HK_KeyValue.cpp:79
int GetCount()
Return The Amount Of Setting Keys Stored.
Definition: HK_KeyValue.cpp:26
void SetClean(int Selection)
Clears The Dirty Status For The Selected Setting.
vector< string > GetUpdate(string TableName)
Returns An SQL Statement Updating All Settings Marked Dirty. Marks All Clean.
int CheckSelection(int Selection)
Checks Selection And Returns An Error If Out Of Bounds.
Definition: HK_KeyValue.cpp:16
string GetValue(int Selection)
Returns The Selected Setting Value As A String.
Definition: HK_KeyValue.cpp:67
HK_KeyValue Search(string Value)
Returns A HK_KeyValue Object With Matches To Value.
Definition: HK_KeyValue.cpp:84