-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_handler_definitions.cpp
More file actions
77 lines (63 loc) · 2.1 KB
/
csv_handler_definitions.cpp
File metadata and controls
77 lines (63 loc) · 2.1 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
//
// Created by ChrisD on 13/10/23.
//
#include "csv_handler.h"
// Read more
void CSV_Handler::ReadSettings()
{
std::ifstream settingsCSV;
settingsCSV.open("../settings/settings.csv");
if(!settingsCSV.is_open())
{
std::cout << std::strerror(errno) << std::endl;
exit(errno);
}
std::string column;
while(getline(settingsCSV ,column, '\n'))
{
csvLines.push_back(column);
}
settingsCSV.close();
}
// Start adding data to the CSV file
void CSV_Handler::WriteSavedSearchOptions(std::string &keyword, std::string &url)
{
std::ofstream csvfile;
csvfile.open("../settings/settings.csv", std::ios::app);
// std::setlocale(LC_ALL, "el_GR.UTF-8");
// To lower case
// Source: https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case
// std::transform(keyword.begin(), keyword.end(), keyword.begin(),
// [](char c){return std::tolower(c, std::locale("el_GR"));});
// Iterating through each character ends up with ? symbols instead of
// printing the correct character. This is why the iterator in std::transform
// doesn't work
std::string keywordLowercase = boost::locale::to_lower(keyword);
if (!csvfile.is_open())
{
std::cout << "Failed to open settings file." << std::endl;
std::cout << std::strerror(errno) << std::endl;
exit(errno);
}
std::string csvData = url;
std::string separator = ",";
csvData.append(separator);
csvData.append(keywordLowercase);
csvfile << csvData << std::endl;
csvfile.close();
}
// Clearing a file quite easy, just open it without setting
// an input or output mode and close it
// Source: https://stackoverflow.com/questions/25201131/writing-csv-files-from-c
void CSV_Handler::ClearPreviousOptions()
{
std::ofstream csvFileToClear;
csvFileToClear.open("../settings/settings.csv");
if (!csvFileToClear.is_open())
{
std::cout << "Failed to save settings." << std::endl;
std::cout << std::strerror(errno) << std::endl;
exit(errno);
}
csvFileToClear.close();
}