-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_sha_file.hpp
72 lines (64 loc) · 1.49 KB
/
retrieve_sha_file.hpp
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
namespace retrieve_sha_file
{
map<string, string> get_map_from(string path)
{
map<string, string> toret;
fstream f(path, std::ios_base::in);
if (f.is_open())
{
string line;
while (getline(f, line)) //reading line by line of file
{
stringstream ss(line); //tokenising using space filename sha
string filename = "";
string sha = "";
ss >> filename;
ss >> sha;
toret[filename] = sha;
}
}
else
{
perror("unable to open file inf get_map_from function");
exit(1);
}
return toret;
}
void retrieve_sha_file(string s, string vno)
{
char cwd[PATH_MAX]; //defined in limits
string mygit_path = "";
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
mygit_path = cwd;
mygit_path += "/.mygit/";
}
else
{
perror("unable to get current working directory");
exit(1);
}
//READING VERSION NUMBER AND MAP OF ALREADY ADDED FILES
//-----------------------------------------------------
string vcpath = mygit_path + vno + "/index.txt";
//cout << vcpath << endl;
map<string, string> filemap = get_map_from(vcpath);
string ans = "";
for (auto x : filemap)
{
if (x.second == s)
{
ans = x.first;
break;
}
}
if (ans == "")
{
cout << "file with given sha not found" << endl;
}
else
{
cout << ans << endl;
}
}
}