View Raw SPL
#include 

static cbvarname = "";

/* paste series/object from clipboard file to window - called internally */
_cbpastedata(vfile = "", vname = "", w = refwindow(w0))
{
        local s, cbf, cbv, title, local_s = "", pastewin, isseries;

        cbvarname = vname;

        /* return result if target is not a window */
        pastewin = iswindow(w);

        if (strlen(vfile) > 0 && strlen(vname) > 0)
        {
                if (fstat(vfile) > 0)
                {
                        if (pastewin)
                        {
                                printf(_cbrdstr_psing, getwnum(w));
                        }

                        /* load it */
                        loadvar(vfile);

                        /*
                         *  loadvar preserves storage info of the source file for
                         *  direct series reading so we need to make explicit
                         *  copies to avoid read locking the clipboard file
                         */

                        if (eval(sprintf("isglobal('%s')", vname)))
                        {
                                isseries = eval(sprintf("isarray(%s)", vname));

                                if (not(isseries) && pastewin)
                                {
                                        error(-1, 300);
                                }

                                if (pastewin)
                                {
                                        /* copies series, use getvar for macro name circumvention */
                                        eval(sprintf("w%d = copyobject(getvar('%s'))", getwnum(w), vname));

                                        /* label with dataset name or comment */
                                        title = getseriesname(w);

                                        if (not(isdsname(title)))
                                        {
                                                /* not a dataset, use comment if not default comment */
                                                title = getcomment(w);

                                                if (strlen(title) == 0 || title == getwksattribute("defaultcomment"))
                                                {
                                                        title = _cbrdstr_undat;
                                                }
                                        }

                                        label(w, title);

                                        printf("");
                                }
                                else
                                {
                                        /* explict copy - handles all types, use getvar for macro name circumvention */
                                        eval(sprintf("setlocal('local_s', copyobject(getvar('%s')))", vname));
                                }
                        }

                        /* delete clipboard variable */
                        eval(sprintf("clear('%s')", vname));
                }
        }

        cbvarname = "";

        if (not(pastewin))
        {
                return(local_s);
        }
}


/* error handler */
_cbpastedata_error(errnum, errmes)
{
        if (strlen(cbvarname) > 0)
        {
                eval(sprintf("clear('%s')", cbvarname));
                cbvarname = "";
        }

        error(errmes, -1, errnum);
}