value-labels: Interpret \n as new-line in value labels.
[pspp-builds.git] / src / data / sys-file-writer.c
index efefa8fb7e7e17754c1ff0589337fb52f07e4b60..d2522c23f82d1093be8edcea269c5841fc3fde69 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 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
@@ -47,6 +47,7 @@
 #include "libpspp/message.h"
 #include "libpspp/misc.h"
 #include "libpspp/str.h"
+#include "libpspp/string-array.h"
 #include "libpspp/version.h"
 
 #include "gl/xmemdup0.h"
@@ -238,7 +239,7 @@ sfm_open_writer (struct file_handle *fh, struct dictionary *d,
       idx += sfm_width_to_octs (var_get_width (v));
     }
 
-  if (dict_get_documents (d) != NULL)
+  if (dict_get_document_line_cnt (d) > 0)
     write_documents (w, d);
 
   write_integer_info_record (w);
@@ -531,7 +532,8 @@ write_value_labels (struct sfm_writer *w, struct variable *v, int idx)
   for (i = 0; i < n_labels; i++)
     {
       const struct val_lab *vl = labels[i];
-      char *label = recode_string (var_get_encoding (v), UTF8, val_lab_get_label (vl), -1);
+      char *label = recode_string (var_get_encoding (v), UTF8,
+                                   val_lab_get_escaped_label (vl), -1);
       uint8_t len = MIN (strlen (label), 255);
 
       write_value (w, val_lab_get_value (vl), var_get_width (v));
@@ -552,11 +554,22 @@ write_value_labels (struct sfm_writer *w, struct variable *v, int idx)
 static void
 write_documents (struct sfm_writer *w, const struct dictionary *d)
 {
-  size_t line_cnt = dict_get_document_line_cnt (d);
+  const struct string_array *docs = dict_get_documents (d);
+  const char *enc = dict_get_encoding (d);
+  size_t i;
 
   write_int (w, 6);             /* Record type. */
-  write_int (w, line_cnt);
-  write_bytes (w, dict_get_documents (d), line_cnt * DOC_LINE_LENGTH);
+  write_int (w, docs->n);
+  for (i = 0; i < docs->n; i++)
+    {
+      char *s = recode_string (enc, "UTF-8", docs->strings[i], -1);
+      size_t s_len = strlen (s);
+      size_t write_len = MIN (s_len, DOC_LINE_LENGTH);
+
+      write_bytes (w, s, write_len);
+      write_spaces (w, DOC_LINE_LENGTH - write_len);
+      free (s);
+    }
 }
 
 static void
@@ -679,7 +692,7 @@ write_mrsets (struct sfm_writer *w, const struct dictionary *dict,
         ds_put_format (&s, " %s", var_get_short_name (mrset->vars[j], 0));
       ds_put_byte (&s, '\n');
     }
-  write_attribute_record (w, &s, 7);
+  write_attribute_record (w, &s, pre_v14 ? 7 : 19);
   ds_destroy (&s);
 }
 
@@ -770,7 +783,7 @@ write_long_string_value_labels (struct sfm_writer *w,
       size += 12 + strlen (var_get_name (var));
       for (val_lab = val_labs_first (val_labs); val_lab != NULL;
            val_lab = val_labs_next (val_labs, val_lab))
-        size += 8 + width + strlen (val_lab_get_label (val_lab));
+        size += 8 + width + strlen (val_lab_get_escaped_label (val_lab));
     }
   if (size == 0)
     return;
@@ -799,7 +812,7 @@ write_long_string_value_labels (struct sfm_writer *w,
       for (val_lab = val_labs_first (val_labs); val_lab != NULL;
            val_lab = val_labs_next (val_labs, val_lab))
         {
-          const char *label = val_lab_get_label (val_lab);
+          const char *label = val_lab_get_escaped_label (val_lab);
           size_t label_length = strlen (label);
 
           write_int (w, width);