So, das erste was ich direkt nach dem compilieren bekomme....
fifo konnte nicht geöffnet werden, da er noch nicht existiert. Das sollte durch dieses Programm erledigt werden
Auch wird nicht geprüft ob der fifo geöffnet wurde. Nachdem ich den Pfad angepasst und den Fifo erstellt habe, hab ich mal die Funktion getestet.
Mit einem Programm hab ich das dann mal für 3h durchgetestet und es hat keinen Crash gegeben (nur wenn der Fifo nicht da ist).
Code: Alles auswählen
g++ --std=c++14 SendRnd.cpp -l boost_program_options -o sendrnd
./sendrnd --of /home/georg/fifo --alphabet=random --runs=1000000 --min_length=1000 --max_length=1280000
#include <array>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
enum class alphabet_t
{
alpha,
alnum,
print,
rnd
};
int run(std::ostream &os, const alphabet_t alphabet, const int min_length,
const int max_length, const int runs)
{
// clang-format off
// - 1 because 0 based array index
std::uniform_int_distribution<> dis_alpha (0, 2 * 26 - 1);
std::uniform_int_distribution<> dis_alnum (0, 2 * 26 + 10 - 1);
std::uniform_int_distribution<> dis_print (0, 95 - 1);
std::uniform_int_distribution<> dis_rnd (0, 256 - 1);
std::array<char, 95> int2char =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '!', '"',
'#', '$', '%', '&', '(', ')', '*', '+',
',', '-', '.', '/', ':', ';', '<', '=',
'>', '?', '@', '[', '\\', ']', '^', '_',
'`', '{', '|', '}', '~', '\'', ' '
};
// clang-format on
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<> dis_length(min_length, max_length);
for (int i = 0; i < runs; ++i)
{
const int len = dis_length(gen);
std::string buffer;
buffer.reserve(len);
for (int l = 0; l < len; ++l)
{
switch (alphabet)
{
default:
case alphabet_t::alpha:
buffer += int2char[dis_alpha(gen)];
break;
case alphabet_t::alnum:
buffer += int2char[dis_alnum(gen)];
break;
case alphabet_t::print:
buffer += int2char[dis_print(gen)];
break;
case alphabet_t::rnd:
buffer += char(dis_rnd(gen));
break;
}
}
// write out buffer and the expected \n\0 ending
os.write(buffer.data(), buffer.size());
os.write("\n", 1);
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
try
{
po::options_description desc("SendRnd options");
// clang-format off
desc.add_options()
("help", "produce help message")
("alphabet", po::value< std::string >(), "alpha (default)|alnum|print|random (may contain additional newlines and zeros)")
("min_length", po::value< int >(), "minimum length between newline (Default 128k)")
("max_length", po::value< int >(), "maximum length between newline (Default 1024k)")
("runs", po::value< int >(), "number of runs")
("of", po::value< std::string >(), "output file (use - for stdout = default)")
;
// clang-format on
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << "\n";
return EXIT_FAILURE;
}
// this values are now needed
alphabet_t alphabeth = alphabet_t::alpha;
if (vm.count("alphabet"))
{
std::string value = vm["alphabet"].as<std::string>();
if (value == "alpha" || value == "default")
{
alphabeth = alphabet_t::alpha;
}
else if (value == "alnum")
{
alphabeth = alphabet_t::alnum;
}
else if (value == "print")
{
alphabeth = alphabet_t::print;
}
else if (value == "random")
{
alphabeth = alphabet_t::rnd;
}
else
{
std::cerr << "Invalid alphabet mode" << std::endl;
return EXIT_FAILURE;
}
}
// length of the output
int min_length = 128 * 1024;
if (vm.count("min_length"))
{
min_length = vm["min_length"].as<int>();
if (min_length < 1)
{
std::cerr << "min_length must be >=1" << std::endl;
return EXIT_FAILURE;
}
}
int max_length = 1024 * 1024;
if (vm.count("max_length"))
{
max_length = vm["max_length"].as<int>();
if (max_length < 1)
{
std::cerr << "max_length must be >=1" << std::endl;
return EXIT_FAILURE;
}
}
if (max_length < min_length)
{
std::cerr << "min_length must be smaller or equal than max_length"
<< std::endl;
return EXIT_FAILURE;
}
if (min_length < 1)
{
std::cerr << "min_length must >= 1" << std::endl;
return EXIT_FAILURE;
}
if (max_length < 1)
{
std::cerr << "max_length must >= 1" << std::endl;
return EXIT_FAILURE;
}
// runs
int runs = -1;
if (!vm.count("runs"))
{
std::cout << desc << "\n";
return EXIT_FAILURE;
}
else
{
runs = vm["runs"].as<int>();
}
if (runs < 1)
{
std::cerr << "runs must be >= 1" << std::endl;
return EXIT_FAILURE;
}
// out
std::string fname = "-";
if (vm.count("of"))
{
fname = vm["of"].as<std::string>();
}
if (fname != "-")
{
std::ofstream ofs(fname.c_str(), std::ios_base::binary);
if (!ofs.is_open())
{
std::cerr << "Could not open output file: " << fname
<< std::endl;
return EXIT_FAILURE;
}
return run(ofs, alphabeth, min_length, max_length, runs);
}
else
{
return run(std::cout, alphabeth, min_length, max_length, runs);
}
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
}
return EXIT_FAILURE;
}