Finish converting struct variable to an opaque type. In this
[pspp-builds.git] / src / ui / gui / helper.c
1 #include <config.h>
2
3 #include "helper.h"
4 #include <data/data-in.h>
5 #include <data/data-out.h>
6 #include <libpspp/message.h>
7
8 #include <libpspp/i18n.h>
9
10 #include <ctype.h>
11 #include <string.h>
12 #include <data/settings.h>
13
14 /* Formats a value according to FORMAT 
15    The returned string must be freed when no longer required */
16 gchar *
17 value_to_text(union value v, struct fmt_spec format)
18 {
19   gchar *s = 0;
20
21   s = g_new(gchar, format.w + 1);
22   data_out(&v, &format, s);
23   s[format.w]='\0';
24   g_strchug(s);
25
26   return s;
27 }
28
29
30
31 gboolean 
32 text_to_value(const gchar *text, union value *v, 
33               struct fmt_spec format)
34 {
35   bool ok;
36
37   if ( format.type != FMT_A) 
38     {
39       if ( ! text ) return FALSE;
40
41       {
42         const gchar *s = text;
43         while(*s) 
44           {
45             if ( !isspace(*s))
46               break;
47             s++;
48           }
49  
50         if ( !*s) return FALSE;
51       }
52     }
53
54   msg_disable ();
55   ok = data_in (ss_cstr (text), format.type, 0, 0,
56                 v, fmt_var_width (&format));
57   msg_enable ();
58
59   return ok;
60 }
61
62
63 GtkWidget *
64 get_widget_assert(GladeXML *xml, const gchar *name)
65 {
66   GtkWidget *w;
67   g_assert(xml);
68   g_assert(name);
69   
70   w = glade_xml_get_widget(xml, name);
71
72   if ( !w ) 
73     g_warning("Widget \"%s\" could not be found\n", name);
74
75   return w;
76 }
77
78 /* Converts a string in the pspp locale to utf-8 */
79 char *
80 pspp_locale_to_utf8(const gchar *text, gssize len, GError **err)
81 {
82   return recode_string(CONV_PSPP_TO_UTF8, text, len);
83 }
84