Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get the result of "puts command" in C/C++

70 views
Skip to first unread message

sam

unread,
Jan 11, 2010, 10:54:53 PM1/11/10
to
hi...
i already know redirect "puts command" channel to a "text.txt"(Using
Tcl_OpenFileChannel) in C/C++,
but, i hope the result of "puts command" could be save as char* or
Cstring directly in C/C++,
i don't know which tcl library function is the right choice...
or are there some ways to reach this purpose?
thanks

Andreas Leitgeb

unread,
Jan 12, 2010, 5:21:55 AM1/12/10
to
sam <sam...@gmail.com> wrote:
> i hope the result of "puts command" could be save as char* or
> Cstring directly in C/C++,

Then why do you "puts" it at all?

Probably I misunderstood your problem.

MartinLemburg@Siemens-PLM

unread,
Jan 12, 2010, 6:18:14 AM1/12/10
to
Hi Sam,

first some questions:

1. do you want to produce tcl channel output in C(++), identical to
the tcl usage of "puts ..."?
In other words: do you want to save an ASCII file from C(++) using the
tcl file IO mechanisms?

2. do you want to capture the tcl channel output in C(++) to be reused
e.g. for logging in C++?

Some answers:

1.a) Use Tcl_OpenFileChannel to open a channel, configure it via
Tcl_SetChannelOption, to write to it via Tcl_Write(Chars|Obj)?,
Tcl_Flush, etc. and close it with Tcl_Close

1.b) To write to a tcl standard channel (stdout, stderr) use
Tcl_GetStdChannel to get the channel and write to it like in 1.a, but
without closing it.

2.a) To capture a tcl standard channel, you can replace a tcl standard
channel via Tcl_SetStdChannel using a tcl channel created by
Tcl_CreateChannel or Tcl_(Open|Make)FileChannel,
Tcl_OpenCommandChannel, Tcl_(Open|Make)TcpClient, Tcl_OpenTcpServer.
After replacing a tcl standard channel (stdout, stderr) every writing
to this channel in tcl will cause "reactions" in the C++ sided tcl
channel. So you are able to capture the strings pushed into "puts std
(out|err) ..." in C(++)

2.b) To capture a tcl standard channel without redirecting it is a bit
easier, because you can register a channel handler via
Tcl_CreateChannelHandler to react on the tcl standard channel actions.
You only have to use Tcl_GetStdChannel to retrieve the tcl channel for
the appropriate tcl standard channel and to register your own channel
handler, capturing the arriving data.

2.c) To capture a non-standard tcl channel is a bit trickier. You have
to stack the channel via Tcl_StackChannel, so that you put your IO
handler for reading/writing data through this channel onto the top.
Then via Tcl_CreateChannelHandler you register your IO handler for the
new stacked channel.
So if in tcl via "puts $channel ..." data is pushed into the channel,
the "new" implemented stack onto the original channel causes the
registered IO handler to be executed, so that you may capture or
manipulate the data pushed from tcl into the original channel.
The only additional thing you have to do is, to create a new tcl
command from C(++) like e.g. "captureChannel channelName ?readable|
writable?" via Tcl_Create(Obj)?Command to be called for each channel
to be captured from C(++).
This "captureChannel" C(++) tcl command has to do the things mentioned
above (Tcl_StackChannel, Tcl_CreateChannelHandler, etc.).
The same scheme can be used to capture/manipulate the tcl standard
channels in addition to 2.b, so that stacking the needed tcl standard
channel and registering a IO handler for this stacked standard channel
(perhaps without the need for "captureChannel") allows to capture or
manipulate arriving data, too.

WARNING: If you are working inside a virtual file system, than you
have to take a look at the Tcl_FS\w+ library functions, too!

I hope a hit more or less your point.

Good luck and much fun!

Martin

MartinLemburg@Siemens-PLM

unread,
Jan 13, 2010, 4:54:54 AM1/13/10
to
Sorry to say, but there are errors in my explainations ... like
talking nonsense about using "Tcl_CreateChannelHandler".

I hope the descriptions/explainations in ...

http://groups.google.de/group/comp.lang.tcl/browse_frm/thread/d58bd30b84025c05?hl=de#

... are "more" correct!

Best regards,

Martin Lemburg

On 12 Jan., 12:18, "MartinLemburg@Siemens-PLM"

> > thanks- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

0 new messages