soi-header/include/soi

95 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2019-10-06 16:45:29 +02:00
// -*- c++ -*-
// Copyright Johannes Kapfhammer 2019-2022.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
2022-03-31 21:41:11 +02:00
#ifndef SOI_HEADER_INCLUDED
#define SOI_HEADER_INCLUDED
2019-10-06 16:45:29 +02:00
#include "bits/include-all.hpp"
#include "bits/soi-dbg.hpp"
2019-10-06 18:38:23 +02:00
#include <fstream>
2019-10-06 16:45:29 +02:00
2019-10-06 18:37:37 +02:00
namespace soi {
2019-10-06 17:19:10 +02:00
2019-10-06 16:45:29 +02:00
void check_for_eof() {
2019-10-06 18:38:23 +02:00
if (!(std::cin >> std::ws).eof())
std::cerr << "WARNING: didn't read the whole input\n";
2019-10-06 16:45:29 +02:00
}
void noninteractive_check_eof() {
// make symbol 0x1A (Ctrl+Z) a whitespace
struct console_ctype : std::ctype<char> {
2019-10-06 18:38:23 +02:00
static const mask *get_table() {
2019-10-06 16:45:29 +02:00
static const std::array<mask, table_size> table = []() {
std::array<mask, table_size> table;
std::copy(classic_table(), classic_table() + table_size, table.begin());
table['\x1a'] |= space;
return table;
}();
return table.data();
}
2019-10-06 18:38:23 +02:00
console_ctype(std::size_t refs = 0) : ctype(get_table(), false, refs) {}
2019-10-06 16:45:29 +02:00
};
std::cin.imbue(std::locale(std::cin.getloc(), new console_ctype));
check_for_eof();
}
bool should_check_for_eof() {
2019-10-06 18:38:23 +02:00
if (const char *eofcheck_enabled = std::getenv("SOI_EOFCHECK")) {
2019-10-06 16:45:29 +02:00
if (!std::strcmp(eofcheck_enabled, "1"))
return true;
if (!std::strcmp(eofcheck_enabled, "0"))
return false;
}
return false;
}
void initialize_debug() {
std::cout << std::unitbuf; // enable automatic flushing
std::cin.exceptions(std::ifstream::failbit | std::ifstream::badbit);
std::ios::sync_with_stdio(false);
2019-10-06 17:19:10 +02:00
2019-10-06 18:38:23 +02:00
if (should_check_for_eof() && std::atexit(noninteractive_check_eof) != 0) {
std::cerr
<< "WARNING: soi.h -- registration of sanity check at exit failed\n";
2019-10-06 16:45:29 +02:00
}
2019-10-06 18:37:37 +02:00
soi::detail::dbg_init();
2019-10-06 16:45:29 +02:00
}
2019-10-06 17:19:10 +02:00
2019-10-06 18:37:37 +02:00
struct soi_initializer {
soi_initializer(bool release) {
2019-10-06 16:45:29 +02:00
if (release) {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
} else {
initialize_debug();
}
}
};
#ifdef SOI_RELEASE
2019-10-06 18:37:37 +02:00
soi_initializer soi_initializer_{true};
2019-10-06 16:45:29 +02:00
#else
2019-10-06 18:37:37 +02:00
soi_initializer soi_initializer_{false};
2019-10-06 16:45:29 +02:00
#endif
2019-10-06 17:19:10 +02:00
void interactive_task() {
std::cin.tie(&std::cout);
}
2019-10-06 18:37:37 +02:00
} // end namespace soi
2019-10-06 16:45:29 +02:00
#include "bits/soi-io.hpp"
#include "bits/soi-redirect.hpp"
#include "bits/soi-deprecate.hpp"
using soi::interactive_task;
2019-10-06 16:45:29 +02:00
#define int int64_t
using namespace std;
2022-03-31 21:41:11 +02:00
#endif // SOI_HEADER_INCLUDED