-
Notifications
You must be signed in to change notification settings - Fork 0
/
useful.cpp
61 lines (48 loc) · 1.73 KB
/
useful.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
#include "useful.h"
/*** Takes the name (nom) of a file and store each new line in a case of an array given as parameter ***/
void openFile(std::string nom, std::vector<std::string*>& tab) {
std::fstream file{ nom };
std::string temp;
std::string* value;
if (!file) { std::cout << "Error : file can't be opened"; }
else {
while (getline(file, temp)) {
value = new std::string;
*value = temp;
tab.push_back(value);
}
}
}
void openFile(std::string nom, std::vector<unsigned int>& tab) {
std::fstream file{ nom };
std::string temp;
unsigned int value;
if (!file) { std::cout << "Error : file can't be opened"; }
else {
while (getline(file, temp)) {
value = std::stoul(temp);
tab.push_back(value);
}
}
}
///**************************************************************************************************///
///**** Returns the byte i of elem ; return a special velue if i is larger than the number of bytes of elem ****///
short int getByte(unsigned int elem, unsigned int i) {
if (sizeof(elem) > i) { return (elem >> ((3-i)*8)&(255)); }
else return -1;
}
short int getByte(std::string* elem, unsigned int i) {
if ((*elem).size() > i) { return (*elem)[i]; }
else return -1;
}
///************************************************************************************************************///
///**** Return the maximum number of bytes needed ****///
std::size_t getMaxLength(std::vector<unsigned int>& tab) {
return 4;
}
std::size_t getMaxLength(std::vector<std::string*>& tab) {
std::size_t maxLength = 0;
for (const auto& elem : tab) { if ((*elem).size() > maxLength) maxLength = (*elem).size(); }
return maxLength;
}
///***************************************************///