[comp.windows.news] stringtype => dicttype

walker@prlhp1.prl.philips.co.uk (walker) (04/04/89)

  Hi, i'm writing a cps apllication and i need to pass a dicttype
from c into ps. Cps allows me to pass strings, but i can find no way
of converting this string into a dicttype. Does anyone have a solution
or workaround?

  david walker 

walker@prl.philips.co.uk

davidw@prlhp1.UUCP (David Walker) (04/06/89)

  In a cps application it does not see, possible to pass a dicttype
from the c code to postscript. You can only pass strings or ints and
there is no postscript command to convert a string into a dict name.
Does anyone know how to do this, if it is possible. 

  david walker
davidw@prl.philips.co.uk

chris@goedel.uucp (Chris White) (04/06/89)

In article <3230.8904051131@prsun5f.prl.philips.co.uk> davidw@prlhp1.UUCP (David Walker) writes:
>
>  In a cps application it does not see, possible to pass a dicttype
>from the c code to postscript. You can only pass strings or ints and
>there is no postscript command to convert a string into a dict name.
>Does anyone know how to do this, if it is possible.
>
>  david walker
>davidw@prl.philips.co.uk


Assuming you have passed the following into NeWS from C:

(10 dict begin /somekey (Value) def end)

you can run it by using the:

        (stringval) cvx exec

operator combination. I'm pretty sure this works, but I can't test
it now -- I'm at home on a Mac without any PostScript references. If that's
not the exact command, it's something similiar though.

                        -- Chris White

ps. I tried to send mail, but the address didn't work. Always the way
when I try to reply. Hey, anyone feel like using that wy program to make
a good NeWS interface for rn -- that'd be really cool. It could have a
scrollable list of topics (a la '=') and bring up an article window
when the topic is selected. You could have multiple article windows
so that you could compare and contrast. Okay, maybe I'm getting carried
away...but I'd try it if I didn't have other pet projects to do.

cjc@ulysses.homer.nj.att.com (Chris Calabrese[mav]) (04/07/89)

In article <3230.8904051131@prsun5f.prl.philips.co.uk>, davidw@prlhp1.UUCP (David Walker) writes:
> 
>   In a cps application it does not see, possible to pass a dicttype
> from the c code to postscript. You can only pass strings or ints and
> there is no postscript command to convert a string into a dict name.
> Does anyone know how to do this, if it is possible. 

Well, I already answered the original posting to this thread by email,
but I guess it's a problem of general interest.

While it is true that you can not pass types other than string and int,
it is possible to pass raw postscript to be executed on the server.


The first is to use pprintf(), which is how CPS sends information to
the server (the cps routines become preprocessor macros which call
pprintf()).

My favorite method, however, is to make use of some postscript operators
to change a string into an executable array and execute it.  I do something
like this:

in foo.cps:
cps raw_postscript(string mystring)
	mystring cvx exec

in foo.c:
int array[SIZE];
int maxarray;
int i;
char buffer[BufferSize];

raw_postscript("/foo [ ");
for(i=0; i<maxarray; i++)
	{
	sprintf(buffer, "%d ", array[i];
	raw_postscript(buffer);
raw_postscript("] def\n");


This leads to one of my favorite examples of doing this, which is a method
for reading postscript files on remote machines (BTW, don't flame
if this doesn't work as coded, since I pulled out of some other code.
The general idea is all there, however):

in foo.cps:
	...
	/read_file filename -> -
		{
		READ_TAG tagprint
		typedprint
		uniquecid dup typedprint
		[exch cidinterest] forkeventmgr
		waitprocess pop
		} def

cdef read_file(string data, int id) => READ_TAG (data, id)
cdef end_cid(int id) id {exit} sendcidevent
cdef cid_exec(string data, int id) id {data cvx exec} sendcidevent

in foo.c:
	...
	else if (read_file(data, &intdata))
		{
		if(debug)
			fprintf(stderr, "read request %s %d\n",
			data, intdata);
		do_read(data, intdata);
		end_cid(intdata);
		}
	...
/*
 * Read a file and send it to the postscript server
 * as if it had been done with '{ <name> run } stopped'
 */
do_read(name, id)
char *name;
int id;
{
	FILE    *fileptr;
	char data[STRING_SIZE];
	/* start the block to execute */
	cid_exec("{", id);
	/* open the file */
	if((fileptr = fopen(name, "r")) == NULL)
		{
		cid_exec("} stopped pop\n", id);
		return;
		}
	/* go through the file and dump it */
	while(fgets(data, STRING_SIZE, fileptr) != NULL)
		{
		cid_exec(data, id);
		}
	/* now we're done! */
	cid_exec(" } stopped pop\n", id);
	}
-- 
Name:			Christopher J. Calabrese
Brain loaned to:	AT&T Bell Laboratories, Murray Hill, NJ
att!ulysses!cjc		cjc@ulysses.att.com
Obligatory Quote:	``Now, where DID I put that bagel?''