1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32 #include "file-handle.h"
40 #include "debug-print.h"
42 /* pfm writer file_handle extension. */
45 FILE *file; /* Actual file. */
47 int lc; /* Number of characters on this line so far. */
49 int nvars; /* Number of variables. */
50 int *vars; /* Variable widths. */
53 static struct fh_ext_class pfm_w_class;
55 static int bufwrite (struct file_handle *h, const void *buf, size_t nbytes);
56 static int write_header (struct file_handle *h);
57 static int write_version_data (struct file_handle *h);
58 static int write_variables (struct file_handle *h, struct dictionary *d);
59 static int write_value_labels (struct file_handle *h, struct dictionary *d);
61 /* Writes the dictionary DICT to portable file HANDLE. Returns
62 nonzero only if successful. */
64 pfm_write_dictionary (struct file_handle *handle, struct dictionary *dict)
66 struct pfm_fhuser_ext *ext;
68 if (handle->class != NULL)
70 msg (ME, _("Cannot write file %s as portable file: already opened "
72 fh_handle_name (handle), handle->class->name);
76 msg (VM (1), _("%s: Opening portable-file handle %s for writing."),
77 fh_handle_filename (handle), fh_handle_name (handle));
79 /* Open the physical disk file. */
80 handle->class = &pfm_w_class;
81 handle->ext = ext = xmalloc (sizeof (struct pfm_fhuser_ext));
82 ext->file = fopen (handle->norm_fn, "wb");
84 if (ext->file == NULL)
86 msg (ME, _("An error occurred while opening \"%s\" for writing "
87 "as a portable file: %s."), handle->fn, strerror (errno));
96 ext->nvars = dict->nvar;
97 ext->vars = xmalloc (sizeof *ext->vars * dict->nvar);
98 for (i = 0; i < dict->nvar; i++)
99 ext->vars[i] = dict->var[i]->width;
102 /* Write the file header. */
103 if (!write_header (handle))
106 /* Write version data. */
107 if (!write_version_data (handle))
110 /* Write variables. */
111 if (!write_variables (handle, dict))
114 /* Write value labels. */
115 if (!write_value_labels (handle, dict))
118 /* Write beginning of data marker. */
119 if (!bufwrite (handle, "F", 1))
122 msg (VM (2), _("Wrote portable-file header successfully."));
127 msg (VM (1), _("Error writing portable-file header."));
130 handle->class = NULL;
135 /* Write NBYTES starting at BUF to the portable file represented by
136 H. Break lines properly every 80 characters. */
138 bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
140 struct pfm_fhuser_ext *ext = h->ext;
142 assert (buf != NULL);
143 while (nbytes + ext->lc >= 80)
145 size_t n = 80 - ext->lc;
147 if (n && 1 != fwrite (buf, n, 1, ext->file))
150 /* PORTME: line ends. */
151 if (1 != fwrite ("\r\n", 2, 1, ext->file))
155 *((char **) &buf) += n;
159 if (nbytes && 1 != fwrite (buf, nbytes, 1, ext->file))
167 msg (ME, _("%s: Writing portable file: %s."), h->fn, strerror (errno));
171 /* Write D to the portable file as a floating-point field, and return
174 write_float (struct file_handle *h, double d)
189 if (d == fabs (SYSMIS) || d == HUGE_VAL)
190 return bufwrite (h, "*.", 2);
192 /* Use GNU libgmp2 to convert D into base-30. */
196 mpf_init_set_d (f, d);
197 mantissa = mpf_get_str (NULL, &exponent, 30, 0, f);
200 for (cp = mantissa; *cp; cp++)
204 /* Choose standard or scientific notation. */
205 mantissa_len = (int) strlen (mantissa);
206 cp = buf = local_alloc (mantissa_len + 32);
209 if (mantissa_len == 0)
211 else if (exponent < -4 || exponent > (mp_exp_t) mantissa_len)
213 /* Scientific notation. */
216 cp = stpcpy (cp, &mantissa[1]);
217 cp = spprintf (cp, "%+ld", (long) (exponent - 1));
219 else if (exponent <= 0)
221 /* Standard notation, D <= 1. */
223 memset (cp, '0', -exponent);
225 cp = stpcpy (cp, mantissa);
229 /* Standard notation, D > 1. */
230 memcpy (cp, mantissa, exponent);
233 cp = stpcpy (cp, &mantissa[exponent]);
237 success = bufwrite (h, buf, cp - buf);
243 /* Write N to the portable file as an integer field, and return success. */
245 write_int (struct file_handle *h, int n)
263 /* PORTME: character codes. */
267 *--bp = r - 10 + 'A';
276 return bufwrite (h, bp, &buf[64] - bp);
279 /* Write S to the portable file as a string field. */
281 write_string (struct file_handle *h, const char *s)
283 size_t n = strlen (s);
284 return write_int (h, (int) n) && bufwrite (h, s, n);
287 /* Write file header. */
289 write_header (struct file_handle *h)
295 for (i = 0; i < 5; i++)
296 if (!bufwrite (h, "ASCII SPSS PORT FILE ", 40))
301 /* PORTME: Translation table from SPSS character code to this
302 computer's native character code (which is probably ASCII). */
303 static const unsigned char spss2ascii[256] =
305 "0000000000000000000000000000000000000000000000000000000000000000"
306 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
307 "<(+|&[]!$*);^-/|,%_>?`:$@'=\"000000~-0000123456789000-()0{}\\00000"
308 "0000000000000000000000000000000000000000000000000000000000000000"
311 if (!bufwrite (h, spss2ascii, 256))
315 if (!bufwrite (h, "SPSSPORT", 8))
321 /* Writes version, date, and identification records. */
323 write_version_data (struct file_handle *h)
325 if (!bufwrite (h, "A", 1))
335 if ((time_t) -1 == time (&t))
337 tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_mon = tm.tm_year = 0;
342 tmp = localtime (&t);
344 sprintf (date_str, "%04d%02d%02d",
345 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
346 sprintf (time_str, "%02d%02d%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
347 if (!write_string (h, date_str) || !write_string (h, time_str))
351 /* Product identification. */
352 if (!bufwrite (h, "1", 1) || !write_string (h, version))
355 /* Subproduct identification. */
356 if (!bufwrite (h, "3", 1) || !write_string (h, host_system))
362 /* Write format F to file H, and return success. */
364 write_format (struct file_handle *h, struct fmt_spec *f)
366 return (write_int (h, formats[f->type].spss)
367 && write_int (h, f->w)
368 && write_int (h, f->d));
371 /* Write value V for variable VV to file H, and return success. */
373 write_value (struct file_handle *h, union value *v, struct variable *vv)
375 if (vv->type == NUMERIC)
376 return write_float (h, v->f);
378 return write_int (h, vv->width) && bufwrite (h, v->s, vv->width);
381 /* Write variable records, and return success. */
383 write_variables (struct file_handle *h, struct dictionary *dict)
387 if (!bufwrite (h, "4", 1) || !write_int (h, dict->nvar)
388 || !write_int (h, 161))
391 for (i = 0; i < dict->nvar; i++)
393 static const char *miss_types[MISSING_COUNT] =
395 "", "8", "88", "888", "B ", "9", "A", "B 8", "98", "A8",
401 struct variable *v = dict->var[i];
403 if (!bufwrite (h, "7", 1) || !write_int (h, v->width)
404 || !write_string (h, v->name)
405 || !write_format (h, &v->print) || !write_format (h, &v->write))
408 for (m = miss_types[v->miss_type], j = 0; j < (int) strlen (m); j++)
409 if ((m[j] != ' ' && !bufwrite (h, &m[j], 1))
410 || !write_value (h, &v->missing[j], v))
413 if (v->label && (!bufwrite (h, "C", 1) || !write_string (h, v->label)))
420 /* Write value labels to disk. FIXME: Inefficient. */
422 write_value_labels (struct file_handle *h, struct dictionary *dict)
426 for (i = 0; i < dict->nvar; i++)
429 struct variable *v = dict->var[i];
430 struct value_label *vl;
432 if (v->val_lab == NULL)
435 if (!bufwrite (h, "D", 1)
437 || !write_string (h, v->name)
438 || !write_int (h, avl_count (v->val_lab)))
441 avl_traverser_init (iter);
442 while (NULL != (vl = avl_traverse (v->val_lab, &iter)))
443 if (!write_value (h, &vl->v, v)
444 || !write_string (h, vl->s))
451 /* Writes case ELEM to the portable file represented by H. Returns
454 pfm_write_case (struct file_handle *h, const union value *elem)
456 struct pfm_fhuser_ext *ext = h->ext;
460 for (i = 0; i < ext->nvars; i++)
462 const int width = ext->vars[i];
466 if (!write_float (h, elem[i].f))
471 if (!write_int (h, width) || !bufwrite (h, elem->c, width))
479 /* Closes a portable file after we're done with it. */
481 pfm_close (struct file_handle *h)
483 struct pfm_fhuser_ext *ext = h->ext;
488 int n = 80 - ext->lc;
492 memset (buf, 'Z', n);
493 bufwrite (h, buf, n);
496 if (EOF == fclose (ext->file))
497 msg (ME, _("%s: Closing portable file: %s."), h->fn, strerror (errno));
503 static struct fh_ext_class pfm_w_class =
506 N_("writing as a portable file"),