Internationalisation.
[pspp-builds.git] / src / ui / gui / helper.c
1 #include "helper.h"
2 #include <data/data-in.h>
3 #include <libpspp/message.h>
4
5 #include <libpspp/i18n.h>
6
7 #include <ctype.h>
8 #include <string.h>
9 #include <data/settings.h>
10
11 /* Formats a value according to FORMAT 
12    The returned string must be freed when no longer required */
13 gchar *
14 value_to_text(union value v, struct fmt_spec format)
15 {
16   gchar *s = 0;
17
18   s = g_new(gchar, format.w + 1);
19   if ( ! data_out(s, &format, &v) ) 
20     {
21       g_warning("Can't format missing discrete value \n");
22     }
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   struct data_in di;
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   di.s = text;
55   di.e = text + strlen(text);
56   di.v = v;
57   di.flags = DI_IGNORE_ERROR;
58   di.f1 = di.f2 = 0;
59   di.format = format;
60   
61   return data_in(&di);
62
63 }
64
65
66 GtkWidget *
67 get_widget_assert(GladeXML *xml, const gchar *name)
68 {
69   GtkWidget *w;
70   g_assert(xml);
71   g_assert(name);
72   
73   w = glade_xml_get_widget(xml, name);
74
75   if ( !w ) 
76     g_warning("Widget \"%s\" could not be found\n", name);
77
78   return w;
79 }
80
81 /* Converts a string in the pspp locale to utf-8 */
82 char *
83 pspp_locale_to_utf8(const gchar *text, gssize len, GError **err)
84 {
85   return recode_string(CONV_PSPP_TO_UTF8, text, len);
86 }
87