textclip.library

A forum for general AmigaOS 4.x support questions that are not platform-specific
Post Reply
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 6:47 pm
Contact:

textclip.library

Post 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?
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 534
Joined: Sat Jun 18, 2011 4:12 pm
Location: Finland
Contact:

Re: textclip.library

Post 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().
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 6:47 pm
Contact:

Re: textclip.library

Post 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);
}

User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 534
Joined: Sat Jun 18, 2011 4:12 pm
Location: Finland
Contact:

Re: textclip.library

Post 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).
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 6:47 pm
Contact:

Re: textclip.library

Post 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,
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 534
Joined: Sat Jun 18, 2011 4:12 pm
Location: Finland
Contact:

Re: textclip.library

Post 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;
}
JosDuchIt
Posts: 291
Joined: Sun Jun 26, 2011 6:47 pm
Contact:

Re: textclip.library

Post by JosDuchIt »

@salas00
OK & Thanks for my_strtok_r
Post Reply