Go to the documentation of this file.
7 #ifndef _POWERDXX_SYS_IO_HPP_
8 #define _POWERDXX_SYS_IO_HPP_
109 static constexpr
bool const value{
true};
122 static constexpr
bool const value{
false};
131 template <
auto ... Set>
struct set {};
151 template <
auto ... Set,
auto Value>
155 template <
class SetT, auto Value>
159 template <
class SetT, auto Value>
178 template <
class LSetT,
auto ... RSet>
180 enable_if<(contains_v<LSetT, RSet> && ...)> {};
183 template <
class LSetT,
class RSetT>
187 template <
class LSetT,
class RSetT>
212 for (
auto it =
str; it && *it; ++it) {
232 template <
typename ... CharTs>
233 constexpr
bool any(CharTs
const ... chars)
const {
234 for (
auto it =
str; it && *it; ++it) {
235 if (((*it == chars) || ...)) {
254 template <
typename ... CharTs>
255 constexpr
bool all(CharTs
const ... chars)
const {
256 for (
auto ch : {chars ...}) {
258 for (
auto it =
str; it && *it && !match; ++it) {
279 template <
class FileT,
feature ...>
291 return *
static_cast<FileT *
>(
this);
315 explicit operator bool()
const {
316 return this->handle && !feof(this->handle) &&
317 !ferror(this->handle);
329 return this->handle && feof(this->handle);
341 return this->handle && ferror(this->handle);
353 template <
class FileT,
feature ... Tail>
368 return fgetc(this->
handle);
384 template <
typename T>
387 return fread(&dst,
sizeof(T), 1, this->
handle);
407 template <
typename T, std::
size_t Count>
408 std::size_t
read(T (& dst)[Count], std::size_t
const count) {
410 return fread(&dst,
sizeof(T),
411 (count <= Count ? count : Count),
434 template <
auto CountFmt,
typename ... RefTs>
435 int scanf(
char const (& fmt)[CountFmt], RefTs & ... refs) {
437 return fscanf(this->
handle, fmt, &refs ...);
459 template <auto Count>
460 bool gets(
char (& dst)[Count]) {
473 template <
class FileT,
feature ... Tail>
494 template <
auto CountFmt,
typename ... ArgTs>
495 FileT &
printf(
char const (& fmt)[CountFmt], ArgTs
const & ... args) {
497 fprintf(this->
handle, fmt, args ...);
517 template <auto CountFmt>
518 FileT &
printf(
char const (& fmt)[CountFmt]) {
520 fprintf(this->
handle, fmt, 0);
540 template <std::
size_t Count>
541 FileT &
print(
char const (& msg)[Count]) {
543 fwrite(msg,
sizeof(
char), Count - 1, this->
handle);
557 FileT &
putc(
int const character) {
559 fputc(character, this->
handle);
575 template <
typename T>
578 fwrite(&src,
sizeof(T), 1, this->
handle);
598 template <
typename T, std::
size_t Count>
599 FileT &
write(T
const (& src)[Count], std::size_t
const count) {
601 fwrite(&src,
sizeof(T),
602 (count <= Count ? count : Count),
631 template <
class FileT,
feature ... Tail>
650 FileT &
seek(
long int const offset,
int const origin) {
652 fseek(this->
handle, offset, origin);
680 return ftell(this->
handle);
700 template <
feature ... Features>
701 class file<own, Features ...> final :
702 public file_feature<file<own, Features ...>, Features ...> {
732 explicit file(FILE *
const handle) :
751 template <
feature ... Superset,
755 file{move.release()} {}
791 ? nullptr : fopen(
filename, mode)} {}
809 std::swap(this->handle, move.handle);
826 template <
feature ... Superset,
829 std::swap(this->handle, move.handle);
854 auto const handle = this->handle;
855 this->handle =
nullptr;
867 fclose(this->handle);
868 this->handle =
nullptr;
889 template <
feature ... Features>
890 class file<link, Features ...> final :
891 public file_feature<file<link, Features ...>, Features ...> {
902 explicit file(FILE *
const handle) :
935 template <
feature ... Superset,
937 file(file<own, Superset ...> &&) =
delete;
959 this->handle = copy.get();
971 template <
feature ... Superset,
@ own
The file instance owns the FILE object.
file(FILE *const handle)
Take ownership of the given FILE object.
Definition: io.hpp:732
FileT & write(T const &src)
Write an object to file.
Definition: io.hpp:576
file(file< own, Superset ... > &&move)
Move construct from another owning file type instance.
Definition: io.hpp:754
FileT & print(char const (&msg)[Count])
Print an unformatted string, excluding the last character.
Definition: io.hpp:541
~file()
Free all resources.
Definition: io.hpp:796
void type
Provide the requested type.
Definition: io.hpp:104
Ask questions about the contents of a string.
Definition: io.hpp:193
Check whether a set type contains a value.
Definition: io.hpp:141
FileT & flush()
Flush file buffers.
Definition: io.hpp:615
file(FILE *const handle)
Use the given FILE object.
Definition: io.hpp:902
FILE * get() const
Provide the internal FILE object pointer.
Definition: io.hpp:983
file()
Default construct.
Definition: io.hpp:738
file< link, write > fout
File access instances for stdout.
Definition: io.hpp:1012
@ seek
The file type supports seek operations.
file(file< Ownership, Superset ... > const ©)
Copy construct from another file type instance.
Definition: io.hpp:927
Wrappers around native system interfaces.
Test a string whether it contains a set of characters.
Definition: io.hpp:197
FileT & printf(char const (&fmt)[CountFmt])
Output a printf style formatted string.
Definition: io.hpp:518
typename is_superset_of< LSetT, RSetT >::type is_superset_of_t
Check whether the left hand set is a superest of the right hand set.
Definition: io.hpp:188
feature
Feature flags for file type composition.
Definition: io.hpp:25
const char * filename(char const *const path)
Performs very rudimentary file name argument checks.
Definition: loadplay.cpp:77
long int tell()
Retrieve the current file position.
Definition: io.hpp:678
file()
Default construct.
Definition: io.hpp:908
static constexpr const bool value
The given expression is true.
Definition: io.hpp:109
Check whether the left hand set is a superest of the right hand set.
Definition: io.hpp:168
std::size_t read(T(&dst)[Count], std::size_t const count)
Read the requested number of objects from the file.
Definition: io.hpp:408
FileT & seek(long int const offset, int const origin)
Seek file position.
Definition: io.hpp:650
file & close()
Close the file.
Definition: io.hpp:865
FileT & printf(char const (&fmt)[CountFmt], ArgTs const &... args)
Output with printf style formatting.
Definition: io.hpp:495
Similar to std::enable_if, but it also has the value of the expression.
Definition: io.hpp:100
int scanf(char const (&fmt)[CountFmt], RefTs &... refs)
Read formatted input.
Definition: io.hpp:435
FILE * get() const
Provide the internal FILE object pointer.
Definition: io.hpp:841
Implements the base functionality of all file access types.
Definition: io.hpp:280
file(char const *const filename, char const *const mode)
Open a file by name.
Definition: io.hpp:783
file< link, read > fin
File access instances for stdin.
Definition: io.hpp:1017
struct sys::io::query::contains_ftor contains
Query the string for characters.
Pack a set of integral values in a type.
Definition: io.hpp:131
FileT & rewind()
Reset file position to the beginning of the file.
Definition: io.hpp:664
file(file &&move)
Move construct from a temporary.
Definition: io.hpp:721
ownership
Ownership relation to the underlying FILE object.
Definition: io.hpp:58
std::size_t read(T &dst)
Read the given object from the file.
Definition: io.hpp:385
file_feature(FILE *const handle)
Construct from a FILE object pointer.
Definition: io.hpp:300
bool error() const
Return whether the file instance is in an error state.
Definition: io.hpp:340
This namespace contains c++ wrappers for <cstdio> functionality.
Definition: io.hpp:18
int getc()
Read a single character from the file.
Definition: io.hpp:366
typename contains< SetT, Value >::type contains_t
Check whether a set type contains a value.
Definition: io.hpp:160
FILE * release()
Surrender ownership of the internal FILE object pointer.
Definition: io.hpp:853
FileT & putc(int const character)
Write a single character to the string.
Definition: io.hpp:557
friend class file
Friend all file classes for move assignment.
Definition: io.hpp:706
const char *const str
The string to ask about.
Definition: io.hpp:201
@ write
The file type supports write operations.
constexpr bool all(CharTs const ... chars) const
Check for a set of characters if all of them are part of the string.
Definition: io.hpp:255
@ link
The file instance refers to a FILE object managed somewhere else.
constexpr const auto is_superset_of_v
Check whether the left hand set is a superest of the right hand set.
Definition: io.hpp:184
bool eof() const
Return whether the file instance is in EOF state.
Definition: io.hpp:328
FILE * handle
A pointer to the underlying FILE object.
Definition: io.hpp:285
constexpr bool any(CharTs const ... chars) const
Check for a set of characters if at least one is part of the string.
Definition: io.hpp:233
Produces file access types around the C file handling facilities.
Definition: io.hpp:92
file< link, write > ferr
File access instances for stderr.
Definition: io.hpp:1007
@ read
The file type supports read operations.
constexpr const auto contains_v
Check whether a set type contains a value.
Definition: io.hpp:156
bool gets(char(&dst)[Count])
Read a line from the file.
Definition: io.hpp:460
constexpr bool operator()(char const ch) const
Check for a single character.
Definition: io.hpp:211
FileT & write(T const (&src)[Count], std::size_t const count)
Write an objects to file.
Definition: io.hpp:599