[gtk-server] Re: Formatting floats (Was: gtk-server Digest V5 #3)

  • From: Peter van Eerten <administrator@xxxxxxxxxxxxxx>
  • To: gtk-server@xxxxxxxxxxxxx
  • Date: Fri, 05 Sep 2008 22:03:59 +0200

Hi Erik,

Looking further into this matter, I realized that 'gtk_server_opaque' always allocates a fixed amount of memory which is of GtkWidget size. On a 32bit system, this is limited to 60 bytes. But what if you need a larger memory buffer? Things would segfault very fast.

Therefore, it is better to use g_malloc to allocate memory, and use g_snprintf to limit the output.

So, IMHO the best way to go would be this, assuming 20 bytes of allocated memory:

peter[~]$ gtk-server -stdin
gtk_server_define g_snprintf NONE NONE 4 WIDGET INT STRING DOUBLE
ok
gtk_server_define g_malloc NONE WIDGET 1 INT
ok
g_malloc 20   <----------------------------------- reserve 20 bytes
135155200
g_snprintf 135155200 20 "%.2f" 1.23456 <-------- limit sprintf to 20 bytes also
ok
g_object_set renderer "text" 135155200 NULL
ok
g_free 135155200   <------------------------------ free the 20 bytes
ok


That should do the trick in a memory safe manner.

Greetings
Peter

Quoting Peter van Eerten <administrator@xxxxxxxxxxxxxx>:

Reading the GLib docs:

http://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-snprintf

The 'g_snprintf' function and friends are very similar to the default
libc functions, so why not give it a shot with GLIB? This is more
portable also. Then it would look like:

gtk_server_define g_sprintf NONE NONE 3 WIDGET STRING DOUBLE

or

gtk_server_define g_snprintf NONE NONE 4 WIDGET INT STRING DOUBLE


On my Linux system:

peter@host:~$ gtk-server -stdin
gtk_server_define g_sprintf NONE NONE 3 WIDGET STRING DOUBLE
ok
gtk_server_opaque
7301168
g_sprintf 7301168 "%.2f" 1.234
ok
gtk_server_define g_printf NONE NONE 2 STRING WIDGET
ok
g_printf "%s\n" 7301168
1.23
ok

Please let me know if it works!

Regards
Peter

Quoting Erik Winkels <euqirea@xxxxxxxxx>:

Date: Fri, 05 Sep 2008 16:41:49 +0200
From: Peter van Eerten <administrator@xxxxxxxxxxxxxx>
Subject: [gtk-server] Re: List Activity

I have the same issue on my 64bit system. Can you please send me the output of the following:

echo "gtk_server_ffi" | gtk-server -stdin

FFI

echo "gtk_server_version" | gtk-server -stdin

2.2 build 7

uname -a

Windows XP Professional SP2 ;-)

I'm also using CLISP 2.45.

While I'm a Unix man by heart I'm actually targetting the application I'm writing at Windows (it will be a utility for a game I'm playing[1]) since the target audience is mainly using that platform.

I should have been more clear about that, sorry :-( Would it still be worth it to try the sprintf / printf solution you posted on the Windows platform?


Erik



--
http://www.gtk-server.org

--
http://www.gtk-server.org


Other related posts: