Skip to content

Commit e570eab

Browse files
authored
Add files via upload
1 parent 1d1e97b commit e570eab

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

utility.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <map>
2+
#include <string>
3+
#include <vector>
4+
#include <string.h>
5+
#include <sstream>
6+
#include <fstream>
7+
#include <algorithm>
8+
9+
using std::map;
10+
using std::ios;
11+
using std::cout;
12+
using std::fstream;
13+
using std::string;
14+
using std::vector;
15+
using std::stringstream;
16+
17+
template <typename T, typename U>
18+
void print_map(map<T, U> to_print) {
19+
// a templatised function to output a map's key value pairs
20+
21+
for (auto itr = to_print.begin(); itr != to_print.end(); ++itr) {
22+
cout << itr->first << ": " << itr->second << "\n";
23+
}
24+
}
25+
26+
string charToString(char arr[]) {
27+
// function that returns a string representation of a character array
28+
29+
string str = "";
30+
31+
for (int i = 0; arr[i] != '\0'; i++)
32+
str += arr[i];
33+
34+
return str;
35+
}
36+
37+
vector<string> split_string(const string str, const char delim) {
38+
39+
// this is a custom function to split a string
40+
// returns a vector of "words", according to the delimiter char
41+
42+
string token;
43+
vector<string> out;
44+
stringstream ss(str);
45+
46+
while (getline(ss, token, delim)) // splitting or tokenizing the string according to the specified delimiter
47+
out.push_back(token); // adding each token to the vector
48+
49+
return out;
50+
}
51+
52+
string get_salt_hash(const string file, const string username) {
53+
54+
// this function reads the file (typically shadow.txt) and finds the value corresponding to the specified username
55+
// the value is then parsed to get the salt and hash (as one string) for this username
56+
// salt and hash are returned as one string
57+
58+
string line, salt_hash;
59+
60+
fstream infile;
61+
infile.open(file, ios::in);
62+
63+
if (infile.is_open()) {
64+
while (getline(infile, line)) {
65+
if (strstr(line.c_str(), username.c_str())) { // if line has specified username
66+
vector<string> tokens = split_string(line, ':'); // split by " : " character
67+
salt_hash = tokens[1]; // salt and hash are after first colon and before second
68+
break;
69+
}
70+
}
71+
72+
return salt_hash;
73+
}
74+
75+
else {
76+
cout << "Shadow file not opened :(\n";
77+
return "";
78+
}
79+
}
80+
81+
map<int, string> divide_alphabet(const int slave_procs) {
82+
83+
// this function divides the alphabet characters and assigns certain number of characters to each process
84+
// also takes into account the case when alphabet is not perfectly divisible by number of slave processes
85+
// so may also assign master some characters
86+
// the distribution is stored in a map with (key, value) pair being (process rank, characters assigned)
87+
// rank 0 is master, rank 1 is slave 1, rank 2 is slave 2 and so on...
88+
89+
map<int, string> distrib;
90+
string alphabet = "abcdefghijklmnopqrstuvwxyz";
91+
random_shuffle(alphabet.begin(), alphabet.end());
92+
93+
// case 1: alphabet perfectly divisible by number of slaves
94+
if (alphabet.length() % slave_procs == 0) {
95+
int chunk = alphabet.length() / slave_procs;
96+
97+
distrib[0] = ""; // no letters assigned to master (characters can be evenly distributed among the slaves), still we keep a record for master in case required
98+
99+
int index = 0;
100+
for (int i = 1; i <= slave_procs; i++) {
101+
int start = index;
102+
103+
string letters = alphabet.substr(start, chunk);
104+
105+
distrib[i] = letters; // assigning letters to that slave rank
106+
107+
index += chunk;
108+
}
109+
110+
}
111+
112+
// case 2: alphabet not perfectly divisible, some characters will have to be allotted to master too
113+
else {
114+
int mod = alphabet.length() % slave_procs;
115+
116+
distrib[0] = alphabet.substr(0,mod);
117+
118+
int new_len = alphabet.length()-mod;
119+
int mod1 = new_len/slave_procs;
120+
121+
int index = mod;
122+
123+
for (int i = 1 ; i <= slave_procs; i++) {
124+
int start = index;
125+
126+
string letters = alphabet.substr(start, mod1);
127+
128+
distrib[i] = letters;
129+
130+
index+=mod1;
131+
}
132+
}
133+
134+
return distrib;
135+
}

0 commit comments

Comments
 (0)