-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitcoinExchange.cpp
More file actions
280 lines (260 loc) · 8.49 KB
/
BitcoinExchange.cpp
File metadata and controls
280 lines (260 loc) · 8.49 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BitcoinExchange.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dhuss <dhuss@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/09 15:51:38 by dhuss #+# #+# */
/* Updated: 2025/05/26 13:15:03 by dhuss ### ########.fr */
/* */
/* ************************************************************************** */
#include "BitcoinExchange.hpp"
//--------Constructors----------//
/*------------------------------*/
/* Default Constructor */
/*------------------------------*/
BitcoinExchange::BitcoinExchange() : _filename("default"), _date("1970-01-01")
{
std::cout << std::fixed << std::setprecision(2);
}
/*------------------------------*/
/* Constructor */
/*------------------------------*/
BitcoinExchange::BitcoinExchange(std::string filename) : _filename(filename), _date(todaysDate())
{
std::cout << std::fixed << std::setprecision(2);
}
/*------------------------------*/
/* Copy Constructor */
/*------------------------------*/
BitcoinExchange::BitcoinExchange(const BitcoinExchange& src) : _filename(src._filename), _date(src._date), _dataBase(src._dataBase)
{
}
/*----------------------------------*/
/* Overloaded Assignment Operator */
/*----------------------------------*/
BitcoinExchange BitcoinExchange::operator=(const BitcoinExchange& other)
{
if (this != &other)
{
_filename = other._filename;
_date = other._date;
_dataBase = other._dataBase;
}
return (*this);
}
/*------------------------------*/
/* Destructor */
/*------------------------------*/
BitcoinExchange::~BitcoinExchange()
{
}
//--------helper----------//
/*------------------------------------------*/
/* checks whether the year is a leap year */
/*------------------------------------------*/
bool isLeapYear(int year)
{
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
return (true);
return (false);
}
//--------public methods----------//
/*--------------------------------------*/
/* Returns today's date as a string */
/*--------------------------------------*/
std::string BitcoinExchange::todaysDate()
{
std::time_t now;
char time_str[18];
now = std::time(nullptr);
std::strftime(time_str, sizeof(time_str), "%Y-%m-%d", std::localtime(&now));
return (time_str);
}
/*----------------------------------------------*/
/* Checks if the time span is valid */
/* - from epoch until current day */
/*----------------------------------------------*/
bool BitcoinExchange::dateSpan(int year, int month, int day)
{
std::smatch matches;
std::regex pattern("^(\\d{4})-(\\d{2})-(\\d{2})$");
if (!std::regex_match(_date, matches, pattern))
return (false);
if (year >= 1970 && year < std::stoi(matches[1].str()))
return (true);
else if (year == std::stoi(matches[1].str()))
{
if (month <= std::stoi(matches[2].str()))
{
if (day <= std::stoi(matches[3].str()))
return (true);
}
}
return (false);
}
/*------------------------------------------------------*/
/* validates the Date format && whether date exists */
/*------------------------------------------------------*/
bool BitcoinExchange::validateDate(std::string str)
{
std::smatch matches;
std::regex pattern("^(\\d{4})-(\\d{2})-(\\d{2})$");
if (!std::regex_match(str, matches, pattern))
return (false);
int year = std::stoi(matches[1].str());
int month = std::stoi(matches[2].str());
int day = std::stoi(matches[3].str());
const int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31};
if (month < 1 || month > 12)
return (false);
int maxDays = daysInMonth[month];
if (month == 2 && isLeapYear(year))
maxDays = 29;
if (day < 1 || day > maxDays)
return (false);
if (!dateSpan(year, month, day))
return (false);
return (true);
}
/*------------------------------------------------------*/
/* validates the Value */
/* - catches overflows */
/* - no negative values */
/* - if input value not above 1000 */
/* - if data.csv value not above 1e20 */
/* -> prevents the final result from being inf */
/*------------------------------------------------------*/
bool BitcoinExchange::validateValue(std::string str, bool exiting)
{
float num = -1;
try
{
num = std::stof(str);
}
catch(const std::exception& e)
{
err(E_INPUT, str, exiting);
return (false);
}
if (num < 0)
{
err(E_VALUE, "not a positive number.", exiting);
return (false);
}
if (!exiting && num > 1000)
{
err(E_VALUE, "too large a number.", exiting);
return (false);
}
if (exiting && num > 1e20)
err(E_VALUE, "Exchange rate too high.", exiting);
return (true);
}
/*----------------------------------------------------------------------------------*/
/* finds matching dates and multiples exchange rate with value */
/* - returns the first iterator where the key is greater than the searched key */
/* - subtracts one from iterator to get matching date or closest lower date */
/* - prints the multiplied value at date in question */
/*----------------------------------------------------------------------------------*/
void BitcoinExchange::matchDates(std::string date, std::string value)
{
std::map<std::string, float>::iterator it = _dataBase.upper_bound(date);
if (it != _dataBase.begin())
it--;
std::cout << date << " => " << std::stof(value) << " = " << std::stof(value) * it->second << std::endl;
}
/*----------------------------------------------------------------------*/
/* parses input file */
/* - opens input file */
/* - extracts one line at a time */
/* - matches line to regex pattern and splits it into dates and values */
/* - split dates and values are valdiated */
/*----------------------------------------------------------------------*/
void BitcoinExchange::extractInput()
{
std::string line;
std::ifstream file(_filename);
std::smatch split;
std::regex pattern("^(\\d{4}-\\d{2}-\\d{2})( \\| )([+-]?\\d+[.]?\\d*)$");
if (!file)
err(E_FILE, _filename, true);
getline(file, line);
if (line.compare("date | value") != 0)
file.close(), err(E_WRONGHEADER, "for input file", true);
while (getline(file, line))
{
if (std::regex_match(line, split, pattern))
{
if (validateDate(split[1]))
{
if(validateValue(split[3], false))
matchDates(split[1], split[3]);
}
else
err(E_INPUT, line, false);
}
else if (line.empty())
err(E_EMPTY, line, false);
else
err(E_INPUT, line, false);
}
file.close();
}
/*--------------------------------------------------------------*/
/* - opens data.csv */
/* - extracts line from file */
/* - splits data and exchange rate with regex */
/* - populate multimap container with date and exchange rate */
/*--------------------------------------------------------------*/
void BitcoinExchange::mapDataBase()
{
std::string line;
std::ifstream file("data.csv");
std::regex pattern("^(\\d{4}-\\d{2}-\\d{2}),([+-]?\\d+\\.?\\d*)$");
std::smatch split;
if (!file)
err(E_FILE, "data.csv!", true);
getline(file, line);
if (line.compare("date,exchange_rate") != 0)
file.close(), err(E_WRONGHEADER, "for data.csv.", true);
while (getline(file, line))
{
if (std::regex_match(line, split, pattern))
{
if (validateDate(split[1]) && validateValue(split[2], true))
_dataBase[split[1]] = std::stof(split[2]);
}
else if (line.empty())
file.close(), err(E_EMPTY, line, true);
else
file.close(), err(E_INPUT, line, true);
}
file.close();
}
/*--------------------------*/
/* handles errors */
/*--------------------------*/
void BitcoinExchange::err(t_errors err, std::string msg, bool exiting)
{
std::cerr << "\033[31mError: ";
if (err == E_ARGS)
std::cerr << "invalid number of arguments.";
else if (err == E_FILE)
std::cerr << "could not open " << msg;
else if (err == E_INPUT)
std::cerr << "bad input => " << msg;
else if (err == E_VALUE)
std::cerr << msg;
else if (err == E_EMPTY)
std::cerr << "line is empty!";
else if (err == E_WRONGHEADER)
std::cerr << "wrong header " << msg;
if (exiting)
{
std::cerr << " Exiting...\033[0m" << std::endl;
exit(EXIT_FAILURE);
}
std::cerr << "\033[0m" << std::endl;
}