Page 1 of 1

textclip.library

Posted: Mon Dec 22, 2014 9:27 am
by JosDuchIt
textclip.library/DisposeClipVector
textclip.library/DisposeClipVector textclip.library/DisposeClipVector
ReadClipVector -- read a string vector from the clipboard device
SYNOPSIS
ReadClipVector( vecptr, sizeptr )
LONG ReadClipVector( STRPTR *, ULONG *);
RESULT
A boolean success/failure indicator. In case of failure, the
size and the pointer will be nulled out for you.
I guess the result (LONG) is the vector's dimension? & not as described a boolean?

Re: textclip.library

Posted: Mon Dec 22, 2014 9:46 am
by salass00
JosDuchIt wrote:
textclip.library/DisposeClipVector
textclip.library/DisposeClipVector textclip.library/DisposeClipVector
ReadClipVector -- read a string vector from the clipboard device
SYNOPSIS
ReadClipVector( vecptr, sizeptr )
LONG ReadClipVector( STRPTR *, ULONG *);
RESULT
A boolean success/failure indicator. In case of failure, the
size and the pointer will be nulled out for you.
I guess the result (LONG) is the vector's dimension? & not as described a boolean?
No, the result is just a boolean with non-zero for success and zero for failure. The size is written to the ULONG variable, the pointer of which you have provided as the second parameter to ReadClipVector().

Re: textclip.library

Posted: Mon Dec 22, 2014 5:44 pm
by JosDuchIt
I am stuck with using textclip.library to make a list from a multiline copy to clipboard fpr ListBrowser
(using clips:o as input file with LyleHaze's code , works ok, just a FORM line at the topMy wndow opens if nothing is in the clipboard too.

edit: after replacing the erroneous while check, the program freezes now after entering the loop with a muli line clip
and does not enter any more when the clip is empty
struct List *make_listbrowser_clip (BOOL yes )
{
STRPTR clipstring[TOTAL_LINES]; // =
STRPTR * clipstrings;
clipstrings = &clipstring[0];

ULONG* clipsizes;
ULONG clipsize[TOTAL_LINES];
clipsizes = &clipsize[0];

struct List *list=NULL;
LONG len, i = 0;
Printf("clip start %ld\n",i); ///seen
ReadClipVector(clipstrings, clipsizes);///ITextClip->
Printf("clip start %s\n",clipstring); /// seen with all the lines
if ((list = AllocVecTags (sizeof(struct List), AVT_ClearWithValue, 0, TAG_END)))
{
NewList (list);
while (clipstring[0] != '\0' ) ; /// no problem when nothing in, clipboard
{
Printf("clip loop %s\n",clipstring);
struct Node *node;
if (node = AllocListBrowserNode (1,LBNCA_Text,clipstring,TAG_END))
{
Printf("clip loop Allocdone %s\n",clipstring);
AddTail (list,node);
}
else
{
free_listbrowser_list (list);
return (NULL);
}
Printf("clip loop i %ld\n",i);
i++;
}
if (!(list->lh_Head->ln_Succ))
{
free_listbrowser_list (list);
return (NULL);
}
}
return (list);
}


Re: textclip.library

Posted: Mon Dec 22, 2014 6:41 pm
by salass00
Your loop doesn't work because in your arrays only the first element is initialised by ReadClipVector() so the contents of all others is undefined.

If I understand you correctly then a function like this should do what you want:

Code: Select all

struct List *ClipToListBrowserList(void) {
    STRPTR clipstring;
    uint32 clipsize;
    struct List *list;
    STRPTR line, state;
    struct Node *node;

    if (!ReadClipVector(&clipstring, &clipsize))
        return NULL;

    list = AllocSysObject(ASOT_LIST, NULL);
    if (list == NULL) {
        DisposeClipVector(clipstring);
        return NULL;
    }

    line = strtok_r(clipstring, "\n", &state);
    while (line != NULL) {
        node = AllocListBrowserNode(1,
            LBNCA_CopyText, TRUE,
            LBNCA_Text, line,
            TAG_END);
        if (node == NULL) {
            DisposeClipVector(clipstring);
            FreeListBrowserList(list);
            FreeSysObject(ASOT_LIST, list);
            return NULL;
        }

        AddTail(list, node);

        line = strtok_r(NULL, "\n", &state);
    }

    DisposeClipVector(clipstring);

    if (IsListEmpty(list)) {
        FreeSysObject(ASOT_LIST, list);
        return NULL;
    }

    return list;
}
Note that in this code I have changed the list so that it's allocated with AllocSysObject rather than AllocVecTags so this means that it has to be freed with FreeSysObject(ASOT_LIST, list) rather than FreeVec(list).

strtok_r() a standard clib function is used to split the string based on newline characters (you will need to include <string.h> for this).

Re: textclip.library

Posted: Mon Dec 22, 2014 7:51 pm
by JosDuchIt
@salass00

Thanks for the code.
I learned a number of things from it.

Do you agree however that using strtok_r() is a workaround that one wouldn't have to use?
ReadClipVector() delivers all the strings together(with their line feed )


Using strtok_r() eats empty lines too.

I can use LyleHaze's code (file to Listbrowser list) pointing a clips:0 which probably respects the empty lines,

Re: textclip.library

Posted: Mon Dec 22, 2014 9:47 pm
by salass00
JosDuchIt wrote: Do you agree however that using strtok_r() is a workaround that one wouldn't have to use?
ReadClipVector() delivers all the strings together(with their line feed )
No, not really.

The current form is much more general and useful if you want to insert the text into a texteditor.gadget for example and if you need each line as a separate NUL-terminated string as you do it's not very hard to do the necessary conversion.
Using strtok_r() eats empty lines too.
I rarely ever find a use for this function so I don't remember its quirks but if you need to keep the empty lines you should be able to use this replacement strtok_r() function:

Code: Select all

char *my_strtok_r(char *s, const char *sep, char **state) {
    char *tok;
    char c;

    if (s == NULL && (s = *state) == NULL)
        return NULL;

    tok = s;
    while ((c = *s) != '\0' && strchr(sep, c) == NULL) s++;

    if (c != '\0')
        *s++ = '\0';
    else
        s = NULL;

    *state = s;

    return tok;
}

Re: textclip.library

Posted: Tue Dec 23, 2014 9:25 am
by JosDuchIt
@salas00
OK & Thanks for my_strtok_r