-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.cpp
More file actions
82 lines (67 loc) · 1.75 KB
/
Copy pathutils.cpp
File metadata and controls
82 lines (67 loc) · 1.75 KB
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
#include "commands.h"
#include <random>
#include <string>
#include <fstream>
#include <unordered_map>
std::string cmd::utils::readFileLine(const std::string& path, int &index)
{
std::ifstream file(path);
std::string line;
if (file.is_open())
{
for (int i = 0; std::getline(file, line) && i < index; i++);
// validate next line
if (std::string next; !getline(file, next) || line.length() == 0)
index = 0;
else
index++;
file.close();
}
else
line = "[!] Could not open " + path;
return line;
}
std::string cmd::utils::readFileLineCached(const std::string& path, int index)
{
static std::unordered_map<std::string, std::vector<std::string>> seen_files;
if (seen_files.find(path) == seen_files.end())
{
seen_files[path] = {};
std::ifstream file(path);
if (!file.is_open())
{
return "[!] Could not open " + path;
}
std::string line;
while (std::getline(file, line)) {
seen_files[path].push_back(line);
}
file.close();
std::vector<std::string> lines = seen_files.at(path);
if (index >= lines.size())
{
return "[!] Out of range";
}
return lines.at(index);
}
else
{
std::vector<std::string> lines = seen_files.at(path);
if (index >= lines.size())
{
return "[!] Out of range";
}
return lines.at(index);
}
}
int cmd::utils::getRandomNumber(int min, int max)
{
if (min > max)
{
return 0;
}
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<int> distrib(min, max);
return distrib(gen);
}