[comp.sys.next] Connecting a Stream to a Text Object

martin@icsl.ucla.edu (Kenneth Martin/) (11/28/90)

On page 10-4 of the Concepts manual, it says "You can also implement the
functions needed to connect a stream to a different source or destination,
such as a Text object, thereby creating your own type of stream." Unfortunately,
two paragraphs later it says "The last section contains information about
how to connect a stream to a source or destination other than memory, a file,
or a Mach port.  <<This section isn't currently available.>>.  Does anyone
out there know how to connect a stream to a Text window for reading and
writing?  The Text window is a subwindow of a larger window.

My best guess at the present time seems to be:

1) Open two Mach ports, one for reading and one for writing.  (I'd greatly
prefer to have one stream for both directions.)
2) Create a thread that reads from the one port and sends it to the Text
window.
3) Create another thread that reads from the text window and sends it to
the thread.
4) Somehow get all these things happening concurrently without any timing
problems.

I admit that I am very ignorant of Mach, threads, and Mach ports, but even
so, this seems like an incredibly complicated and burdensome way to simply
read and write through a stream to a Text window.  Could anyone tell me if
I'm way off base, or should I plug ahead?  All comments are appreciated. Thanks.

-Ken Martin: martin@icsl.ucla.edu

wjs@milton.u.washington.edu (William Jon Shipley) (11/29/90)

Kenneth Martin writes:
>On page 10-4 of the Concepts manual, it says "You can also implement the
>functions needed to connect a stream to a different source or destination,
>such as a Text object, thereby creating your own type of stream."

Well, I'll take a guess at this...

First off, this is my guess at explaining how one might create one's
own NXStream.  It SOUNDS like you don't really want to do this, so skip
on to the end and I'll guess at what you really want to do (I think).

Creating your own kind of NXStream
==================================

Lets say you have a NXStream *myStream.  Then there will be a structure
myStream->functions.  This structure looks like:

struct stream_functions {
	int	(*read_bytes)(NXStream *s, void *buf, int count);
	int	(*write_bytes)(NXStream *s, const void *buf, int count);
	int	(*flush_buffer)(NXStream *s);
	int	(*fill_buffer)(NXStream *s);
	void	(*change_buffer)(NXStream *s);
	void	(*seek)(NXStream *s, long offset);
	void	(*destroy)(NXStream *s);
};


I'd guess that most of the work of defining a new type of stream would be
pointing those function pointers to some functions of your own devising.

(For example, when you have an NXStream that is connected to a file descriptor,
read_bytes probably just points to read(2) (with some packaging).)

So, if you wanted to hook up an NXStream to your Text object, you'd make
write_bytes point to a routine that adds a character to the object, read_bytes
point to a routine that gets a character, etc.  Of course, you don't
want to do this for the Text object, because it's already done for you...

Reading and Writing to a Text object with an NXStream
=====================================================

The Text object has "readText:" and "writeText:" methods which both take
a stream.  The first reads text from the stream into the Text object, the
second writes the Text object out onto the stream.

It sounds like this is what you want to do.  However, you may be saying
to yourself: "Well, what if I want to read text in that I generate myself
on the fly (that is, not from a file or from memory or a port)?"

Then you can either a) try to diddle with the above, b) wait for the 2.0
docs to arrive, or c) just write the text you generate into an NXStream opened
on a port opened into memory, and then read the memory NXStream into your
Text object using the "readText:" method. 

I use the c) method in my newsreader (when I'm grabbing articles off of NNTP).

Clear as mud?  Good.  Send mail to me if you have any questions, but realise
that I'm bluffing here.  (Hey, there aren't really any docs.)

-william shipley

absinthe@milton.u.washington.edu (Daniel/Absinthe/God) (11/29/90)

Look at the code for SetUpScrollView in [sorry I dont remember his name]'s
Executive object (I'd look it up but I'm not on a NeXT)

It uses a matrix installed in a Text object in a ScrollView to add rows
of text..a good example.

It's available for anon ftp at cs.orst
[Thanks again to the author]

[including file: /altdimension7/'heaven'/God/Christian/.signature...]
-----------------------------------------------------------------------------
Faithfully and unerringly saving our civilization from certain demise..
absinthe@milton.u.washington.edu     |    "Don't get Odd, get Even"
------------------------------------------------------------------------------
The opinions (and/or facts) expressed herein may represent most of the
student body and administratium here at the UW. But then again, they may not.