-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
115 lines (106 loc) · 3.2 KB
/
main.cpp
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
#include "SortAlgorithms/Bubble.h"
#include "SortAlgorithms/Merge.h"
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
using namespace std;
/**
* @brief Method made for the validation of input file
* @param path string with the input file path
* @return true if the file contents satisfy the requeriments
* @return false otherwise
*/
bool CheckFile(const string &path)
{
bool result{true};
int counter{};
string currentLine{};
ifstream inputFile(path);
result = inputFile.is_open();
if (!result)
{
cout << "Can't open the input file" << endl;
return result;
}
while (getline(inputFile, currentLine) && result)
{
counter++;
if (currentLine.size() > 100 || counter > 10000)
{
cout << "The input file is not in a valid format" << endl;
result = false;
}
}
return result;
}
int main(int argc, char **argv)
{
if (argc == 4)
{
string inputFile(argv[1]);
//! If it isn't a valid file just exit
if (!CheckFile(inputFile))
{
return -1;
}
string outputFile(argv[2]);
string sortAlgorithm(argv[3]);
unique_ptr<SortBase> sorter{};
//! Just can't work if one of the valid algorithms was received in param 3
if (sortAlgorithm.compare("bubble") == 0)
{
sorter = make_unique<Bubble>(outputFile);
}
else if (sortAlgorithm.compare("merge") == 0)
{
sorter = make_unique<Merge>(outputFile);
}
if (sorter == nullptr)
{
cout << "Invalid algorithm selected" << endl;
return -1;
}
ifstream inputFileStream(inputFile);
if (!inputFileStream.is_open())
{
cout << "Can't open the input file" << endl;
return -1;
}
string currentLine;
vector<thread> threadHolder;
bool couldRead{true};
while (couldRead)
{
for (int i = 0; (i < 4) && couldRead; i++)
{
couldRead = !(inputFileStream.eof() || inputFileStream.fail());
if (couldRead)
{
getline(inputFileStream, currentLine);
threadHolder.push_back(
std::thread(&SortBase::SortLine,
sorter.get(),
currentLine));
}
}
for (size_t i = 0; i < threadHolder.size(); i++)
{
threadHolder.at(i).join();
}
threadHolder.clear();
};
}
else
{
cout << "Invalid quantity of input parameters" << endl
<< "The usage of this software needs three params as follows:" << endl
<< " 1. Name of input file" << endl
<< " 2. Name of output file" << endl
<< " 3. Name of sorting algorithm, you can use one of this: " << endl
<< " 3.1. bubble" << endl
<< " 3.2. merge" << endl
<< "Please try running again with the correct amount of input parameters...";
}
return 0;
}