On Mar 26, 11:11 pm, mathieu <mathieu.malate...@gmail.comwrote:
I am trying to implement a cross platform way of printing
colored strings (tty on unix and CONSOLE_SCREEN_BUFFER_INFO on
win32). As far as I understand there is no way to find out if
a C++ stream is attached to a tty, correct ?
Nothing portable. Even under Unix, you'd need to 1) determine
that the streambuf of the istream is a filebuf, 2) find the
corresponding fd (most implementations support this as an
extention, but it's not part of the standard), and then call
isatty. And even then, you don't know whether it will support
color, and if so, how to change the colors.
Can I do something like that instead:
int detect(std::istream &is)
{
if( &is == &cout ) return fileno(stdout);
...
}
The if doesn't tell you whether the device is a tty: it could be
a pipe or a file as well. And fileno() isn't a standard
function either; it's a Posix extension.
There might be something in curses which could help. (There is
a version of curses for Windows.) But it may be more than you
want or need.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34