Migrate to latest project structure

This commit is contained in:
2025-01-08 12:56:53 +01:00
parent be5f03a64e
commit be496d3e12
15 changed files with 628 additions and 65 deletions

1
src/commit.inc Normal file
View File

@ -0,0 +1 @@
static const char* commit = "be5f03a64e7d0d27a88d6f341a8beff8ad788fc9";

84
src/main.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <iostream>
#include <fstream>
#include <string>
#include <random>
#include <cstdlib>
#include <stdexcept>
#include <getopt.h>
#include "version.hpp"
using namespace std;
static double next_random() {
static random_device dev;
static default_random_engine engine {dev()};
static uniform_real_distribution<double> dist {0, 1};
return dist(engine);
}
static string random_line_from_file(istream& file) {
// This function selects a line from a file by reading
// the file line by line, so the file is never in memory
// as a whole. Still, the chance a line is being picked
// is equal for every line.
string line;
string result;
for (int nr = 1; getline(file, line); ++nr) {
if (next_random() * nr < 1.0) {
result = line;
}
}
return result;
}
static void print_help() {
cout << "usage: ranlin [-h|--version]\n";
cout << " -h, --help show this help text and exit\n";
cout << " --version show version number and exit\n";
}
int main(int argc, char* argv[]) {
try {
int opt_char, opt_val;
struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0}
};
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) {
string arg {optarg ? optarg : ""};
switch (opt_char) {
case 0: {
// handle long-only options here
switch (opt_val) {
case 1:
cout << ranlin_version() << endl;
return EXIT_SUCCESS;
}
break;
}
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
throw runtime_error("unrecognized option");
}
}
if (optind == argc) {
// here when no file args
cout << random_line_from_file(cin) << '\n';
}
for (int i = optind; i < argc; ++i) {
try {
// process file argv[i]
ifstream infile {argv[1]};
cout << random_line_from_file(infile) << '\n';
} catch (const runtime_error& ex) {
cerr << "ranlin: " << ex.what() << '\n';
}
}
} catch (const exception& ex) {
cerr << "ranlin: " << ex.what() << '\n';
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

127
src/precomp.hpp Normal file
View File

@ -0,0 +1,127 @@
// C++98 (first official C++ standard)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if (__cplusplus >= 201103L) // C++11
#include <array>
#include <atomic>
#include <cfenv>
#include <chrono>
#include <cinttypes>
#include <codecvt> // deprecated in C++17, removed in C++26
#include <condition_variable>
#include <cstdint>
#include <cuchar>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif // C++11
#if (__cplusplus >= 201402L) // C++14
#include <shared_mutex>
#endif // C++14
#if (__cplusplus >= 201703L) // C++17
#include <any>
#include <charconv>
#include <execution>
#include <filesystem>
#include <memory_resource>
#include <optional>
#include <string_view>
#include <variant>
#endif // C++17
#if (__cplusplus >= 202002L) // C++20
#include <barrier>
#include <bit>
#include <compare>
#include <concepts>
#include <coroutine>
#include <format>
#include <latch>
#include <numbers>
#include <ranges>
#include <semaphore>
#include <source_location>
#include <span>
//#include <stop_token> not yet supported by clang 16
//#include <syncstream> not yet supported by clang 17
#include <version>
#endif // C++20
#if (__cplusplus >= 202302L) // C++23
#include <expected>
#include <flat_map>
#include <flat_set>
#include <generator>
#include <mdspan>
#include <print>
#include <spanstream>
#include <stacktrace>
#include <stdfloat>
#endif // C++23
#if (__cplusplus > 202302L) // C++26
#include <debugging>
#include <hazard_pointer>
#include <inplace_vector>
#include <linalg>
#include <rcu>
#include <text_encoding>
#endif // C++26

19
src/version.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "version.hpp"
#include <sstream>
std::string ranlin_version() {
#include "version.inc"
#include "commit.inc"
std::ostringstream oss;
oss << "ranlin version " << version;
#ifdef DEBUG
oss << " DEBUG";
#endif
oss << '\n';
if (commit[0] != '\0') {
oss << "build " << commit << ", ";
}
oss << __DATE__ << ", " << __TIME__ << '\n';
oss << "(c) Bob Polis, all rights reserved";
return oss.str();
}

8
src/version.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef _RANLIN_VERSION_H_
#define _RANLIN_VERSION_H_
#include <string>
std::string ranlin_version();
#endif // _RANLIN_VERSION_H_

1
src/version.inc Normal file
View File

@ -0,0 +1 @@
static const char* version = "1.0.1";