Alexander Gromnitsky
Alexander Gromnitsky

Reputation: 3029

How to get an exact size of a GtkTextBuffer in bytes?

At the present time I'm doing it like:

GtkTextBuffer *buf = gtk_text_view_get_buffer(...);
gtk_text_buffer_get_bounds(buf, &start, &end);
gchar *data = gtk_text_buffer_get_text(buf, &start, &end, true);
gint size = strlen(data); // ouch

But this is rather ugly. I found (and tested) gtk_text_iter_get_offset() but it returns the size in characters, not physical bytes.

Upvotes: 1

Views: 1183

Answers (3)

splugenbrau
splugenbrau

Reputation: 9

strlen() stops at the first space encountered. I suggest a modified version:

short int strlen_0(char *s)
{ short int i=0;
  if (s!=NULL)
     {while (*(s+i)!='\0') i++;
      return i;}
  else return -1;}

Upvotes: 0

unwind
unwind

Reputation: 399949

Since GTK+ stores all text in UTF-8 by definition, I think your solution to get a pointer to the characters and use a plain old strlen() is awesome.

UTF-8 guarantees that the byte with value 0 does not occur, so strlen() will perform the proper counting operation and return the length of the buffer in bytes. Plus, it's a classic C runtime function that is well-known and very probably as highly optimized as possible.

Upvotes: 2

ptomato
ptomato

Reputation: 57910

There's no corresponding gtk_text_buffer_get_byte_count() or gtk_text_iter_get_index() function, unfortunately. If you need an absolute upper bound on the number of bytes required to store the buffer text, you could take the value from gtk_text_buffer_get_char_count() and multiply it by 4, the maximum number of bytes required to encode one UTF-8 character. If it's allocating and deallocating a string holding the full text of the buffer you're worried about, you could do the following:

glong bytecount = 0;
GtkTextIter iter;
for(gtk_text_buffer_get_start_iter(buf, &iter); gtk_text_iter_forward_line(&iter); )
    bytecount += gtk_text_iter_get_bytes_in_line(&iter);

I don't claim that this isn't ugly.

Upvotes: 0

Related Questions