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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <cstdio>
#include <iosfwd>
namespace cm {
namespace StdIo {
/**
* Identify the kind of terminal to which a stream is attached, if any.
*/
enum class TermKind
{
/** Not an interactive terminal. */
None,
/** A VT100 terminal. */
VT100,
#ifdef _WIN32
/** A Windows Console that does not support VT100 sequences. */
Console,
#endif
};
/**
* Represent stdin, stdout, or stderr stream metadata.
*/
class Stream
{
public:
/** The kind of terminal to which the stream is attached, if any. */
TermKind Kind() const { return this->Kind_; }
/** The underlying C++ stream. */
std::ios& IOS() const { return this->IOS_; }
/** The underlying file descriptor. */
int FD() const { return this->FD_; }
#ifdef _WIN32
/** The underlying HANDLE of an attached Windows Console, if any. */
void* Console() const { return this->Console_; }
#endif
protected:
enum class Direction
{
In,
Out,
};
Stream(std::ios& s, FILE* file, Direction direction);
~Stream(); // NOLINT(performance-trivially-destructible)
private:
std::ios& IOS_;
int FD_ = -1;
TermKind Kind_ = TermKind::None;
#ifdef _WIN32
void* Console_ = nullptr;
unsigned long ConsoleOrigMode_ = 0;
#endif
};
/**
* Represent stdin metadata.
*/
class IStream : public Stream
{
friend class Globals;
IStream(std::istream& is, FILE* file);
public:
/** The underlying C++ stream. */
std::istream& IOS() const;
};
/**
* Represent stdout or stderr metadata.
*/
class OStream : public Stream
{
friend class Globals;
OStream(std::ostream& os, FILE* file);
public:
/** The underlying C++ stream. */
std::ostream& IOS() const;
};
/** Metadata for stdin. */
IStream& In();
/** Metadata for stdout. */
OStream& Out();
/** Metadata for stderr. */
OStream& Err();
}
}
|