-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelta.cpp
More file actions
182 lines (157 loc) · 5.8 KB
/
delta.cpp
File metadata and controls
182 lines (157 loc) · 5.8 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
//
// delta.cpp
// Project 4
//
// Created by Nihar Tamhankar on 5/27/16.
// Copyright © 2016 Nihar Tamhankar. All rights reserved.
//
///create get next item
/// add items to hash table
//take new file and find item in hash table
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "Node.h"
using namespace std;
void createDelta(istream& oldf, istream& newf, ostream& deltaf){
string Oldfilestring;
string line;
int N=16;
while(getline(oldf,line)){ ///as long as not end of file, adding contents of file into a string and adding newline as necessary
Oldfilestring+=line;
if(!oldf.eof()){
Oldfilestring+='\n';
}
}
string newFilestring;
while(getline(newf, line)){///as long as not end of file, adding contents of file into a string and adding newline as necessary
newFilestring+=line;
if(!newf.eof()){
newFilestring+='\n';
}
}
NodeHashTable oldstringTokens(2000);
int currentindex=0;
while(currentindex<Oldfilestring.size()){ ///until end of string substringing and adding to hashtable
Node* s= new Node;
s->token=Oldfilestring.substr(currentindex,N);
s->index=currentindex;
oldstringTokens.insertNode(s);
currentindex=currentindex+N;
}
currentindex=0;
int startunmatchedindex=0;
while(currentindex<newFilestring.length()){
string token=newFilestring.substr(currentindex,N);
Node* matchnode=oldstringTokens.find(token);
if(matchnode!=nullptr){
if(currentindex > startunmatchedindex){ //for chracters behind matches, creating add instruction
string delta=newFilestring.substr(startunmatchedindex,currentindex-startunmatchedindex);
deltaf<<"A"<<(currentindex-startunmatchedindex)<<":"<<delta;
}
string oldtoken=matchnode->token;
int oldtokenIndex=(matchnode->index)+(int)(oldtoken.size());
currentindex+=(int)oldtoken.size();
while(oldtokenIndex<Oldfilestring.size() && currentindex<newFilestring.size()){
//once match has been found looking beyond to see if more match
if (Oldfilestring[oldtokenIndex] != newFilestring[currentindex]) {
break;
}
oldtoken += oldtoken[oldtokenIndex];
++oldtokenIndex;
++currentindex;
}
// make copy to delta file with copy instruction matchedNode->index, oldToken string (actual characters and size)
deltaf<<"C"<<oldtoken.length()<<","<<matchnode->index;
startunmatchedindex=currentindex;
}
else { ///no match found
currentindex += 1;
}
}
//copy last unmatched string
// copy all the unmatched characters
if(currentindex > startunmatchedindex){
string delta=newFilestring.substr(startunmatchedindex,currentindex-startunmatchedindex);
deltaf<<"A"<<(currentindex-startunmatchedindex)<<":"<<delta;
}
}
bool getInt(istream& inf, int& n)
{
char ch;
if (!inf.get(ch) || !isascii(ch) || !isdigit(ch))
return false;
inf.unget();
inf >> n;
return true;
}
bool getCommand(istream& inf, char& cmd, int& length, int& offset)
{
if (!inf.get(cmd) || (cmd == '\n' && !inf.get(cmd)) )
{
cmd = 'x'; // signals end of file
return true;
}
char ch;
switch (cmd)
{
case 'A':
return getInt(inf, length) && inf.get(ch) && ch == ':';
case 'C':
return getInt(inf, length) && inf.get(ch) && ch == ',' && getInt(inf, offset);
}
return false;
}
bool applyDelta(istream& oldf, istream& deltaf, ostream& newf){
string Oldfilestring;
string line;
while(getline(oldf,line)){ ///as long as not end of file, adding contents of file into a string and adding newline as necessary
Oldfilestring+=line;
if(!oldf.eof()){
Oldfilestring+='\n';
}
}
int length;
int offset;
char cmd;
while(getCommand(deltaf, cmd, length, offset)==true){ //checkinggetCommand is valid
bool leaveloop=false; ///for checking if at end of the file
switch (cmd) {
case 'x':{
leaveloop=true; ///at end of the file
break;
}
case 'A':{
char* addstring=new char[length+1]; //using C++ read function from where character is and for how long then adding string
deltaf.read(addstring, length);
addstring[length]='\0';
newf<<addstring;
delete[] addstring;
break;
}
case 'C':
string copy=Oldfilestring.substr(offset,length); //copying from old file with index and length
newf<<copy;
break;
}
if(leaveloop==true){ ///have reached end of file
return true;
}
}
return false;
}
int main(){
ifstream oldf("/Users/nihartamhankar/Downloads/p4test/greeneggs1.txt");
ifstream newf("/Users/nihartamhankar/Downloads/p4test/greeneggs2.txt");
ofstream deltaf("/Users/nihartamhankar/Downloads/p4test/greeneggsdelta16.txt");
ofstream changef("/Users/nihartamhankar/Downloads/p4test/greeneggschanges.txt");
createDelta(oldf, newf, deltaf);
oldf.close();
newf.close();
deltaf.close();
ifstream old2f("/Users/nihartamhankar/Downloads/p4test/greeneggs1.txt");
ifstream delta2f("/Users/nihartamhankar/Downloads/p4test/greeneggsdelta16.txt");
applyDelta(old2f, delta2f, changef);
cout<<"Passed all tests"<<endl;
}