Delete trailing whitespace at end of lines.
[pspp-builds.git] / src / data / sys-file-writer.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "sys-file-writer.h"
22 #include "sys-file-private.h"
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #include <libpspp/alloc.h>
33 #include <libpspp/hash.h>
34 #include <libpspp/magic.h>
35 #include <libpspp/message.h>
36 #include <libpspp/misc.h>
37 #include <libpspp/str.h>
38 #include <libpspp/version.h>
39
40 #include <data/case.h>
41 #include <data/casewriter-provider.h>
42 #include <data/casewriter.h>
43 #include <data/dictionary.h>
44 #include <data/file-handle-def.h>
45 #include <data/format.h>
46 #include <data/missing-values.h>
47 #include <data/settings.h>
48 #include <data/value-labels.h>
49 #include <data/variable.h>
50
51 #include "minmax.h"
52
53 #include "gettext.h"
54 #define _(msgid) gettext (msgid)
55
56 /* Find 64-bit floating-point type. */
57 #if SIZEOF_FLOAT == 8
58   #define flt64 float
59   #define FLT64_MAX FLT_MAX
60 #elif SIZEOF_DOUBLE == 8
61   #define flt64 double
62   #define FLT64_MAX DBL_MAX
63 #elif SIZEOF_LONG_DOUBLE == 8
64   #define flt64 long double
65   #define FLT64_MAX LDBL_MAX
66 #else
67   #error Which one of your basic types is 64-bit floating point?
68 #endif
69
70 /* Figure out SYSMIS value for flt64. */
71 #include <libpspp/magic.h>
72 #if SIZEOF_DOUBLE == 8
73 #define second_lowest_flt64 second_lowest_value
74 #else
75 #error Must define second_lowest_flt64 for your architecture.
76 #endif
77
78 /* Record Type 1: General Information. */
79 struct sysfile_header
80   {
81     char rec_type[4] ;          /* 00: Record-type code, "$FL2". */
82     char prod_name[60] ;        /* 04: Product identification. */
83     int32_t layout_code ;       /* 40: 2. */
84     int32_t nominal_case_size ; /* 44: Number of `value's per case.
85                                    Note: some systems set this to -1 */
86     int32_t compress ;          /* 48: 1=compressed, 0=not compressed. */
87     int32_t weight_idx ;         /* 4c: 1-based index of weighting var, or 0. */
88     int32_t case_cnt ;          /* 50: Number of cases, -1 if unknown. */
89     flt64 bias ;                /* 54: Compression bias (100.0). */
90     char creation_date[9] ;     /* 5c: `dd mmm yy' creation date of file. */
91     char creation_time[8] ;     /* 65: `hh:mm:ss' 24-hour creation time. */
92     char file_label[64] ;       /* 6d: File label. */
93     char padding[3] ;           /* ad: Ignored padding. */
94   } ATTRIBUTE((packed)) ;
95
96 /* Record Type 2: Variable. */
97 struct sysfile_variable
98   {
99     int32_t rec_type ;          /* 2. */
100     int32_t type ;              /* 0=numeric, 1-255=string width,
101                                    -1=continued string. */
102     int32_t has_var_label ;     /* 1=has a variable label, 0=doesn't. */
103     int32_t n_missing_values ;  /* Missing value code of -3,-2,0,1,2, or 3. */
104     int32_t print ;             /* Print format. */
105     int32_t write ;             /* Write format. */
106     char name[SHORT_NAME_LEN] ; /* Variable name. */
107     /* The rest of the structure varies. */
108   } ATTRIBUTE((packed)) ;
109
110 /* Compression bias used by PSPP.  Values between (1 -
111    COMPRESSION_BIAS) and (251 - COMPRESSION_BIAS) inclusive can be
112    compressed. */
113 #define COMPRESSION_BIAS 100
114
115 /* System file writer. */
116 struct sfm_writer
117   {
118     struct file_handle *fh;     /* File handle. */
119     FILE *file;                 /* File stream. */
120
121     int needs_translation;      /* 0=use fast path, 1=translation needed. */
122     int compress;               /* 1=compressed, 0=not compressed. */
123     int case_cnt;               /* Number of cases written so far. */
124     size_t flt64_cnt;           /* Number of flt64 elements in case. */
125     bool has_vls;               /* Does the dict have very long strings? */
126
127     /* Compression buffering. */
128     flt64 *buf;                 /* Buffered data. */
129     flt64 *end;                 /* Buffer end. */
130     flt64 *ptr;                 /* Current location in buffer. */
131     unsigned char *x;           /* Location in current instruction octet. */
132     unsigned char *y;           /* End of instruction octet. */
133
134     /* Variables. */
135     struct sfm_var *vars;       /* Variables. */
136     size_t var_cnt;             /* Number of variables. */
137     size_t var_cnt_vls;         /* Number of variables including
138                                    very long string components. */
139   };
140
141 /* A variable in a system file. */
142 struct sfm_var
143   {
144     int width;                  /* 0=numeric, otherwise string width. */
145     int fv;                     /* Index into case. */
146     size_t flt64_cnt;           /* Number of flt64 elements. */
147   };
148
149 static struct casewriter_class sys_file_casewriter_class;
150
151 static char *append_string_max (char *, const char *, const char *);
152 static void write_header (struct sfm_writer *, const struct dictionary *);
153 static void buf_write (struct sfm_writer *, const void *, size_t);
154 static void write_variable (struct sfm_writer *, const struct variable *);
155 static void write_value_labels (struct sfm_writer *,
156                                 struct variable *, int idx);
157 static void write_rec_7_34 (struct sfm_writer *);
158
159 static void write_longvar_table (struct sfm_writer *w,
160                                  const struct dictionary *dict);
161
162 static void write_vls_length_table (struct sfm_writer *w,
163                               const struct dictionary *dict);
164
165
166 static void write_variable_display_parameters (struct sfm_writer *w,
167                                                const struct dictionary *dict);
168
169 static void write_documents (struct sfm_writer *, const struct dictionary *);
170
171 bool write_error (const struct sfm_writer *);
172 bool close_writer (struct sfm_writer *);
173
174 static inline int
175 var_flt64_cnt (const struct variable *v)
176 {
177   assert(sizeof(flt64) == MAX_SHORT_STRING);
178   return sfm_width_to_bytes(var_get_width (v)) / MAX_SHORT_STRING ;
179 }
180
181 static inline int
182 var_flt64_cnt_nom (const struct variable *v)
183 {
184   return (var_is_numeric (v)
185           ? 1 : DIV_RND_UP (var_get_width (v), sizeof (flt64)));
186 }
187
188
189 /* Returns default options for writing a system file. */
190 struct sfm_write_options
191 sfm_writer_default_options (void)
192 {
193   struct sfm_write_options opts;
194   opts.create_writeable = true;
195   opts.compress = get_scompression ();
196   opts.version = 3;
197   return opts;
198 }
199
200
201 /* Return a short variable name to be used as the continuation of the
202    variable with the short name SN.
203
204    FIXME: Need to resolve clashes somehow.
205
206  */
207 static const char *
208 cont_var_name(const char *sn, int idx)
209 {
210   static char s[SHORT_NAME_LEN + 1];
211
212   char abb[SHORT_NAME_LEN + 1 - 3]= {0};
213
214   strncpy(abb, sn, SHORT_NAME_LEN - 3);
215
216   snprintf(s, SHORT_NAME_LEN + 1, "%s%03d", abb, idx);
217
218   return s;
219 }
220
221
222 /* Opens the system file designated by file handle FH for writing
223    cases from dictionary D according to the given OPTS.  If
224    COMPRESS is nonzero, the system file will be compressed.
225
226    No reference to D is retained, so it may be modified or
227    destroyed at will after this function returns.  D is not
228    modified by this function, except to assign short names. */
229 struct casewriter *
230 sfm_open_writer (struct file_handle *fh, struct dictionary *d,
231                  struct sfm_write_options opts)
232 {
233   struct sfm_writer *w = NULL;
234   mode_t mode;
235   int fd;
236   int idx;
237   int i;
238
239   /* Check version. */
240   if (opts.version != 2 && opts.version != 3)
241     {
242       msg (ME, _("Unknown system file version %d. Treating as version %d."),
243            opts.version, 3);
244       opts.version = 3;
245     }
246
247   /* Create file. */
248   mode = S_IRUSR | S_IRGRP | S_IROTH;
249   if (opts.create_writeable)
250     mode |= S_IWUSR | S_IWGRP | S_IWOTH;
251   fd = open (fh_get_file_name (fh), O_WRONLY | O_CREAT | O_TRUNC, mode);
252   if (fd < 0)
253     goto open_error;
254
255   /* Open file handle. */
256   if (!fh_open (fh, FH_REF_FILE, "system file", "we"))
257     goto error;
258
259   /* Create and initialize writer. */
260   w = xmalloc (sizeof *w);
261   w->fh = fh;
262   w->file = fdopen (fd, "w");
263
264   w->needs_translation = dict_compacting_would_change (d);
265   w->compress = opts.compress;
266   w->case_cnt = 0;
267   w->flt64_cnt = 0;
268   w->has_vls = false;
269
270   w->buf = w->end = w->ptr = NULL;
271   w->x = w->y = NULL;
272
273   w->var_cnt = dict_get_var_cnt (d);
274   w->var_cnt_vls = w->var_cnt;
275   w->vars = xnmalloc (w->var_cnt, sizeof *w->vars);
276   for (i = 0; i < w->var_cnt; i++)
277     {
278       const struct variable *dv = dict_get_var (d, i);
279       struct sfm_var *sv = &w->vars[i];
280       sv->width = var_get_width (dv);
281       /* spss compatibility nonsense */
282       if ( var_get_width (dv) >= MIN_VERY_LONG_STRING )
283           w->has_vls = true;
284
285       sv->fv = var_get_case_index (dv);
286       sv->flt64_cnt = var_flt64_cnt (dv);
287     }
288
289   /* Check that file create succeeded. */
290   if (w->file == NULL)
291     {
292       close (fd);
293       goto open_error;
294     }
295
296   /* Write the file header. */
297   write_header (w, d);
298
299   /* Write basic variable info. */
300   dict_assign_short_names (d);
301   for (i = 0; i < dict_get_var_cnt (d); i++)
302     {
303       int count = 0;
304       const struct variable *v = dict_get_var(d, i);
305       int wcount = var_get_width (v);
306
307       do {
308         struct variable *var_cont = var_clone (v);
309         var_set_short_name (var_cont, var_get_short_name (v));
310         if ( var_is_alpha (v))
311           {
312             if ( 0 != count )
313               {
314                 var_clear_missing_values (var_cont);
315                 var_set_short_name (var_cont,
316                                     cont_var_name (var_get_short_name (v),
317                                                    count));
318                 var_clear_label (var_cont);
319                 w->var_cnt_vls++;
320               }
321             count++;
322             if ( wcount >= MIN_VERY_LONG_STRING )
323               {
324                 var_set_width (var_cont, MIN_VERY_LONG_STRING - 1);
325                 wcount -= EFFECTIVE_LONG_STRING_LENGTH;
326               }
327             else
328               {
329                 var_set_width (var_cont, wcount);
330                 wcount -= var_get_width (var_cont);
331               }
332           }
333
334         write_variable (w, var_cont);
335         var_destroy (var_cont);
336       } while(wcount > 0);
337     }
338
339   /* Write out value labels. */
340   for (idx = i = 0; i < dict_get_var_cnt (d); i++)
341     {
342       struct variable *v = dict_get_var (d, i);
343
344       write_value_labels (w, v, idx);
345       idx += var_flt64_cnt (v);
346     }
347
348   if (dict_get_documents (d) != NULL)
349     write_documents (w, d);
350
351   write_rec_7_34 (w);
352
353   write_variable_display_parameters (w, d);
354
355   if (opts.version >= 3)
356     write_longvar_table (w, d);
357
358   write_vls_length_table(w, d);
359
360   /* Write end-of-headers record. */
361   {
362     struct
363       {
364         int32_t rec_type ;
365         int32_t filler ;
366     } ATTRIBUTE((packed))
367     rec_999;
368
369     rec_999.rec_type = 999;
370     rec_999.filler = 0;
371
372     buf_write (w, &rec_999, sizeof rec_999);
373   }
374
375   if (w->compress)
376     {
377       w->buf = xnmalloc (128, sizeof *w->buf);
378       w->ptr = w->buf;
379       w->end = &w->buf[128];
380       w->x = (unsigned char *) w->ptr++;
381       w->y = (unsigned char *) w->ptr;
382     }
383
384   if (write_error (w))
385     goto error;
386
387   return casewriter_create (&sys_file_casewriter_class, w);
388
389  error:
390   close_writer (w);
391   return NULL;
392
393  open_error:
394   msg (ME, _("Error opening \"%s\" for writing as a system file: %s."),
395        fh_get_file_name (fh), strerror (errno));
396   goto error;
397 }
398
399 /* Returns value of X truncated to two least-significant digits. */
400 static int
401 rerange (int x)
402 {
403   if (x < 0)
404     x = -x;
405   if (x >= 100)
406     x %= 100;
407   return x;
408 }
409
410 /* Write the sysfile_header header to system file W. */
411 static void
412 write_header (struct sfm_writer *w, const struct dictionary *d)
413 {
414   struct sysfile_header hdr;
415   char *p;
416   int i;
417
418   time_t t;
419
420   memcpy (hdr.rec_type, "$FL2", 4);
421
422   p = stpcpy (hdr.prod_name, "@(#) SPSS DATA FILE ");
423   p = append_string_max (p, version, &hdr.prod_name[60]);
424   p = append_string_max (p, " - ", &hdr.prod_name[60]);
425   p = append_string_max (p, host_system, &hdr.prod_name[60]);
426   memset (p, ' ', &hdr.prod_name[60] - p);
427
428   hdr.layout_code = 2;
429
430   w->flt64_cnt = 0;
431   for (i = 0; i < dict_get_var_cnt (d); i++)
432     {
433       w->flt64_cnt += var_flt64_cnt (dict_get_var (d, i));
434     }
435   hdr.nominal_case_size = w->flt64_cnt;
436
437   hdr.compress = w->compress;
438
439   if (dict_get_weight (d) != NULL)
440     {
441       const struct variable *weight_var;
442       int recalc_weight_idx = 1;
443       int i;
444
445       weight_var = dict_get_weight (d);
446       for (i = 0; ; i++)
447         {
448           struct variable *v = dict_get_var (d, i);
449           if (v == weight_var)
450             break;
451           recalc_weight_idx += var_flt64_cnt (v);
452         }
453       hdr.weight_idx = recalc_weight_idx;
454     }
455   else
456     hdr.weight_idx = 0;
457
458   hdr.case_cnt = -1;
459   hdr.bias = COMPRESSION_BIAS;
460
461   if (time (&t) == (time_t) -1)
462     {
463       memcpy (hdr.creation_date, "01 Jan 70", 9);
464       memcpy (hdr.creation_time, "00:00:00", 8);
465     }
466   else
467     {
468       static const char *month_name[12] =
469         {
470           "Jan", "Feb", "Mar", "Apr", "May", "Jun",
471           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
472         };
473       struct tm *tmp = localtime (&t);
474       int day = rerange (tmp->tm_mday);
475       int mon = rerange (tmp->tm_mon + 1);
476       int year = rerange (tmp->tm_year);
477       int hour = rerange (tmp->tm_hour + 1);
478       int min = rerange (tmp->tm_min + 1);
479       int sec = rerange (tmp->tm_sec + 1);
480       char buf[10];
481
482       sprintf (buf, "%02d %s %02d", day, month_name[mon - 1], year);
483       memcpy (hdr.creation_date, buf, sizeof hdr.creation_date);
484       sprintf (buf, "%02d:%02d:%02d", hour - 1, min - 1, sec - 1);
485       memcpy (hdr.creation_time, buf, sizeof hdr.creation_time);
486     }
487
488   {
489     const char *label = dict_get_label (d);
490     if (label == NULL)
491       label = "";
492
493     buf_copy_str_rpad (hdr.file_label, sizeof hdr.file_label, label);
494   }
495
496   memset (hdr.padding, 0, sizeof hdr.padding);
497
498   buf_write (w, &hdr, sizeof hdr);
499 }
500
501 /* Translates format spec from internal form in SRC to system file
502    format in DEST. */
503 static inline void
504 write_format_spec (const struct fmt_spec *src, int32_t *dest)
505 {
506   assert (fmt_check_output (src));
507   *dest = (fmt_to_io (src->type) << 16) | (src->w << 8) | src->d;
508 }
509
510 /* Write the variable record(s) for primary variable P and secondary
511    variable S to system file W. */
512 static void
513 write_variable (struct sfm_writer *w, const struct variable *v)
514 {
515   struct sysfile_variable sv;
516
517   /* Missing values. */
518   struct missing_values mv;
519   flt64 m[3];           /* Missing value values. */
520   int nm;               /* Number of missing values, possibly negative. */
521   const char *label = var_get_label (v);
522
523   sv.rec_type = 2;
524   sv.type = MIN (var_get_width (v), MIN_VERY_LONG_STRING - 1);
525   sv.has_var_label = label != NULL;
526
527   mv_copy (&mv, var_get_missing_values (v));
528   nm = 0;
529   if (mv_has_range (&mv))
530     {
531       double x, y;
532       mv_pop_range (&mv, &x, &y);
533       m[nm++] = x == LOWEST ? second_lowest_flt64 : x;
534       m[nm++] = y == HIGHEST ? FLT64_MAX : y;
535     }
536   while (mv_has_value (&mv))
537     {
538       union value value;
539       mv_pop_value (&mv, &value);
540       if (var_is_numeric (v))
541         m[nm] = value.f;
542       else
543         buf_copy_rpad ((char *) &m[nm], sizeof m[nm], value.s,
544                        var_get_width (v));
545       nm++;
546     }
547   if (mv_has_range (var_get_missing_values (v)))
548     nm = -nm;
549
550   sv.n_missing_values = nm;
551   write_format_spec (var_get_print_format (v), &sv.print);
552   write_format_spec (var_get_write_format (v), &sv.write);
553   buf_copy_str_rpad (sv.name, sizeof sv.name, var_get_short_name (v));
554   buf_write (w, &sv, sizeof sv);
555
556   if (label != NULL)
557     {
558       struct label
559         {
560           int32_t label_len ;
561           char label[255] ;
562       } ATTRIBUTE((packed))
563       l;
564
565       int ext_len;
566
567       l.label_len = MIN (strlen (label), 255);
568       ext_len = ROUND_UP (l.label_len, sizeof l.label_len);
569       memcpy (l.label, label, l.label_len);
570       memset (&l.label[l.label_len], ' ', ext_len - l.label_len);
571
572       buf_write (w, &l, offsetof (struct label, label) + ext_len);
573     }
574
575   if (nm)
576     buf_write (w, m, sizeof *m * abs (nm));
577
578   if (var_is_alpha (v) && var_get_width (v) > (int) sizeof (flt64))
579     {
580       int i;
581       int pad_count;
582
583       sv.type = -1;
584       sv.has_var_label = 0;
585       sv.n_missing_values = 0;
586       memset (&sv.print, 0, sizeof sv.print);
587       memset (&sv.write, 0, sizeof sv.write);
588       memset (&sv.name, 0, sizeof sv.name);
589
590       pad_count = DIV_RND_UP (MIN(var_get_width (v), MIN_VERY_LONG_STRING - 1),
591                               (int) sizeof (flt64)) - 1;
592       for (i = 0; i < pad_count; i++)
593         buf_write (w, &sv, sizeof sv);
594     }
595 }
596
597 /* Writes the value labels for variable V having system file
598    variable index IDX to system file W. */
599 static void
600 write_value_labels (struct sfm_writer *w, struct variable *v, int idx)
601 {
602   struct value_label_rec
603     {
604       int32_t rec_type ;
605       int32_t n_labels ;
606       flt64 labels[1] ;
607     } ATTRIBUTE((packed));
608
609   struct var_idx_rec
610     {
611       int32_t rec_type ;
612       int32_t n_vars ;
613       int32_t vars[1] ;
614     } ATTRIBUTE((packed));
615
616   const struct val_labs *val_labs;
617   struct val_labs_iterator *i;
618   struct value_label_rec *vlr;
619   struct var_idx_rec vir;
620   struct val_lab *vl;
621   size_t vlr_size;
622   flt64 *loc;
623
624   val_labs = var_get_value_labels (v);
625   if (val_labs == NULL)
626     return;
627
628   /* Pass 1: Count bytes. */
629   vlr_size = (sizeof (struct value_label_rec)
630               + sizeof (flt64) * (val_labs_count (val_labs) - 1));
631   for (vl = val_labs_first (val_labs, &i); vl != NULL;
632        vl = val_labs_next (val_labs, &i))
633     vlr_size += ROUND_UP (strlen (vl->label) + 1, sizeof (flt64));
634
635   /* Pass 2: Copy bytes. */
636   vlr = xmalloc (vlr_size);
637   vlr->rec_type = 3;
638   vlr->n_labels = val_labs_count (val_labs);
639   loc = vlr->labels;
640   for (vl = val_labs_first_sorted (val_labs, &i); vl != NULL;
641        vl = val_labs_next (val_labs, &i))
642     {
643       size_t len = strlen (vl->label);
644
645       *loc++ = vl->value.f;
646       *(unsigned char *) loc = len;
647       memcpy (&((char *) loc)[1], vl->label, len);
648       memset (&((char *) loc)[1 + len], ' ',
649               REM_RND_UP (len + 1, sizeof (flt64)));
650       loc += DIV_RND_UP (len + 1, sizeof (flt64));
651     }
652
653   buf_write (w, vlr, vlr_size);
654   free (vlr);
655
656   vir.rec_type = 4;
657   vir.n_vars = 1;
658   vir.vars[0] = idx + 1;
659   buf_write (w, &vir, sizeof vir);
660 }
661
662 /* Writes record type 6, document record. */
663 static void
664 write_documents (struct sfm_writer *w, const struct dictionary *d)
665 {
666   struct
667   {
668     int32_t rec_type ;          /* Always 6. */
669     int32_t n_lines ;           /* Number of lines of documents. */
670   } ATTRIBUTE((packed)) rec_6;
671
672   const char * documents = dict_get_documents (d);
673   size_t doc_bytes = strlen (documents);
674
675   assert (doc_bytes % 80 == 0);
676
677   rec_6.rec_type = 6;
678   rec_6.n_lines = doc_bytes / 80;
679   buf_write (w, &rec_6, sizeof rec_6);
680   buf_write (w, documents, 80 * rec_6.n_lines);
681 }
682
683 /* Write the alignment, width and scale values */
684 static void
685 write_variable_display_parameters (struct sfm_writer *w,
686                                    const struct dictionary *dict)
687 {
688   int i;
689
690   struct
691   {
692     int32_t rec_type ;
693     int32_t subtype ;
694     int32_t elem_size ;
695     int32_t n_elem ;
696   } ATTRIBUTE((packed)) vdp_hdr;
697
698   vdp_hdr.rec_type = 7;
699   vdp_hdr.subtype = 11;
700   vdp_hdr.elem_size = 4;
701   vdp_hdr.n_elem = w->var_cnt_vls * 3;
702
703   buf_write (w, &vdp_hdr, sizeof vdp_hdr);
704
705   for ( i = 0 ; i < w->var_cnt ; ++i )
706     {
707       struct variable *v;
708       struct
709       {
710         int32_t measure ;
711         int32_t width ;
712         int32_t align ;
713       } ATTRIBUTE((packed)) params;
714
715       v = dict_get_var(dict, i);
716
717       params.measure = (var_get_measure (v) == MEASURE_NOMINAL ? 1
718                         : var_get_measure (v) == MEASURE_ORDINAL ? 2
719                         : 3);
720       params.width = var_get_display_width (v);
721       params.align = (var_get_alignment (v) == ALIGN_LEFT ? 0
722                       : var_get_alignment (v) == ALIGN_RIGHT ? 1
723                       : 2);
724
725       buf_write (w, &params, sizeof(params));
726
727       if (var_is_long_string (v))
728         {
729           int wcount = var_get_width (v) - EFFECTIVE_LONG_STRING_LENGTH ;
730
731           while (wcount > 0)
732             {
733               params.width = wcount >= MIN_VERY_LONG_STRING ? 32 : wcount;
734
735               buf_write (w, &params, sizeof(params));
736
737               wcount -= EFFECTIVE_LONG_STRING_LENGTH ;
738             }
739         }
740     }
741 }
742
743 /* Writes the table of lengths for Very Long String Variables */
744 static void
745 write_vls_length_table (struct sfm_writer *w,
746                         const struct dictionary *dict)
747 {
748   int i;
749   struct
750   {
751     int32_t rec_type ;
752     int32_t subtype ;
753     int32_t elem_size ;
754     int32_t n_elem ;
755   } ATTRIBUTE((packed)) vls_hdr;
756
757   struct string vls_length_map;
758
759   ds_init_empty (&vls_length_map);
760
761   vls_hdr.rec_type = 7;
762   vls_hdr.subtype = 14;
763   vls_hdr.elem_size = 1;
764
765
766   for (i = 0; i < dict_get_var_cnt (dict); ++i)
767     {
768       const struct variable *v = dict_get_var (dict, i);
769
770       if ( var_get_width (v) < MIN_VERY_LONG_STRING )
771         continue;
772
773       ds_put_format (&vls_length_map, "%s=%05d",
774                      var_get_short_name (v), var_get_width (v));
775       ds_put_char (&vls_length_map, '\0');
776       ds_put_char (&vls_length_map, '\t');
777     }
778
779   vls_hdr.n_elem = ds_length (&vls_length_map);
780
781   if ( vls_hdr.n_elem > 0 )
782     {
783       buf_write (w, &vls_hdr, sizeof vls_hdr);
784       buf_write (w, ds_data (&vls_length_map), ds_length (&vls_length_map));
785     }
786
787   ds_destroy (&vls_length_map);
788 }
789
790 /* Writes the long variable name table */
791 static void
792 write_longvar_table (struct sfm_writer *w, const struct dictionary *dict)
793 {
794   struct
795     {
796       int32_t rec_type ;
797       int32_t subtype ;
798       int32_t elem_size ;
799       int32_t n_elem ;
800   } ATTRIBUTE((packed)) lv_hdr;
801
802   struct string long_name_map;
803   size_t i;
804
805   ds_init_empty (&long_name_map);
806   for (i = 0; i < dict_get_var_cnt (dict); i++)
807     {
808       struct variable *v = dict_get_var (dict, i);
809
810       if (i)
811         ds_put_char (&long_name_map, '\t');
812       ds_put_format (&long_name_map, "%s=%s",
813                      var_get_short_name (v), var_get_name (v));
814     }
815
816   lv_hdr.rec_type = 7;
817   lv_hdr.subtype = 13;
818   lv_hdr.elem_size = 1;
819   lv_hdr.n_elem = ds_length (&long_name_map);
820
821   buf_write (w, &lv_hdr, sizeof lv_hdr);
822   buf_write (w, ds_data (&long_name_map), ds_length (&long_name_map));
823
824   ds_destroy (&long_name_map);
825 }
826
827 /* Writes record type 7, subtypes 3 and 4. */
828 static void
829 write_rec_7_34 (struct sfm_writer *w)
830 {
831   struct
832     {
833       int32_t rec_type_3 ;
834       int32_t subtype_3 ;
835       int32_t data_type_3 ;
836       int32_t n_elem_3 ;
837       int32_t elem_3[8] ;
838       int32_t rec_type_4 ;
839       int32_t subtype_4 ;
840       int32_t data_type_4 ;
841       int32_t n_elem_4 ;
842       flt64 elem_4[3] ;
843   } ATTRIBUTE((packed)) rec_7;
844
845   /* Components of the version number, from major to minor. */
846   int version_component[3];
847
848   /* Used to step through the version string. */
849   char *p;
850
851   /* Parses the version string, which is assumed to be of the form
852      #.#x, where each # is a string of digits, and x is a single
853      letter. */
854   version_component[0] = strtol (bare_version, &p, 10);
855   if (*p == '.')
856     p++;
857   version_component[1] = strtol (bare_version, &p, 10);
858   version_component[2] = (isalpha ((unsigned char) *p)
859                           ? tolower ((unsigned char) *p) - 'a' : 0);
860
861   rec_7.rec_type_3 = 7;
862   rec_7.subtype_3 = 3;
863   rec_7.data_type_3 = sizeof (int32_t);
864   rec_7.n_elem_3 = 8;
865   rec_7.elem_3[0] = version_component[0];
866   rec_7.elem_3[1] = version_component[1];
867   rec_7.elem_3[2] = version_component[2];
868   rec_7.elem_3[3] = -1;
869
870   /* PORTME: 1=IEEE754, 2=IBM 370, 3=DEC VAX E. */
871 #ifdef FPREP_IEEE754
872   rec_7.elem_3[4] = 1;
873 #endif
874
875   rec_7.elem_3[5] = 1;
876
877   /* PORTME: 1=big-endian, 2=little-endian. */
878 #if WORDS_BIGENDIAN
879   rec_7.elem_3[6] = 1;
880 #else
881   rec_7.elem_3[6] = 2;
882 #endif
883
884   /* PORTME: 1=EBCDIC, 2=7-bit ASCII, 3=8-bit ASCII, 4=DEC Kanji. */
885   rec_7.elem_3[7] = 2;
886
887   rec_7.rec_type_4 = 7;
888   rec_7.subtype_4 = 4;
889   rec_7.data_type_4 = sizeof (flt64);
890   rec_7.n_elem_4 = 3;
891   rec_7.elem_4[0] = -FLT64_MAX;
892   rec_7.elem_4[1] = FLT64_MAX;
893   rec_7.elem_4[2] = second_lowest_flt64;
894
895   buf_write (w, &rec_7, sizeof rec_7);
896 }
897
898 /* Write NBYTES starting at BUF to the system file represented by
899    H. */
900 static void
901 buf_write (struct sfm_writer *w, const void *buf, size_t nbytes)
902 {
903   assert (buf != NULL);
904   fwrite (buf, nbytes, 1, w->file);
905 }
906
907 /* Copies string DEST to SRC with the proviso that DEST does not reach
908    byte END; no null terminator is copied.  Returns a pointer to the
909    byte after the last byte copied. */
910 static char *
911 append_string_max (char *dest, const char *src, const char *end)
912 {
913   int nbytes = MIN (end - dest, (int) strlen (src));
914   memcpy (dest, src, nbytes);
915   return dest + nbytes;
916 }
917
918 /* Makes certain that the compression buffer of H has room for another
919    element.  If there's not room, pads out the current instruction
920    octet with zero and dumps out the buffer. */
921 static void
922 ensure_buf_space (struct sfm_writer *w)
923 {
924   if (w->ptr >= w->end)
925     {
926       memset (w->x, 0, w->y - w->x);
927       w->x = w->y;
928       w->ptr = w->buf;
929       buf_write (w, w->buf, sizeof *w->buf * 128);
930     }
931 }
932
933 static void write_compressed_data (struct sfm_writer *w, const flt64 *elem);
934
935 /* Writes case C to system file W. */
936 static void
937 sys_file_casewriter_write (struct casewriter *writer, void *w_,
938                            struct ccase *c)
939 {
940   struct sfm_writer *w = w_;
941   if (ferror (w->file))
942     {
943       casewriter_force_error (writer);
944       case_destroy (c);
945       return;
946     }
947
948   w->case_cnt++;
949
950   if (!w->needs_translation && !w->compress
951       && sizeof (flt64) == sizeof (union value) && ! w->has_vls )
952     {
953       /* Fast path: external and internal representations are the
954          same and the dictionary is properly ordered.  Write
955          directly to file. */
956       buf_write (w, case_data_all (c), sizeof (union value) * w->flt64_cnt);
957     }
958   else
959     {
960       /* Slow path: internal and external representations differ.
961          Write into a bounce buffer, then write to W. */
962       flt64 *bounce;
963       flt64 *bounce_cur;
964       flt64 *bounce_end;
965       size_t bounce_size;
966       size_t i;
967
968       bounce_size = sizeof *bounce * w->flt64_cnt;
969       bounce = bounce_cur = local_alloc (bounce_size);
970       bounce_end = bounce + bounce_size;
971
972       for (i = 0; i < w->var_cnt; i++)
973         {
974           struct sfm_var *v = &w->vars[i];
975
976           memset(bounce_cur, ' ', v->flt64_cnt * sizeof (flt64));
977
978           if (v->width == 0)
979             {
980               *bounce_cur = case_num_idx (c, v->fv);
981               bounce_cur += v->flt64_cnt;
982             }
983           else
984             { int ofs = 0;
985             while (ofs < v->width)
986               {
987                 int chunk = MIN (MIN_VERY_LONG_STRING - 1, v->width - ofs);
988                 int nv = DIV_RND_UP (chunk, sizeof (flt64));
989                 buf_copy_rpad ((char *) bounce_cur, nv * sizeof (flt64),
990                                case_data_idx (c, v->fv)->s + ofs, chunk);
991                 bounce_cur += nv;
992                 ofs += chunk;
993               }
994             }
995
996         }
997
998       if (!w->compress)
999         buf_write (w, bounce, bounce_size);
1000       else
1001         write_compressed_data (w, bounce);
1002
1003       local_free (bounce);
1004     }
1005
1006   case_destroy (c);
1007 }
1008
1009 static void
1010 sys_file_casewriter_destroy (struct casewriter *writer, void *w_)
1011 {
1012   struct sfm_writer *w = w_;
1013   if (!close_writer (w))
1014     casewriter_force_error (writer);
1015 }
1016
1017 static void
1018 put_instruction (struct sfm_writer *w, unsigned char instruction)
1019 {
1020   if (w->x >= w->y)
1021     {
1022       ensure_buf_space (w);
1023       w->x = (unsigned char *) w->ptr++;
1024       w->y = (unsigned char *) w->ptr;
1025     }
1026   *w->x++ = instruction;
1027 }
1028
1029 static void
1030 put_element (struct sfm_writer *w, const flt64 *elem)
1031 {
1032   ensure_buf_space (w);
1033   memcpy (w->ptr++, elem, sizeof *elem);
1034 }
1035
1036 static void
1037 write_compressed_data (struct sfm_writer *w, const flt64 *elem)
1038 {
1039   size_t i;
1040
1041   for (i = 0; i < w->var_cnt; i++)
1042     {
1043       struct sfm_var *v = &w->vars[i];
1044
1045       if (v->width == 0)
1046         {
1047           if (*elem == -FLT64_MAX)
1048             put_instruction (w, 255);
1049           else if (*elem >= 1 - COMPRESSION_BIAS
1050                    && *elem <= 251 - COMPRESSION_BIAS
1051                    && *elem == (int) *elem)
1052             put_instruction (w, (int) *elem + COMPRESSION_BIAS);
1053           else
1054             {
1055               put_instruction (w, 253);
1056               put_element (w, elem);
1057             }
1058           elem++;
1059         }
1060       else
1061         {
1062           size_t j;
1063
1064           for (j = 0; j < v->flt64_cnt; j++, elem++)
1065             {
1066               if (!memcmp (elem, "        ", sizeof (flt64)))
1067                 put_instruction (w, 254);
1068               else
1069                 {
1070                   put_instruction (w, 253);
1071                   put_element (w, elem);
1072                 }
1073             }
1074         }
1075     }
1076 }
1077
1078 /* Returns true if an I/O error has occurred on WRITER, false otherwise. */
1079 bool
1080 write_error (const struct sfm_writer *writer)
1081 {
1082   return ferror (writer->file);
1083 }
1084
1085 /* Closes a system file after we're done with it.
1086    Returns true if successful, false if an I/O error occurred. */
1087 bool
1088 close_writer (struct sfm_writer *w)
1089 {
1090   bool ok;
1091
1092   if (w == NULL)
1093     return true;
1094
1095   ok = true;
1096   if (w->file != NULL)
1097     {
1098       /* Flush buffer. */
1099       if (w->buf != NULL && w->ptr > w->buf)
1100         {
1101           memset (w->x, 0, w->y - w->x);
1102           buf_write (w, w->buf, (w->ptr - w->buf) * sizeof *w->buf);
1103         }
1104       fflush (w->file);
1105
1106       ok = !write_error (w);
1107
1108       /* Seek back to the beginning and update the number of cases.
1109          This is just a courtesy to later readers, so there's no need
1110          to check return values or report errors. */
1111       if (ok && !fseek (w->file, offsetof (struct sysfile_header, case_cnt),
1112                         SEEK_SET))
1113         {
1114           int32_t case_cnt = w->case_cnt;
1115           fwrite (&case_cnt, sizeof case_cnt, 1, w->file);
1116           clearerr (w->file);
1117         }
1118
1119       if (fclose (w->file) == EOF)
1120         ok = false;
1121
1122       if (!ok)
1123         msg (ME, _("An I/O error occurred writing system file \"%s\"."),
1124              fh_get_file_name (w->fh));
1125     }
1126
1127   fh_close (w->fh, "system file", "we");
1128
1129   free (w->buf);
1130   free (w->vars);
1131   free (w);
1132
1133   return ok;
1134 }
1135 \f
1136 static struct casewriter_class sys_file_casewriter_class =
1137   {
1138     sys_file_casewriter_write,
1139     sys_file_casewriter_destroy,
1140     NULL,
1141   };