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"
41 /*#define DEBUGGING 1 */
42 #include "debug-print.h"
44 /* pfm writer file_handle extension. */
47 FILE *file; /* Actual file. */
49 int lc; /* Number of characters on this line so far. */
51 int nvars; /* Number of variables. */
52 int *vars; /* Variable widths. */
55 static struct fh_ext_class pfm_w_class;
57 static int bufwrite (struct file_handle *h, const void *buf, size_t nbytes);
58 static int write_header (struct file_handle *h);
59 static int write_version_data (struct file_handle *h);
60 static int write_variables (struct file_handle *h, struct dictionary *d);
61 static int write_value_labels (struct file_handle *h, struct dictionary *d);
63 /* Writes the dictionary DICT to portable file HANDLE. Returns
64 nonzero only if successful. */
66 pfm_write_dictionary (struct file_handle *handle, struct dictionary *dict)
68 struct pfm_fhuser_ext *ext;
70 if (handle->class != NULL)
72 msg (ME, _("Cannot write file %s as portable file: already opened "
74 fh_handle_name (handle), handle->class->name);
78 msg (VM (1), _("%s: Opening portable-file handle %s for writing."),
79 fh_handle_filename (handle), fh_handle_name (handle));
81 /* Open the physical disk file. */
82 handle->class = &pfm_w_class;
83 handle->ext = ext = xmalloc (sizeof (struct pfm_fhuser_ext));
84 ext->file = fopen (handle->norm_fn, "wb");
86 if (ext->file == NULL)
88 msg (ME, _("An error occurred while opening \"%s\" for writing "
89 "as a portable file: %s."), handle->fn, strerror (errno));
98 ext->nvars = dict->nvar;
99 ext->vars = xmalloc (sizeof *ext->vars * dict->nvar);
100 for (i = 0; i < dict->nvar; i++)
101 ext->vars[i] = dict->var[i]->width;
104 /* Write the file header. */
105 if (!write_header (handle))
108 /* Write version data. */
109 if (!write_version_data (handle))
112 /* Write variables. */
113 if (!write_variables (handle, dict))
116 /* Write value labels. */
117 if (!write_value_labels (handle, dict))
120 /* Write beginning of data marker. */
121 if (!bufwrite (handle, "F", 1))
124 msg (VM (2), _("Wrote portable-file header successfully."));
129 msg (VM (1), _("Error writing portable-file header."));
132 handle->class = NULL;
137 /* Write NBYTES starting at BUF to the portable file represented by
138 H. Break lines properly every 80 characters. */
140 bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
142 struct pfm_fhuser_ext *ext = h->ext;
144 assert (buf != NULL);
145 while (nbytes + ext->lc >= 80)
147 size_t n = 80 - ext->lc;
149 if (n && 1 != fwrite (buf, n, 1, ext->file))
152 /* PORTME: line ends. */
153 if (1 != fwrite ("\r\n", 2, 1, ext->file))
157 *((char **) &buf) += n;
161 if (nbytes && 1 != fwrite (buf, nbytes, 1, ext->file))
169 msg (ME, _("%s: Writing portable file: %s."), h->fn, strerror (errno));
173 /* Write D to the portable file as a floating-point field, and return
176 write_float (struct file_handle *h, double d)
191 if (d == fabs (SYSMIS) || d == HUGE_VAL)
192 return bufwrite (h, "*.", 2);
194 /* Use GNU libgmp2 to convert D into base-30. */
198 mpf_init_set_d (f, d);
199 mantissa = mpf_get_str (NULL, &exponent, 30, 0, f);
202 for (cp = mantissa; *cp; cp++)
206 /* Choose standard or scientific notation. */
207 mantissa_len = (int) strlen (mantissa);
208 cp = buf = local_alloc (mantissa_len + 32);
211 if (mantissa_len == 0)
213 else if (exponent < -4 || exponent > (mp_exp_t) mantissa_len)
215 /* Scientific notation. */
218 cp = stpcpy (cp, &mantissa[1]);
219 cp = spprintf (cp, "%+ld", (long) (exponent - 1));
221 else if (exponent <= 0)
223 /* Standard notation, D <= 1. */
225 memset (cp, '0', -exponent);
227 cp = stpcpy (cp, mantissa);
231 /* Standard notation, D > 1. */
232 memcpy (cp, mantissa, exponent);
235 cp = stpcpy (cp, &mantissa[exponent]);
239 success = bufwrite (h, buf, cp - buf);
245 /* Write N to the portable file as an integer field, and return success. */
247 write_int (struct file_handle *h, int n)
265 /* PORTME: character codes. */
269 *--bp = r - 10 + 'A';
278 return bufwrite (h, bp, &buf[64] - bp);
281 /* Write S to the portable file as a string field. */
283 write_string (struct file_handle *h, const char *s)
285 size_t n = strlen (s);
286 return write_int (h, (int) n) && bufwrite (h, s, n);
289 /* Write file header. */
291 write_header (struct file_handle *h)
297 for (i = 0; i < 5; i++)
298 if (!bufwrite (h, "ASCII SPSS PORT FILE ", 40))
303 /* PORTME: Translation table from SPSS character code to this
304 computer's native character code (which is probably ASCII). */
305 static const unsigned char spss2ascii[256] =
307 "0000000000000000000000000000000000000000000000000000000000000000"
308 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
309 "<(+|&[]!$*);^-/|,%_>?`:$@'=\"000000~-0000123456789000-()0{}\\00000"
310 "0000000000000000000000000000000000000000000000000000000000000000"
313 if (!bufwrite (h, spss2ascii, 256))
317 if (!bufwrite (h, "SPSSPORT", 8))
323 /* Writes version, date, and identification records. */
325 write_version_data (struct file_handle *h)
327 if (!bufwrite (h, "A", 1))
337 if ((time_t) -1 == time (&t))
339 tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_mon = tm.tm_year = 0;
344 tmp = localtime (&t);
346 sprintf (date_str, "%04d%02d%02d",
347 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
348 sprintf (time_str, "%02d%02d%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
349 if (!write_string (h, date_str) || !write_string (h, time_str))
353 /* Product identification. */
354 if (!bufwrite (h, "1", 1) || !write_string (h, version))
357 /* Subproduct identification. */
358 if (!bufwrite (h, "3", 1) || !write_string (h, host_system))
364 /* Write format F to file H, and return success. */
366 write_format (struct file_handle *h, struct fmt_spec *f)
368 return (write_int (h, formats[f->type].spss)
369 && write_int (h, f->w)
370 && write_int (h, f->d));
373 /* Write value V for variable VV to file H, and return success. */
375 write_value (struct file_handle *h, union value *v, struct variable *vv)
377 if (vv->type == NUMERIC)
378 return write_float (h, v->f);
380 return write_int (h, vv->width) && bufwrite (h, v->s, vv->width);
383 /* Write variable records, and return success. */
385 write_variables (struct file_handle *h, struct dictionary *dict)
389 if (!bufwrite (h, "4", 1) || !write_int (h, dict->nvar)
390 || !write_int (h, 161))
393 for (i = 0; i < dict->nvar; i++)
395 static const char *miss_types[MISSING_COUNT] =
397 "", "8", "88", "888", "B ", "9", "A", "B 8", "98", "A8",
403 struct variable *v = dict->var[i];
405 if (!bufwrite (h, "7", 1) || !write_int (h, v->width)
406 || !write_string (h, v->name)
407 || !write_format (h, &v->print) || !write_format (h, &v->write))
410 for (m = miss_types[v->miss_type], j = 0; j < (int) strlen (m); j++)
411 if ((m[j] != ' ' && !bufwrite (h, &m[j], 1))
412 || !write_value (h, &v->missing[j], v))
415 if (v->label && (!bufwrite (h, "C", 1) || !write_string (h, v->label)))
422 /* Write value labels to disk. FIXME: Inefficient. */
424 write_value_labels (struct file_handle *h, struct dictionary *dict)
428 for (i = 0; i < dict->nvar; i++)
431 struct variable *v = dict->var[i];
432 struct value_label *vl;
434 if (v->val_lab == NULL)
437 if (!bufwrite (h, "D", 1)
439 || !write_string (h, v->name)
440 || !write_int (h, avl_count (v->val_lab)))
443 avl_traverser_init (iter);
444 while (NULL != (vl = avl_traverse (v->val_lab, &iter)))
445 if (!write_value (h, &vl->v, v)
446 || !write_string (h, vl->s))
453 /* Writes case ELEM to the portable file represented by H. Returns
456 pfm_write_case (struct file_handle *h, const union value *elem)
458 struct pfm_fhuser_ext *ext = h->ext;
462 for (i = 0; i < ext->nvars; i++)
464 const int width = ext->vars[i];
468 if (!write_float (h, elem[i].f))
473 if (!write_int (h, width) || !bufwrite (h, elem->c, width))
481 /* Closes a portable file after we're done with it. */
483 pfm_close (struct file_handle *h)
485 struct pfm_fhuser_ext *ext = h->ext;
490 int n = 80 - ext->lc;
494 memset (buf, 'Z', n);
495 bufwrite (h, buf, n);
498 if (EOF == fclose (ext->file))
499 msg (ME, _("%s: Closing portable file: %s."), h->fn, strerror (errno));
505 static struct fh_ext_class pfm_w_class =
508 N_("writing as a portable file"),