> < ^ Date: Mon, 19 Feb 2001 20:04:25 +1000 (EST)
> < ^ From: Greg Gamble <greg.gamble@math.rwth-aachen.de >
< ^ Subject: Re: New features concernig Input-Ouput Streams

Dear GAP-Forum and Jan,

According to Jan De Beule:
> Dear GAP-Forum,
>
> At first glance, the new features concernig Input-Output Streams seems
> very interesting. Although trying the examples in the documentation I
> encounter some problems.

You made a few mistakes, both in the C program and in how you used
the Input-Output streams within GAP 4.2.

I wrote a very simple and small C-program that
reverses strings as in the example:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

int main(void) {
  int size = 0;
  char* s = (char*) malloc(sizeof(char));
  char c;
  while((c=getchar()) != '\n') {
    s = (char*) realloc(s,(++size)*sizeof(char));
    s[size-1] = c;
  }
  s[size] = '\0';
  while(size--)
    printf("%c",s[size]);
  printf("%c",'\n');
  return 0;
}
The binary is in the directory ~/prog/ en works fine. It uses of course
stdin and stdout.

Yes this works, but just once and exits ... and so the stream dies. You need
to try something like:

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

int main(void) {
  int size = 0;
  char* s = (char*) malloc(sizeof(char));
  char c;
  while((c=getchar())!=EOF) {
    if(c != '\n') {
      s = (char*) realloc(s,(++size)*sizeof(char));
      s[size-1] = c;
    } else {
      while(--size)
        putchar(s[size]);
      putchar('\n');
      size = 0;
    }
  }
  return 0;
}

The program needs to hang around so that GAP can both write to it and read
from it.

So I execute the following commands in GAP, with the
following output:

GAP4, Version: 4r2 fix6 of 26-January-2001, i686-pc-linux-gnu-gcc
Components:  small, small2, small3, small4, small5, small6, small7,
             small8, id2, id3, id4, id5, id6, trans, prim, tbl,
             tom  installed.
gap> d := Directory("./prog/");
dir("./prog/")
gap> f := Filename(d,"rev");
"./prog/rev"
gap> s := InputOutputLocalProcess(d,f,[]);

Here we have your next mistake. The first argument of InputOutputLocalProcess
should be the *current* directory (check the docs) ... this is given by
DirectoryCurrent(). So, the last line should be replaced with:

gap> s := InputOutputLocalProcess(DirectoryCurrent(),f,[]);

(Note, when you used Directory above, you were using an undocumented function
... as it turns out, it does what you expected. But be careful! Probably,
Directory should be documented and may well be in the next revision of GAP.)

Finally, you do not need the \n in:

gap> WriteLine(s,"The cat sat on the mat\n");

(The Writeline function adds one.)
-----------------------------------------------------------------------------

Just to recap ... if the replacement program above is called rev.c and is
compiled with, say: gcc rev.c -o rev and rev is put in directory ./prog
then you should be able to reproduce the following with GAP 4.2 fix 6:

gap> d := Directory("./prog");
dir("./prog/")
gap> f := Filename(d,"rev");
"./prog/rev"
gap> s:=InputOutputLocalProcess(DirectoryCurrent(),f,[]);
< input/output stream to rev >
gap> WriteLine(s,"The cat sat on the mat");
true
gap> WriteLine(s,"The mouse ate the cheese");
true
gap> Print(ReadLine(s));
tam eht no tas tac ehT
gap> Print(ReadLine(s));
eseehc eht eta esuom ehT
gap> CloseStream(s);
gap>

Regards,
Greg Gamble

Miles-Receive-Header: reply


> < [top]