Repository navigation
Expand file tree
/
Copy pathflagsgenerator.cpp
More file actions
142 lines (109 loc) · 3.41 KB
/
Copy pathflagsgenerator.cpp
File metadata and controls
142 lines (109 loc) · 3.41 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "flagsgenerator.hpp"
#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <vector>
std::mt19937 rng(std::random_device{}());
Item parseItem(const json &j) {
Item it;
if (j.is_string()) {
it.isString = true;
it.value = j.get<std::string>();
return it;
}
if (j.is_object()) {
it.isString = false;
for (auto &[key, val] : j.items()) {
it.name = key;
for (const auto &child : val) {
it.children.push_back(parseItem(child));
}
}
}
return it;
}
// -------- ANTIGA GERACAO DE STRING --------
// Agora ela só gera uma string a partir de um Item
std::string generateRandomFromItem(const Item &it);
std::string pickChild(const Item &it) {
if (it.children.empty())
return "";
std::uniform_int_distribution<> dist(0, it.children.size() - 1);
return generateRandomFromItem(it.children[dist(rng)]);
}
std::string generateRandomFromItem(const Item &it) {
if (it.isString)
return it.value;
std::string base = it.name;
std::string nameOnly;
bool hasPar = false;
bool hasAngle = false;
if (base.find("()") != std::string::npos) {
nameOnly = base.substr(0, base.size() - 2);
hasPar = true;
} else if (base.find("<>") != std::string::npos) {
nameOnly = base.substr(0, base.size() - 2);
hasAngle = true;
} else {
nameOnly = base;
}
std::string inner = pickChild(it);
if (hasPar)
return nameOnly + "(" + inner + ")";
if (hasAngle)
return nameOnly + "<" + inner + ">";
return nameOnly + "(" + inner + ")";
}
// ----------------------------------------------------
// NOVO: generateRandomFlags agora retorna vector<Item>
// ----------------------------------------------------
std::vector<Item> generateRandomFlags(const std::vector<Item> &items) {
std::uniform_int_distribution<> sizeDist(3, 50);
int count = sizeDist(rng);
std::uniform_int_distribution<> dist(0, items.size() - 1);
std::vector<Item> out;
out.reserve(count);
for (int i = 0; i < count; i++) {
out.push_back(items[dist(rng)]);
}
return out;
}
// -----------------------------------------------------------
// NOVO: Converte vector<Item> para string com flags separadas
// -----------------------------------------------------------
std::string itemsToFlagString(const std::vector<Item> &list) {
std::string out;
for (size_t i = 0; i < list.size(); i++) {
out += generateRandomFromItem(list[i]);
if (i + 1 < list.size())
out += ",";
}
return out;
}
Item random_item(const std::vector<Item>& FLAGS) {
if (FLAGS.empty()) {
// segurança caso o JSON esteja vazio
Item empty;
empty.isString = true;
empty.value = "";
return empty;
}
std::uniform_int_distribution<> dist(0, FLAGS.size() - 1);
return FLAGS[dist(rng)];
}
// int main() {
int run() {
json j;
std::ifstream f("flags.json");
f >> j;
std::vector<Item> items;
for (const auto &entry : j["Flags"])
items.push_back(parseItem(entry));
// agora retorna lista de Item
std::vector<Item> chosen = generateRandomFlags(items);
// converter lista → string
std::string result = itemsToFlagString(chosen);
std::cout << "Flags aleatórias:\n" << result << "\n\n";
return 0;
}