Fixed some memory leaks in GUI.
[pspp-builds.git] / src / ui / gui / helper.c
1
2 #include "helper.h"
3 #include <data/data-in.h>
4 #include <libpspp/message.h>
5
6 #include <ctype.h>
7 #include <string.h>
8
9 /* Formats a value according to FORMAT 
10    The returned string must be freed when no longer required */
11 gchar *
12 value_to_text(union value v, struct fmt_spec format)
13 {
14   gchar *s = 0;
15
16   s = g_new(gchar, format.w + 1);
17   if ( ! data_out(s, &format, &v) ) 
18     {
19       g_warning("Can't format missing discrete value \n");
20     }
21   s[format.w]='\0';
22   g_strchug(s);
23
24   return s;
25 }
26
27
28
29 gboolean 
30 text_to_value(const gchar *text, union value *v, 
31               struct fmt_spec format)
32 {
33   struct data_in di;
34
35   if ( format.type != FMT_A) 
36     {
37       if ( ! text ) return FALSE;
38
39       {
40         const gchar *s = text;
41         while(*s) 
42           {
43             if ( !isspace(*s))
44               break;
45             s++;
46           }
47  
48         if ( !*s) return FALSE;
49       }
50     }
51
52   di.s = text;
53   di.e = text + strlen(text);
54   di.v = v;
55   di.flags = DI_IGNORE_ERROR;
56   di.f1 = di.f2 = 0;
57   di.format = format;
58   
59   return data_in(&di);
60
61 }
62
63
64 GtkWidget *
65 get_widget_assert(GladeXML *xml, const gchar *name)
66 {
67   GtkWidget *w;
68   g_assert(xml);
69   g_assert(name);
70   
71   w = glade_xml_get_widget(xml, name);
72
73   if ( !w ) 
74     g_warning("Widget \"%s\" could not be found\n", name);
75
76   return w;
77 }
78
79 /* Converts a string in the pspp locale to utf-8 */
80 char *
81 pspp_locale_to_utf8(const gchar *text, gssize len, GError **err)
82 {
83   GError *tmp_error = 0;
84
85   gchar *s;
86
87   if ( ! text ) 
88     return 0;
89
90   s = g_locale_to_utf8(text, len, 0, 0, &tmp_error);
91
92   if ( tmp_error)
93     {
94       g_warning("Error converting to UTF8: %s", tmp_error->message);
95       g_propagate_error (err, tmp_error);
96     }
97
98   return s;
99 }
100