-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileServiceUnfin.cpp
More file actions
271 lines (224 loc) · 9.48 KB
/
FileServiceUnfin.cpp
File metadata and controls
271 lines (224 loc) · 9.48 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
#include "FileService.h"
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <cctype>
#include <utility> // For std::pair
#include <iomanip>
using namespace std;
// Constructor for the FileService class.
FileService::FileService() {}
// Helper function to remove all whitespace characters from a string.
// This function iterates over each character and excludes whitespace characters.
string FileService::removeWhitespace(const std::string& str) {
string result;
for (char c : str) {
if (!isspace(c)) { // Check if the character is not a whitespace (space, tab, newline, etc.)
result.push_back(c);
}
}
return result;
}
// Function to check if a line is empty after removing whitespace.
// It calls removeWhitespace to remove whitespace and checks if the result is empty.
bool FileService::isLineEmpty(const std::string& line) {
return removeWhitespace(line).empty();
}
// Function that compares the lines from two files and displays the differences.
void FileService::compareFilesTwo(const vector<pair<int, string>>& fileOneContents, const vector<pair<int, string>>& fileTwoContents) {
int fileOneSize = fileOneContents.size(); // Get the number of lines in the first file.
int fileTwoSize = fileTwoContents.size(); // Get the number of lines in the second file.
// Compare lines in fileOneContents with fileTwoContents
for (int i = 0; i < fileOneSize; i++) {
string fileOneLine = fileOneContents[i].second; // Get the content of the line.
int lineNumber = fileOneContents[i].first; // Get the line number.
bool foundMatch = false; // Boolean flag to check if a match is found.
// Skip empty lines after removing whitespace.
if (isLineEmpty(fileOneLine)) {
continue;
}
// Compare with each line of fileTwoContents
for (int j = 0; j < fileTwoSize; j++) {
string fileTwoLine = fileTwoContents[j].second;
// Skip empty lines after removing whitespace.
if (isLineEmpty(fileTwoLine)) {
continue;
}
// Remove whitespace from both lines for comparison.
string fileOneLineStripped = removeWhitespace(fileOneLine);
string fileTwoLineStripped = removeWhitespace(fileTwoLine);
// If lines match after stripping whitespace.
if (fileOneLineStripped == fileTwoLineStripped) {
foundMatch = true;
break; // No need to check further lines in fileTwo, since we found a match.
}
}
// If no match is found, print the difference (File 1 line with no corresponding line in File 2).
if (!foundMatch) {
if(fileOneLine!= ""){
cout << fileOneLine << " : " << "File 2 Line " << lineNumber << " - " << fileTwoContents[i].second << endl;
}else{
cout << fileOneLine << " : " << "File 2 Line " << lineNumber << " - null" << endl;
}
}
}
// Now compare lines in fileTwoContents with fileOneContents.
for (int i = 0; i < fileTwoSize; i++) {
string fileTwoLine = fileTwoContents[i].second;
int lineNumber = fileTwoContents[i].first;
bool foundMatch = false;
// Skip empty lines after removing whitespace.
if (isLineEmpty(fileTwoLine)) {
continue;
}
// Compare with each line of fileOneContents.
for (int j = 0; j < fileOneSize; j++) {
string fileOneLine = fileOneContents[j].second;
// Skip empty lines after removing whitespace.
if (isLineEmpty(fileOneLine)) {
continue;
}
// Remove whitespace from both lines for comparison.
string fileOneLineStripped = removeWhitespace(fileOneLine);
string fileTwoLineStripped = removeWhitespace(fileTwoLine);
// If lines match after stripping whitespace.
if (fileOneLineStripped == fileTwoLineStripped) {
foundMatch = true;
break; // No need to check further lines in fileOne, since we found a match.
}
}
// If no match is found, print the difference (File 2 line with no corresponding line in File 1).
if (!foundMatch) {
if(fileTwoLine!= ""){
cout << fileTwoLine << " : " << "File 1 Line " << lineNumber << " - " << fileOneContents[i].second << endl;
}else{
cout << fileTwoLine << " : " << "File 1 Line " << lineNumber << " - null" << endl;
}
}
}
cout << endl;
// Call fancyDiff to display a more detailed diff of the files.
fancyDiff(6, 25, 25, fileOneContents, fileTwoContents);
} // END OF DIFF FUNCTION
// Function to display a more structured "fancy" diff, with indices and content from both files.
void FileService::fancyDiff(int pIndex, int pFileOneLineAmount, int pFileTwoLineAmount, const vector<pair<int, string>>& fileOneContents, const vector<pair<int, string>>& fileTwoContents) {
// Print header line for the diff table.
for (int i = 0; i <= 74; i++) {
cout << "*";
}
cout << "\n|" << setw(pIndex) << "Index" << " | ";
cout << "File One" << setw(pFileOneLineAmount) << "| ";
cout << "File Two" << setw(pFileTwoLineAmount) << "| " << endl;
for (int i = 0; i <= 74; i++) {
cout << "*";
}
// Sidebar Indices, print index for each line in the file.
int sideBarAmount = max(fileOneContents.size(), fileTwoContents.size());
cout << endl;
for (int j = 0; j < sideBarAmount; j++) {
if (j > 8) {
cout << "| " << j + 1 << " |"; // For indices greater than 9, adjust alignment.
}
else {
cout << "| " << j + 1 << " |"; // For single-digit indices, adjust alignment.
}
// Print lines from File One.
if (j < fileOneContents.size()) {
if(fileOneContents[j].second != fileTwoContents[j].second){
cout << left << setw(pFileOneLineAmount + 6) << fileOneContents[j].second;
}
else {
cout << " ";
}
}
else {
cout << setw(pFileOneLineAmount + 6) << "--null--"; // If no line exists, print "--null--".
}
cout << " | ";
// Print lines from File Two.
if (j < fileTwoContents.size()) {
if(fileOneContents[j].second != fileTwoContents[j].second){
cout << left << setw(pFileTwoLineAmount + 6) << fileTwoContents[j].second;
}
else {
cout << " ";// IM RIGHT HERE
}
}
else {
cout << setw(pFileTwoLineAmount + 6) << "--null--"; // If no line exists, print "--null--".
}
cout << "|" << endl;
}
// Print footer line for the diff table.
for (int i = 0; i <= 74; i++) {
cout << "*";
}
cout << endl;
}
// Prompting Service
void FileService::promptingServ(){
string userInput;
cout << "Would you like to make a change?" << endl;
cout << "Answer (Y/n): ";
cin >> userInput;
if (userInput == "y" || userInput == "Y"){
cout << "You chose yes." << endl;
}else{
cout <<" You chose no." << endl;
}
}
// Method that fuses the files together.
void FileService::fusionService(const vector<pair<int, string>>& fileOneContents,
const vector<pair<int, string>>& fileTwoContents, int pLineNum){
vector<pair<int, string>> fileFusedContents;
int baseFile;
// Copying Base File Info into New File
if(baseFile == 1){
for(int i=0;i<fileOneContents.size();i++){
fileFusedContents[i].first = fileOneContents[i].first;
fileFusedContents[i].second = fileOneContents[i].second;
}
}
if(baseFile == 2){
for(int j=0;j<fileTwoContents.size();j++){
fileFusedContents[j].first = fileTwoContents[j].first;
fileFusedContents[j].second = fileTwoContents[j].second;
}
}
}
// Function to read the contents of two files and compare them.
void FileService::diff(string pFileName, string pFileNameTwo) {
vector<pair<int, string>> fileOneContents;
vector<pair<int, string>> fileTwoContents;
// Get filenames from user.
cout << "Filename: ";
getline(cin, pFileName); // Read the first filename from input.
cout << "Filename: ";
getline(cin, pFileNameTwo); // Read the second filename from input.
ifstream inFile, inFileTwo; // Declare file stream objects for both files.
inFile.open(pFileName); // Open the first file.
inFileTwo.open(pFileNameTwo); // Open the second file.
if (inFile.fail() || inFileTwo.fail()) { // Check if files failed to open.
cout << "Error opening file." << endl;
return;
}
string line;
int lineNumber = 1;
// Read the contents of the first file and store in fileOneContents with line numbers.
while (getline(inFile, line)) {
fileOneContents.push_back(make_pair(lineNumber++, line));
}
lineNumber = 1; // Reset line number for the second file.
// Read the contents of the second file and store in fileTwoContents with line numbers.
while (getline(inFileTwo, line)) {
fileTwoContents.push_back(make_pair(lineNumber++, line));
}
// Close the files after reading.
inFile.close();
inFileTwo.close();
// Call the function to compare the contents of both files.
cout << endl;
compareFilesTwo(fileOneContents, fileTwoContents);
promptingServ();
}