gui: Move some formatting functions into PSPP core.
[pspp-builds.git] / src / ui / gui / var-display.c
1 #include <config.h>
2 #include "var-display.h"
3
4 #include <data/variable.h>
5 #include <data/format.h>
6 #include <stdlib.h>
7 #include "psppire-dict.h"
8
9 #include <gettext.h>
10 #define _(msgid) gettext (msgid)
11 #define N_(msgid) msgid
12
13 #include "helper.h"
14 #include <libpspp/i18n.h>
15
16 static const gchar none[] = N_("None");
17
18
19 gchar *
20 missing_values_to_string (const PsppireDict *dict, const struct variable *pv, GError **err)
21 {
22   gchar *s;
23   const struct missing_values *miss = var_get_missing_values (pv);
24   if ( mv_is_empty (miss))
25     return xstrdup (gettext (none));
26   else
27     {
28       if ( ! mv_has_range (miss))
29         {
30           GString *gstr = g_string_sized_new (10);
31           const int n = mv_n_values (miss);
32           gchar *mv[4] = {0,0,0,0};
33           gint i;
34           for (i = 0 ; i < n; ++i )
35             {
36               mv[i] = value_to_text (*mv_get_value (miss, i), pv);
37               if ( i > 0 )
38                 g_string_append (gstr, ", ");
39               g_string_append (gstr, mv[i]);
40               g_free (mv[i]);
41             }
42           s = gstr->str;
43           g_string_free (gstr, FALSE);
44         }
45       else
46         {
47           GString *gstr = g_string_sized_new (10);
48           gchar *l, *h;
49           union value low, high;
50           mv_get_range (miss, &low.f, &high.f);
51
52           l = value_to_text (low, pv);
53           h = value_to_text (high, pv);
54
55           g_string_printf (gstr, "%s - %s", l, h);
56           g_free (l);
57           g_free (h);
58
59           if ( mv_has_value (miss))
60             {
61               gchar *ss = NULL;
62
63               ss = value_to_text (*mv_get_value (miss, 0), pv);
64
65               g_string_append (gstr, ", ");
66               g_string_append (gstr, ss);
67               free (ss);
68             }
69           s = gstr->str;
70           g_string_free (gstr, FALSE);
71         }
72
73       return s;
74     }
75 }