-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathname-generator.cpp
More file actions
171 lines (149 loc) · 5.71 KB
/
name-generator.cpp
File metadata and controls
171 lines (149 loc) · 5.71 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
namespace fs = std::filesystem;
/* Helper: get environment variable or fallback */
std::string getEnv(const std::string& var, const std::string& fallback) {
const char* val = std::getenv(var.c_str());
return (val && *val) ? std::string(val) : fallback;
}
/* Helper: read all non‑empty lines from a file */
std::vector<std::string> readLines(const fs::path& filePath) {
std::vector<std::string> lines;
std::ifstream in(filePath);
if (!in) {
std::cerr << "Error: cannot open file " << filePath << "\n";
std::exit(1);
}
std::string line;
while (std::getline(in, line)) {
if (!line.empty())
lines.push_back(line);
}
return lines;
}
/* Helper: recursively collect all regular files under a directory */
std::vector<fs::path> collectFiles(const fs::path& folder) {
std::vector<fs::path> files;
if (!fs::exists(folder) || !fs::is_directory(folder)) {
std::cerr << "Error: folder does not exist or is not a directory: " << folder << "\n";
std::exit(1);
}
for (auto const& entry : fs::recursive_directory_iterator(folder)) {
if (fs::is_regular_file(entry.path())) {
files.push_back(entry.path());
}
}
if (files.empty()) {
std::cerr << "Error: no regular files found in folder " << folder << "\n";
std::exit(1);
}
return files;
}
/* Helper: pick a random element from a vector */
template <typename T>
const T& randomChoice(const std::vector<T>& vec, std::mt19937& rng) {
std::uniform_int_distribution<std::size_t> dist(0, vec.size() - 1);
return vec[dist(rng)];
}
/* Helper: convert a string to lower case */
std::string toLower(const std::string& s) {
std::string out;
out.reserve(s.size());
std::transform(s.begin(), s.end(), std::back_inserter(out),
[](unsigned char c) { return std::tolower(c); });
return out;
}
/* Debug printer – mirrors the shell script's debugger function */
void debugger(const std::string& adjective,
const std::string& noun,
const fs::path& adjFile,
const fs::path& adjFolder,
const fs::path& nounFile,
const fs::path& nounFolder,
std::size_t countzero,
std::size_t counto) {
const char* dbg = std::getenv("DEBUG");
if (!dbg || std::string(dbg) != "true")
return;
std::cerr << "DEBUG:\n";
std::cerr << " adjective : " << adjective << "\n";
std::cerr << " noun : " << noun << "\n";
std::cerr << " ADJ_FILE : " << adjFile << "\n";
std::cerr << " ADJ_FOLDER: " << adjFolder << "\n";
std::cerr << " NOUN_FILE : " << nounFile << "\n";
std::cerr << " NOUN_FOLDER: " << nounFolder << "\n";
std::cerr << " " << countzero << " > " << counto << "\n";
}
/* Determine terminal height – fallback to 24 if we cannot query it */
std::size_t terminalLines() {
// Simple fallback; more sophisticated approaches would use ioctl or termsize libs.
return 24;
}
/* Resolve a file path from an environment variable, falling back to a random file in a folder */
fs::path resolveFile(const std::string& envVar,
const fs::path& folder,
std::mt19937& rng) {
std::string envPath = getEnv(envVar, "");
if (!envPath.empty()) {
fs::path p = fs::absolute(envPath);
if (!fs::exists(p) || !fs::is_regular_file(p)) {
std::cerr << "Error: environment variable " << envVar
<< " points to a non‑regular file: " << p << "\n";
std::exit(1);
}
return p;
}
// No env var – pick a random file from the folder
std::vector<fs::path> files = collectFiles(folder);
return randomChoice(files, rng);
}
int main() {
// Seed RNG
std::random_device rd;
std::mt19937 rng(rd());
// Resolve configuration (environment variables with defaults)
const std::string separator = getEnv("SEPARATOR", "-");
const std::string countoEnv = getEnv("counto", "");
std::size_t counto = 0;
if (!countoEnv.empty()) {
try {
counto = static_cast<std::size_t>(std::stoul(countoEnv));
} catch (...) {
counto = terminalLines();
}
} else {
counto = terminalLines();
}
const fs::path here = fs::current_path();
const fs::path nounFolder = fs::path(getEnv("NOUN_FOLDER", (here / "nouns").string()));
const fs::path adjFolder = fs::path(getEnv("ADJ_FOLDER", (here / "adjectives").string()));
// Resolve the actual files, respecting NOUN_FILE / ADJ_FILE env vars
const fs::path nounFile = resolveFile("NOUN_FILE", nounFolder, rng);
const fs::path adjFile = resolveFile("ADJ_FILE", adjFolder, rng);
// Load all lines from the selected files
const std::vector<std::string> nounLines = readLines(nounFile);
const std::vector<std::string> adjLines = readLines(adjFile);
if (nounLines.empty() || adjLines.empty()) {
std::cerr << "Error: selected noun or adjective file is empty.\n";
return 1;
}
// Main loop – generate names
std::size_t countzero = 0;
while (countzero < counto) {
std::string rawNoun = randomChoice(nounLines, rng);
std::string rawAdj = randomChoice(adjLines, rng);
std::string noun = toLower(rawNoun);
std::string adjective = rawAdj; // keep original case
debugger(adjective, noun, adjFile, adjFolder, nounFile, nounFolder, countzero, counto);
std::cout << adjective << separator << noun << "\n";
++countzero;
}
return 0;
}