473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

fileno / isatty on C++ stream

Hi there,

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 ?

Can I do something like that instead:

int detect(std::istream &is)
{
if( &is == &cout ) return fileno(stdout);
...
}

Thanks
-Mathieu
Mar 26 '08 #1
4 4062
mathieu wrote:
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 ?
There is no Standard way to find that out, since there is no 'tty'
in the C++ standard language.
Can I do something like that instead:

int detect(std::istream &is)
{
if( &is == &cout ) return fileno(stdout);
...
}
And what would it buy you? In Windows (as in Unix, most likely)
standard output can be redirected to anything.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 27 '08 #2
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
Mar 27 '08 #3
On Mar 27, 1:21 pm, Joe Greer <jgr...@doubletake.comwrote:
(I'm still amazed that they go through all this terminal
emulation stuff. I haven't seen an actual terminal connected
to a *nix box in ages, yet they still emulate a vt100 or one
of its kin. Go figure, :) )
It's to be expected, really. The terminal windows (e.g xterm)
emulate a VT-100 because that's the way the existing software
addresses the terminal. And new software continues to address
the terminal like that because that's what the terminal window
supports.
With Windows, it's a bit more straight forward once you
determine that you are indeed attached to a console, however
that can be a trick.
I'm not sure, but aren't there system calls to change the code
page? Which would fail if you aren't in a terminal window.
(Unix has a standard function isatty(), but the way I've always
seen it implemented is to do a ioctl with some terminal specific
operation.)

--
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
Mar 27 '08 #4
James Kanze <ja*********@gmail.comwrote in news:9012ef97-63a4-4e52-bdc5-
ea**********@s37g2000prg.googlegroups.com:
On Mar 27, 1:21 pm, Joe Greer <jgr...@doubletake.comwrote:
>(I'm still amazed that they go through all this terminal
emulation stuff. I haven't seen an actual terminal connected
to a *nix box in ages, yet they still emulate a vt100 or one
of its kin. Go figure, :) )

It's to be expected, really. The terminal windows (e.g xterm)
emulate a VT-100 because that's the way the existing software
addresses the terminal. And new software continues to address
the terminal like that because that's what the terminal window
supports.
Yes, I understand that, however, you would think that an X way would have
surfaced that didn't rely on serial terminal emulation. I mean with
standalone GUI editors and even GUI command prompts it would be possible to
do quite a bit more than a terminal would allow.
>
>With Windows, it's a bit more straight forward once you
determine that you are indeed attached to a console, however
that can be a trick.

I'm not sure, but aren't there system calls to change the code
page? Which would fail if you aren't in a terminal window.
(Unix has a standard function isatty(), but the way I've always
seen it implemented is to do a ioctl with some terminal specific
operation.)
Not sure. Since I have never wanted to do this sort of thing on Windows,
so I haven't looked too closely. I do know that consoles don't necessarily
have to be visible and that services, for example, have consoles but things
written to them disappear into the void.

joe
Mar 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: chuck | last post by:
I have found that sys.stdin.fileno() and sys.stdout.fileno() always return -1 when executed from within a win32 service written using the win32 extensions for Python. Anyone have experience with...
1
by: HOWARD GOLDEN | last post by:
The standard documentation for isatty() says: "Return True if the file is connected to a tty(-like) device, else False. Note: If a file-like object is not associated with a real file, this...
1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
5
by: andrewcw | last post by:
I have an object to serialize. TextWriter writer = new StreamWriter("test.xml"); serializer.Serialize(writer,obj); writer.Close(); but below does not, why ?? I have a file that I will have...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
4
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose...
4
by: fastback66 | last post by:
Evidently per the C++ standard, it is not possible to measure the distance between two stream iterators, because two non-end-of-stream iterators are equal when they are constructed from the same...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.