12.9.2 Creating an IO stream
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Foreign Language Interface
        • Foreign access to Prolog IO streams
          • Creating an IO stream
            • Snew()
            • Sopen_pipe()
            • Sopenmem()
            • Sfree()
            • PL_unify_stream()
    • Packages
Availability:C-language interface function
IOSTREAM* Sopenmem(char **buffer, size_t *sizep, const char *mode)
Open a memory area as a stream. Output streams are automatically resized using realloc() if *size = 0 or the stream is opened with mode "wa". If the buffer is allocated or enlarged, this is achieved using malloc() or realloc(). In this case the returned buffer should be freed by the caller when done. Example:
    { char buf[1024];             // don't allocate for small stuff
      char *s = buf;
      IOSTREAM *fd;
      size_t size = sizeof(buf);

      fd = Sopenmem(&s, &size, "w");
      ...
      Sclose(fd);
      ...
      if ( s != buf )             // apparently moved
        Sfree(s);
    }

The mode is "r" or "w". The mode "rF" calls PL_free(buffer) when closed.

Note: Its is not allowed to access streams created with this call from multiple threads. This is ok for all usage inside Prolog itself. This call is intended to use Sfprintf() and other output functions to create strings.