-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetting.cpp
More file actions
144 lines (119 loc) · 3.35 KB
/
Setting.cpp
File metadata and controls
144 lines (119 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Setting.cpp
*
* Created on: 23 3 2012
* Author: Max Foster
*/
#include <stdexcept>
#include <cstring>
using namespace std;
#include "Setting.h"
#include "Utils.h"
const string Setting::databaseFilename = "settings.dat";
const int
Setting::minKeyLength = 2, Setting::maxKeyLength = 32,
Setting::minValueLength = 0, Setting::maxValueLength = 2048;
int Setting::size()
{
return Record::size() + maxKeyLength + maxValueLength + 2;
}
Setting::Setting(const char *newKey, const char *newValue)
{
key = new char[maxKeyLength + 1];
value = new char[maxValueLength + 1];
strcpy(key, newKey);
strcpy(value, newValue);
}
Setting::Setting(const Setting &setting)
{
key = new char[maxKeyLength + 1];
value = new char[maxValueLength + 1];
(*this) = setting;
}
Setting::~Setting()
{
delete[] key;
delete[] value;
}
void Setting::operator =(const Setting &setting)
{
*((Record*)this) = (Record)setting;
strcpy(key, setting.key);
strcpy(value, setting.value);
}
void Setting::writeToFile(fstream &file) const
{
Record::writeToFile(file);
file.write(key, maxKeyLength + 1);
file.write(value, maxValueLength + 1);
}
void Setting::readFromFile(fstream &file)
{
Record::readFromFile(file);
file.read(key, maxKeyLength + 1);
file.read(value, maxValueLength + 1);
}
bool Setting::hasMatchingField(const string &fieldName, const int searchTerm) const
{
return Record::hasMatchingField(fieldName, searchTerm);
}
bool Setting::hasMatchingField(const string &fieldName, const char *searchTerm) const
{
if (fieldName == "key") return (strcmp(key, searchTerm) == 0);
if (fieldName == "value") return (strcmp(value, searchTerm) == 0);
return false;
}
bool Setting::fieldCompare(const Setting &rhs) const
{
if (strcmp(key, rhs.key) != 0) return false;
if (strcmp(value, rhs.value) != 0) return false;
return true;
}
bool Setting::completeCompare(const Setting &rhs) const
{
if (getId() != rhs.getId()) return false;
return fieldCompare(rhs);
}
const char * Setting::getKey() const
{
return key;
}
void Setting::setKey(const char *newKey)
{
string errorMessage;
if (isValidKey(newKey, errorMessage)) strcpy(key, newKey);
else throw std::runtime_error(errorMessage);
}
bool Setting::isValidKey(const char *value, string &errorMessage)
{
return validateLengthOf(value, minKeyLength, maxKeyLength, "Key", errorMessage);
}
const char * Setting::getValue() const
{
return value;
}
void Setting::setValue(const char *newValue)
{
string errorMessage;
if (isValidValue(newValue, errorMessage)) strcpy(value, newValue);
else throw std::runtime_error(errorMessage);
}
bool Setting::isValidValue(const char *value, string &errorMessage)
{
return validateLengthOf(value, minValueLength, maxValueLength, "Value", errorMessage);
}
void Setting::validate() const
{
string errorMessage;
if (!isValidKey(key, errorMessage)) throw std::runtime_error(errorMessage);
if (!isValidValue(value, errorMessage)) throw std::runtime_error(errorMessage);
}
ostream & operator <<(ostream &stream, const Setting &setting)
{
stream << "***************" << endl
<< " ID : " << setting.getId() << endl
<< " Key : " << setting.getKey() << endl
<< "Value : " << setting.getValue() << endl
<< "***************" << endl;
return stream;
}