/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
+ Copyright (C) 1997-2000, 2006-2013 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
bool compress; /* 1=compressed, 0=not compressed. */
casenumber case_cnt; /* Number of cases written so far. */
+ uint8_t space; /* ' ' in the file's character encoding. */
/* Compression buffering.
sfm_open_writer (struct file_handle *fh, struct dictionary *d,
struct sfm_write_options opts)
{
+ struct encoding_info encoding_info;
struct sfm_writer *w;
mode_t mode;
int i;
goto error;
}
+ get_encoding_info (&encoding_info, dict_get_encoding (d));
+ w->space = encoding_info.space[0];
+
/* Write the file header. */
write_header (w, d);
write_long_string_value_labels (w, d);
- if (attrset_count (dict_get_attributes (d)))
- write_data_file_attributes (w, d);
- write_variable_attributes (w, d);
+ if (opts.version >= 3)
+ {
+ if (attrset_count (dict_get_attributes (d)))
+ write_data_file_attributes (w, d);
+ write_variable_attributes (w, d);
+ }
write_mrsets (w, d, false);
static void
write_header (struct sfm_writer *w, const struct dictionary *d)
{
+ const char *dict_encoding = dict_get_encoding (d);
char prod_name[61];
char creation_date[10];
char creation_time[9];
time_t t;
/* Record-type code. */
- write_string (w, "$FL2", 4);
+ if (is_encoding_ebcdic_compatible (dict_encoding))
+ write_string (w, EBCDIC_MAGIC, 4);
+ else
+ write_string (w, ASCII_MAGIC, 4);
/* Product identification. */
snprintf (prod_name, sizeof prod_name, "@(#) SPSS DATA FILE %s - %s",
version, host_system);
- write_string (w, prod_name, 60);
+ write_utf8_string (w, dict_encoding, prod_name, 60);
/* Layout code. */
write_int (w, 2);
snprintf (creation_time, sizeof creation_time,
"%02d:%02d:%02d", hour - 1, min - 1, sec - 1);
}
- write_string (w, creation_date, 9);
- write_string (w, creation_time, 8);
+ write_utf8_string (w, dict_encoding, creation_date, 9);
+ write_utf8_string (w, dict_encoding, creation_time, 8);
/* File label. */
file_label = dict_get_label (d);
if (file_label == NULL)
file_label = "";
- write_utf8_string (w, dict_get_encoding (d), file_label, 64);
+ write_utf8_string (w, dict_encoding, file_label, 64);
/* Padding. */
write_zeros (w, 3);
ds_destroy (&s);
}
+static void
+add_role_attribute (enum var_role role, struct attrset *attrs)
+{
+ struct attribute *attr;
+ const char *s;
+
+ switch (role)
+ {
+ case ROLE_NONE:
+ default:
+ s = "0";
+ break;
+
+ case ROLE_INPUT:
+ s = "1";
+ break;
+
+ case ROLE_OUTPUT:
+ s = "2";
+ break;
+
+ case ROLE_BOTH:
+ s = "3";
+ break;
+
+ case ROLE_PARTITION:
+ s = "4";
+ break;
+
+ case ROLE_SPLIT:
+ s = "5";
+ break;
+ }
+ attrset_delete (attrs, "$@Role");
+
+ attr = attribute_create ("$@Role");
+ attribute_add_value (attr, s);
+ attrset_add (attrs, attr);
+}
+
static void
write_variable_attributes (struct sfm_writer *w, const struct dictionary *d)
{
for (i = 0; i < n_vars; i++)
{
struct variable *v = dict_get_var (d, i);
- struct attrset *attrs = var_get_attributes (v);
- if (attrset_count (attrs))
- {
- if (n_attrsets++)
- ds_put_byte (&s, '/');
- ds_put_format (&s, "%s:", var_get_name (v));
- put_attrset (&s, attrs);
- }
+ struct attrset attrs;
+
+ attrset_clone (&attrs, var_get_attributes (v));
+
+ add_role_attribute (var_get_role (v), &attrs);
+ if (n_attrsets++)
+ ds_put_byte (&s, '/');
+ ds_put_format (&s, "%s:", var_get_name (v));
+ put_attrset (&s, &attrs);
+ attrset_destroy (&attrs);
}
if (n_attrsets)
write_utf8_record (w, dict_get_encoding (d), &s, 18);
size_t n_mrsets;
size_t i;
+ if (is_encoding_ebcdic_compatible (encoding))
+ {
+ /* FIXME. */
+ return;
+ }
+
n_mrsets = dict_get_n_mrsets (dict);
if (n_mrsets == 0)
return;
for (j = 0; j < mrset->n_vars; j++)
{
const char *short_name_utf8 = var_get_short_name (mrset->vars[j], 0);
+ char *lower_name_utf8 = utf8_to_lower (short_name_utf8);
char *short_name = recode_string (encoding, "UTF-8",
- short_name_utf8, -1);
- str_lowercase (short_name);
+ lower_name_utf8, -1);
ds_put_format (&s, " %s", short_name);
free (short_name);
+ free (lower_name_utf8);
}
ds_put_byte (&s, '\n');
}
write_integer_info_record (struct sfm_writer *w,
const struct dictionary *d)
{
+ const char *dict_encoding = dict_get_encoding (d);
int version_component[3];
int float_format;
int codepage;
abort ();
/* Choose codepage. */
- codepage = sys_get_codepage_from_encoding (dict_get_encoding (d));
+ codepage = sys_get_codepage_from_encoding (dict_encoding);
if (codepage == 0)
{
- /* Default to "7-bit ASCII" if the codepage number is unknown, because
+ /* The codepage is unknown. Choose a default.
+
+ For an EBCDIC-compatible encoding, use the value for EBCDIC.
+
+ For an ASCII-compatible encoding, default to "7-bit ASCII", because
many files use this codepage number regardless of their actual
- encoding. */
- codepage = 2;
+ encoding.
+ */
+ if (is_encoding_ascii_compatible (dict_encoding))
+ codepage = 2;
+ else if (is_encoding_ebcdic_compatible (dict_encoding))
+ codepage = 1;
}
/* Write record. */
assert (w->data_cnt < 8);
assert (size <= 8);
- memset (w->data[w->data_cnt], ' ', 8);
+ memset (w->data[w->data_cnt], w->space, 8);
memcpy (w->data[w->data_cnt], data, size);
w->data_cnt++;
}
size_t pad_bytes = width - data_bytes;
write_bytes (w, string, data_bytes);
while (pad_bytes-- > 0)
- putc (' ', w->file);
+ putc (w->space, w->file);
}
/* Recodes null-terminated UTF-8 encoded STRING into ENCODING, and writes the
write_spaces (struct sfm_writer *w, size_t n)
{
while (n-- > 0)
- putc (' ', w->file);
+ putc (w->space, w->file);
}