Testing for Empty Fl_Text_Editor?

23 views
Skip to first unread message

Svets

unread,
Jul 9, 2016, 9:04:42 PM7/9/16
to fltk.general
I can't seem to make a check to see if my Text Editor is empty.


I first use this callback.  Here buf2 is the buffer for my Fl_Text_Editor ted2

void clear_callback(Fl_Widget* wid, void* data)
{
  buf2->text("");
 
}



Then I test it with this callback activated via a button press. 
I have also tried  if(ted2->buffer()->text() =="") which doesn't make any difference so I'm not sure how to test for an empty text editor and buffer.

void load_callback(Fl_Widget* wid, void* data)
{
    if(buf2->text()=="")
    {
        fl_alert("No Data");
    }

}


Both callbacks are called as they should be so I'm stumped why I can't test the emtpy buffer successfully?

Albrecht Schlosser

unread,
Jul 10, 2016, 7:55:15 AM7/10/16
to fltkg...@googlegroups.com
On 10.07.2016 03:04 Svets wrote:
> I have also tried if(ted2->buffer()->text() =="") which doesn't make
> any difference so I'm not sure how to test for an empty text editor and
> buffer.
>
> void load_callback(Fl_Widget* wid, void* data)
> {
> if(buf2->text()=="")
> {
> fl_alert("No Data");
> }
>
> }

You can't test two strings for identity with '=='. This comparison tests
if the two pointers to the strings are equal.

text() returns an allocated text which you'd need to free after use,
which is not useful here: if the buffer was not empty, the full text
would be copied internally, which is unnecessary overhead.

Testing for an empty buffer, however, is simple:

if (buf2->length() == 0) { ... }

does what you want.

<http://www.fltk.org/doc-1.3/classFl__Text__Buffer.html#a5afecd40b3dd915ca39a8543a16fa042>

Svets

unread,
Jul 10, 2016, 5:41:58 PM7/10/16
to fltk.general, Albrech...@online.de
Thanks.  That worked perfectly.

Edzard Egberts

unread,
Jul 11, 2016, 2:24:32 AM7/11/16
to 'ed' via fltk.general
Albrecht Schlosser wrote:
> On 10.07.2016 03:04 Svets wrote:
>> I have also tried if(ted2->buffer()->text() =="") which doesn't make
>> any difference so I'm not sure how to test for an empty text editor and
>> buffer.
>>
>> void load_callback(Fl_Widget* wid, void* data)
>> {
>> if(buf2->text()=="")
>> {
>> fl_alert("No Data");
>> }
>>
>> }
>
> You can't test two strings for identity with '=='. This comparison tests
> if the two pointers to the strings are equal.

But he can check a const char* for beeing empty:

if (buf2->text()== nullptr || *buf2->text()== 0)

Because most FLTK text widgets doesn't return a nullptr (NULL, 0) when
empty, it is sufficient to check for the delimiter at first position:

if (*buf2->text()== 0) // checks for empty const char*



Reply all
Reply to author
Forward
0 new messages