Feature #805

Enable memory-output from PSL as alternative to file output

Added by Paul over 1 year ago. Updated about 1 year ago.

Status:ClosedStart date:2015-11-05
Priority:NormalDue date:
Assignee:Paul% Done:


Target version:Candidate for next minor release


Users of the API may wish to receive the PostScript produced by GMT module directly into a memory buffer. This would require a few changes in PSL but open up the API to such use and possibly direct feeding of PostScript into gs, avoiding the "temporary" PostScript file altogether.

Here is a possible plan for this:

  1. Let PSL_beginsession accept a bit flag that if set directs PSL to write to a buffer, else normal output, i.e., set a new internal PSL parameter out_mode = 0 [file] or 1 [memory]. This bit flag would need to be via GMT_Create_Session.
  2. Add these 3 items to the PSL struct:
        char *output;    /* Pointer to memory with text-representation of current PS plot */
        size_t out_alloc;    /* How much allocated so far */
        size_t out_pos;    /* Where to write to next, i.e., output[out_pos];
  3. Since all writing to the PS file goes via PSL_command and PSL_comment, we could make some changes like this one:
    int PSL_command (struct PSL_CTRL *C, const char *format, ...) {
        va_list args;
        static char PSL_buffer[PSL_BUFSIZ];
        size_t len, total;
        va_start (args, format);
        if (PSL-> out_mode == PSL_FILE) {
            vfprintf (C->internal.fp, format, args);
            va_end (args);
            return (0);
        /* Write to memory instead */
        vsnprintf (PSL_buffer , PSL_BUFSIZ, format, args);
        va_end (args);
        len = strlen (PSL_buffer);
        total = PSL->out_pos + len;
        while (total > PSL->out_alloc) {
            PSL->out_alloc << 1;    /* Double the allocation until large enough */
            alloc = true;
        if (alloc) PSL->output = realloc (PSL->output, PSL->out_alloc);
        memcpy (&PSL->output[PSL-> out_pos], PSL_buffer, len);
        PSL-> out_pos = total;
        return (0);
  4. We would add one more PSL function to the library:
    size_t PSL_retrieveplot (struct *PSL, char **plot)
    {    /* Pass responsibility for plot to caller */
        size_t length = PSL->out_pos;
        *plot = PSL->output;
        PSL->out_pos = 0;
         PSL->output = NULL;
        return (length)

Just to make it clear that PSL passes the output and gets reset. Certainly, PSL_beginplot should wipe any slate:

    /* Reset counter to 0 but reuse any allocated space */
    PSL-> out_pos = 0;    PSL->output = NULL [maybe free if not NULL in case PSL_retrieveplot was not called]
PSL_endsession should free PSL->output if used.


Feature #810: Add GMT_PS as a 6th GMT resource for PostScript outputClosed


#1 Updated by Paul over 1 year ago

  • Status changed from New to Resolved

This has been implemented in the 5.3 branch. Documentation has been updated and new test program testpsl.c has been added to demonstrate usage.

#2 Updated by Paul about 1 year ago

  • Status changed from Resolved to Closed

Closing this issue.

Also available in: Atom PDF