forked from OmerNachshon/VersionControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.cpp
91 lines (84 loc) · 2.24 KB
/
diff.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
#include "files.h"
#include "diff.h"
#include <vector>
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
bool compareRows(char *row1, int size1, char *row2, int size2)
{
if (size1 != size2)
return false;
for (int i = 0; i < size1; i++)
{
if (row1[i] != row2[i])
return false;
}
return true;
}
int diff(File *src, File *dest,bool printFlag)
{
int size1, size2, max, min,counter;
counter=0;
char **rows1 = read_lines(src, &size1);
char **rows2 = read_lines(dest, &size2);
// src, dest
// src, is the original file, the older file
// dest, is the newer file, that is being compared
// need to find removed, added, changed rows.
// row that is removed is like changed, and marked as -
// row that is added will be marked as +
// find bigger size
max = size1 > size2 ? size1 : size2;
min = size1 > size2 ? size2 : size1;
std::vector<std::string> v1(max);
std::vector<std::string> v2(max);
for (int i = 0; i < min; i++)
{
if (!compareRows(rows1[i], strlen(rows1[i]), rows2[i], strlen(rows2[i])))
{
std::string rowIndex = std::to_string(i + 1);
v1.push_back("+ " + rowIndex + " " + rows1[i]);
v2.push_back("- " + rowIndex + " " + rows2[i]);
counter+=1;
}
}
if (max == size1)
{
for (int i = min; i < max; i++)
{
std::string rowIndex = std::to_string(i + 1);
v1.push_back("+ " + rowIndex + " " + rows1[i]);
counter += 1;
}
}
if (max == size2)
{
for (int i = min; i < max; i++)
{
std::string rowIndex = std::to_string(i + 1);
v2.push_back("- " + rowIndex + " " + rows2[i]);
counter += 1;
}
}
// print differences
if(printFlag)
{
for (string str1 : v1)
std::cout << str1 ;
cout << "\n\n\n";
for (string str2 : v2)
std::cout << str2 ;
cout << "\n\n\n";
}
delete rows1;
free(rows2);
return counter;
}
void copyFile(char* source,char* destination)
{
std::ifstream src(source, std::ios::binary);
std::ofstream dst(destination, std::ios::binary);
dst << src.rdbuf();
}