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