From: John Darrington Date: Sun, 5 Jul 2009 09:33:29 +0000 (+0800) Subject: Store variable names, labels and value labels as UTF8. X-Git-Tag: sav-api~248^2~117^2~25 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=16ca75fe99859fdec9f214a0fb9c1a3ef58c8442;p=pspp Store variable names, labels and value labels as UTF8. This change converts long variable names, variable labels and value labels to utf8 encoding when system files are loaded. It is therefore no longer necessary (nor correct) to convert them when displaying. --- diff --git a/src/data/sys-file-reader.c b/src/data/sys-file-reader.c index 67767a9f4a..c9a7097812 100644 --- a/src/data/sys-file-reader.c +++ b/src/data/sys-file-reader.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -186,6 +187,52 @@ static void read_long_string_value_labels (struct sfm_reader *, size_t size, size_t count, struct dictionary *); +/* Convert all the strings in DICT from the dict encoding to UTF8 */ +static void +recode_strings (struct dictionary *dict) +{ + int i; + + const char *enc = dict_get_encoding (dict); + if ( NULL == enc) + return; + + for (i = 0 ; i < dict_get_var_cnt (dict); ++i) + { + /* Convert the long variable name */ + struct variable *var = dict_get_var (dict, i); + char *utf8_name = recode_string (UTF8, enc, var_get_name (var), -1); + dict_rename_var (dict, var, utf8_name); + free (utf8_name); + + /* Convert the variable label */ + if (var_has_label (var)) + { + char *utf8_label = recode_string (UTF8, enc, var_get_label (var), -1); + var_set_label (var, utf8_label); + free (utf8_label); + } + + if (var_has_value_labels (var)) + { + const struct val_lab *vl = NULL; + const struct val_labs *vlabs = var_get_value_labels (var); + + for (vl = val_labs_first (vlabs); vl != NULL; vl = val_labs_next (vlabs, vl)) + { + const union value *val = val_lab_get_value (vl); + const char *label = val_lab_get_label (vl); + char *new_label = NULL; + + new_label = recode_string (UTF8, enc, label, -1); + + var_replace_value_label (var, val, new_label); + free (new_label); + } + } + } +} + /* Opens the system file designated by file handle FH for reading. Reads the system file's dictionary into *DICT. If INFO is non-null, then it receives additional info about the @@ -303,6 +350,8 @@ sfm_open_reader (struct file_handle *fh, struct dictionary **dict, r->has_long_var_names = true; } + recode_strings (*dict); + /* Read record 999 data, which is just filler. */ read_int (r); diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index 7fd7580e89..60189ff76e 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -57,6 +57,7 @@ create_iconv (const char* tocode, const char* fromcode) size_t hash; struct hmapx_node *node; struct converter *converter; + assert (fromcode); hash = hash_string (tocode, hash_string (fromcode, 0)); HMAPX_FOR_EACH_WITH_HASH (converter, node, hash, &map) diff --git a/src/ui/gui/psppire-data-editor.c b/src/ui/gui/psppire-data-editor.c index 212259f8c8..e02cf730c7 100644 --- a/src/ui/gui/psppire-data-editor.c +++ b/src/ui/gui/psppire-data-editor.c @@ -744,15 +744,10 @@ update_data_ref_entry (const PsppireSheet *sheet, gchar *text = g_strdup_printf ("%d: %s", row + FIRST_CASE_NUMBER, var_get_name (var)); - gchar *s = recode_string (UTF8, - psppire_dict_encoding (data_store->dict), - text, -1); - g_free (text); - - gtk_entry_set_text (GTK_ENTRY (de->cell_ref_entry), s); + gtk_entry_set_text (GTK_ENTRY (de->cell_ref_entry), text); - g_free (s); + g_free (text); } else goto blank_entry; diff --git a/src/ui/gui/psppire-data-store.c b/src/ui/gui/psppire-data-store.c index b67e27c139..0e378bbec1 100644 --- a/src/ui/gui/psppire-data-store.c +++ b/src/ui/gui/psppire-data-store.c @@ -795,7 +795,6 @@ get_row_sensitivity (const PsppireSheetModel *model, gint unit) static gchar * get_column_subtitle (const PsppireSheetModel *model, gint col) { - gchar *text; const struct variable *v ; PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); @@ -807,16 +806,12 @@ get_column_subtitle (const PsppireSheetModel *model, gint col) if ( ! var_has_label (v)) return NULL; - text = recode_string (UTF8, psppire_dict_encoding (ds->dict), - var_get_label (v), -1); - - return text; + return xstrdup (var_get_label (v)); } static gchar * get_column_button_label (const PsppireSheetModel *model, gint col) { - gchar *text; struct variable *pv ; PsppireDataStore *ds = PSPPIRE_DATA_STORE (model); @@ -825,10 +820,7 @@ get_column_button_label (const PsppireSheetModel *model, gint col) pv = psppire_dict_get_variable (ds->dict, col); - text = recode_string (UTF8, psppire_dict_encoding (ds->dict), - var_get_name (pv), -1); - - return text; + return xstrdup (var_get_name (pv)); } static gboolean diff --git a/src/ui/gui/psppire-var-store.c b/src/ui/gui/psppire-var-store.c index aadc585548..66da770643 100644 --- a/src/ui/gui/psppire-var-store.c +++ b/src/ui/gui/psppire-var-store.c @@ -435,7 +435,7 @@ psppire_var_store_clear (PsppireSheetModel *model, glong row, glong col) switch (col) { case PSPPIRE_VAR_STORE_COL_LABEL: - var_set_label (pv, 0); + var_set_label (pv, NULL); return TRUE; break; } @@ -468,13 +468,7 @@ psppire_var_store_set_string (PsppireSheetModel *model, case PSPPIRE_VAR_STORE_COL_NAME: { gboolean ok; - char *s = recode_string (psppire_dict_encoding (var_store->dict), - UTF8, - text, -1); - - ok = psppire_dict_rename_var (var_store->dict, pv, s); - - free (s); + ok = psppire_dict_rename_var (var_store->dict, pv, text); return ok; } case PSPPIRE_VAR_STORE_COL_COLUMNS: @@ -540,11 +534,7 @@ psppire_var_store_set_string (PsppireSheetModel *model, break; case PSPPIRE_VAR_STORE_COL_LABEL: { - gchar *s = recode_string (psppire_dict_encoding (var_store->dict), - UTF8, - text, -1); - var_set_label (pv, s); - free (s); + var_set_label (pv, text); return TRUE; } break; @@ -583,6 +573,7 @@ text_for_column (PsppireVarStore *vs, N_("Custom"), N_("String") }; + enum {VT_NUMERIC, VT_COMMA, VT_DOT, VT_SCIENTIFIC, VT_DATE, VT_DOLLAR, VT_CUSTOM, VT_STRING}; @@ -591,8 +582,7 @@ text_for_column (PsppireVarStore *vs, switch (c) { case PSPPIRE_VAR_STORE_COL_NAME: - return recode_string (UTF8, psppire_dict_encoding (dict), - var_get_name (pv), -1); + return xstrdup (var_get_name (pv)); break; case PSPPIRE_VAR_STORE_COL_TYPE: { @@ -679,8 +669,12 @@ text_for_column (PsppireVarStore *vs, } break; case PSPPIRE_VAR_STORE_COL_LABEL: - return recode_string (UTF8, psppire_dict_encoding (dict), - var_get_label (pv), -1); + { + const char *label = var_get_label (pv); + if (label) + return xstrdup (label); + return NULL; + } break; case PSPPIRE_VAR_STORE_COL_MISSING: @@ -694,8 +688,6 @@ text_for_column (PsppireVarStore *vs, return g_locale_to_utf8 (gettext (none), -1, 0, 0, err); else { - gchar *ss; - GString *gstr = g_string_sized_new (10); const struct val_labs *vls = var_get_value_labels (pv); const struct val_lab **labels = val_labs_sorted (vls); const struct val_lab *vl = labels[0]; @@ -706,15 +698,8 @@ text_for_column (PsppireVarStore *vs, { gchar *const vstr = value_to_text (vl->value, *write_spec); - g_string_printf (gstr, "{%s,\"%s\"}_", - vstr, val_lab_get_label (vl)); - g_free (vstr); + return g_strdup_printf ( "{%s,\"%s\"}_", vstr, val_lab_get_label (vl)); } - - ss = recode_string (UTF8, psppire_dict_encoding (dict), - gstr->str, gstr->len); - g_string_free (gstr, TRUE); - return ss; } } break; diff --git a/src/ui/gui/val-labs-dialog.c b/src/ui/gui/val-labs-dialog.c index 0af805912d..0b87d8bb64 100644 --- a/src/ui/gui/val-labs-dialog.c +++ b/src/ui/gui/val-labs-dialog.c @@ -337,17 +337,13 @@ on_remove (GtkWidget *w, gpointer data) static void on_select_row (GtkTreeView *treeview, gpointer data) { - gchar *labeltext; struct val_labs_dialog *dialog = data; union value value; - const char *label; + const char *label = NULL; gchar *text; - PsppireVarStore *var_store = - PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs)); - get_selected_tuple (dialog, &value, &label); text = value_to_text (value, *var_get_write_format (dialog->pv)); @@ -364,12 +360,8 @@ on_select_row (GtkTreeView *treeview, gpointer data) dialog->change_handler_id); - labeltext = recode_string (UTF8, psppire_dict_encoding (var_store->dict), - label, -1); - gtk_entry_set_text (GTK_ENTRY (dialog->label_entry), - labeltext); - g_free (labeltext); + label); g_signal_handler_unblock (GTK_ENTRY (dialog->label_entry), dialog->change_handler_id); @@ -481,9 +473,6 @@ repopulate_dialog (struct val_labs_dialog *dialog) GtkTreeIter iter; - PsppireVarStore *var_store = - PSPPIRE_VAR_STORE (psppire_sheet_get_model (dialog->vs)); - GtkListStore *list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_DOUBLE); @@ -511,13 +500,8 @@ repopulate_dialog (struct val_labs_dialog *dialog) value_to_text (vl->value, *var_get_write_format (dialog->pv)); - gchar *labeltext = - recode_string (UTF8, - psppire_dict_encoding (var_store->dict), - val_lab_get_label (vl), -1); - gchar *const text = g_strdup_printf ("%s = \"%s\"", - vstr, labeltext); + vstr, val_lab_get_label (vl)); gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, @@ -525,7 +509,6 @@ repopulate_dialog (struct val_labs_dialog *dialog) 1, vl->value.f, -1); - g_free (labeltext); g_free (text); g_free (vstr); }