-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.h
121 lines (96 loc) · 2.41 KB
/
utils.h
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
#ifndef utils
#define utils
#include <iostream>
#include <fstream>
#include <sstream>
#include "md5.h"
#include <vector>
using namespace std;
vector<int> is_connected;
//Works well on linux (stackoverflow had an answer about tellg giving slightly less filesize in windows)
int get_filesize(char *filename)
{
ifstream input(filename, ios::binary);
input.seekg(0, ios::end);
return (int)input.tellg();
}
/////////////////////////////////////////////////////////////
// md5_from_file
// This function calculates the unique md5 hash of a file.
// path - path of the file.
string md5_from_file(const string path)
{
ifstream t(path);
stringstream buffer;
buffer << t.rdbuf();
//Calculate MD5 hash
string Temp = md5(buffer.str());
//Clean up
buffer.str("");
return Temp;
}
/////////////////////////////////////////////////////////////
struct filestat
{
string hash;
bool folder;
};
// Filemap function
typedef map<string, filestat> filemap;
typedef pair<string, filestat> fileobject;
/////////////////////////////////////////////////////////////
// Function to write the log file in txt to send to other clients
int WriteFile(string fname, filemap *m)
{
int count = 0;
if (m->empty())
return 0;
FILE *fp = fopen(fname.c_str(), "w");
if (!fp)
return -errno;
for (auto it = m->begin(); it != m->end(); it++)
{
fprintf(fp, "%s=%d,%s\n", it->first.c_str(), it->second.folder, (char *)(it->second.hash.c_str()));
count++;
}
fclose(fp);
return count;
}
/////////////////////////////////////////////////////////////
// Function to read the log file sent as text file
filemap ReadFile(string fname)
{
int count = 0;
FILE *fp = fopen(fname.c_str(), "r");
char *buf = 0;
size_t buflen = 0;
filemap m;
while (getline(&buf, &buflen, fp) > 0)
{
char *nl = strchr(buf, '\n');
if (nl == NULL)
continue;
*nl = 0;
char *sep = strchr(buf, '=');
if (sep == NULL)
continue;
*sep = 0;
sep++;
char *sep2 = strchr(sep, ',');
if (sep2 == NULL)
continue;
*sep2 = 0;
sep2++;
string s1 = buf;
filestat s2;
s2.hash = sep2;
s2.folder = atoi(sep);
m.insert(fileobject(s1, s2));
count++;
}
if (buf)
free(buf);
fclose(fp);
return m;
}
#endif