This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
69 lines (55 loc) · 1.69 KB
/
utils.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
62
63
64
65
66
67
68
69
#include "utils.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <vector>
#include <algorithm>
bool utils::iequals(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(),
b.begin(), b.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
});
}
bool utils::askForConfirmation(const char* program) {
char in[1];
std::cout << "Download and install " << program << "? [Y/n]: ";
std::cin.getline(in, 2, '\n');
if (iequals(in, "y") || iequals(in, "")) {
return true;
} else {
std::cout << "Operation cancelled by user" << std::endl << std::endl;
return false;
}
}
int utils::askForChoice(const char* question, std::vector<std::string> choices) {
char in[1];
std::cout << question << std::endl;
for (auto i = 0; i < choices.size(); ++i) {
std::cout << "[" << i + 1 << "] : " << choices[i] << std::endl;
}
std::cout << std::endl << "Enter an option: ";
std::cin.getline(in, 2, '\n');
return std::stoi(in) - 1;
}
std::string utils::readFile(const char* filename) {
std::filebuf fb;
fb.open(filename, std::ios::in);
std::istream is (&fb);
std::ostringstream oss;
oss << is.rdbuf();
std::string output = oss.str();
if (!output.empty() && output[output.length()-1] == '\n') {
output.erase(output.length()-1);
}
return output;
}
int utils::findInVector(std::vector<std::string> arr, std::string item) {
for (auto i = 0; i < arr.size(); ++i) {
if (iequals(arr[i], item))
return i;
}
return -1;
}