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"
37 #include "value-labels.h"
41 #include "debug-print.h"
43 /* pfm writer file_handle extension. */
46 FILE *file; /* Actual file. */
48 int lc; /* Number of characters on this line so far. */
50 int nvars; /* Number of variables. */
51 int *vars; /* Variable widths. */
54 static struct fh_ext_class pfm_w_class;
56 static int bufwrite (struct file_handle *h, const void *buf, size_t nbytes);
57 static int write_header (struct file_handle *h);
58 static int write_version_data (struct file_handle *h);
59 static int write_variables (struct file_handle *h, struct dictionary *d);
60 static int write_value_labels (struct file_handle *h, struct dictionary *d);
62 /* Writes the dictionary DICT to portable file HANDLE. Returns
63 nonzero only if successful. */
65 pfm_write_dictionary (struct file_handle *handle, struct dictionary *dict)
67 struct pfm_fhuser_ext *ext;
69 if (handle->class != NULL)
71 msg (ME, _("Cannot write file %s as portable file: already opened "
73 fh_handle_name (handle), handle->class->name);
77 msg (VM (1), _("%s: Opening portable-file handle %s for writing."),
78 fh_handle_filename (handle), fh_handle_name (handle));
80 /* Open the physical disk file. */
81 handle->class = &pfm_w_class;
82 handle->ext = ext = xmalloc (sizeof (struct pfm_fhuser_ext));
83 ext->file = fopen (handle->norm_fn, "wb");
85 if (ext->file == NULL)
87 msg (ME, _("An error occurred while opening \"%s\" for writing "
88 "as a portable file: %s."), handle->fn, strerror (errno));
97 ext->nvars = dict_get_var_cnt (dict);
98 ext->vars = xmalloc (sizeof *ext->vars * ext->nvars);
99 for (i = 0; i < ext->nvars; i++)
100 ext->vars[i] = dict_get_var (dict, i)->width;
103 /* Write the file header. */
104 if (!write_header (handle))
107 /* Write version data. */
108 if (!write_version_data (handle))
111 /* Write variables. */
112 if (!write_variables (handle, dict))
115 /* Write value labels. */
116 if (!write_value_labels (handle, dict))
119 /* Write beginning of data marker. */
120 if (!bufwrite (handle, "F", 1))
123 msg (VM (2), _("Wrote portable-file header successfully."));
128 msg (VM (1), _("Error writing portable-file header."));
131 handle->class = NULL;
136 /* Write NBYTES starting at BUF to the portable file represented by
137 H. Break lines properly every 80 characters. */
139 bufwrite (struct file_handle *h, const void *buf, size_t nbytes)
141 struct pfm_fhuser_ext *ext = h->ext;
143 assert (buf != NULL);
144 while (nbytes + ext->lc >= 80)
146 size_t n = 80 - ext->lc;
148 if (n && 1 != fwrite (buf, n, 1, ext->file))
151 /* PORTME: line ends. */
152 if (1 != fwrite ("\r\n", 2, 1, ext->file))
156 *((char **) &buf) += n;
160 if (nbytes && 1 != fwrite (buf, nbytes, 1, ext->file))
168 msg (ME, _("%s: Writing portable file: %s."), h->fn, strerror (errno));
172 /* Write D to the portable file as a floating-point field, and return
175 write_float (struct file_handle *h, double d)
190 if (d == fabs (SYSMIS) || d == HUGE_VAL)
191 return bufwrite (h, "*.", 2);
193 /* Use GNU libgmp2 to convert D into base-30. */
197 mpf_init_set_d (f, d);
198 mantissa = mpf_get_str (NULL, &exponent, 30, 0, f);
201 for (cp = mantissa; *cp; cp++)
205 /* Choose standard or scientific notation. */
206 mantissa_len = (int) strlen (mantissa);
207 cp = buf = local_alloc (mantissa_len + 32);
210 if (mantissa_len == 0)
212 else if (exponent < -4 || exponent > (mp_exp_t) mantissa_len)
214 /* Scientific notation. */
217 cp = stpcpy (cp, &mantissa[1]);
218 cp = spprintf (cp, "%+ld", (long) (exponent - 1));
220 else if (exponent <= 0)
222 /* Standard notation, D <= 1. */
224 memset (cp, '0', -exponent);
226 cp = stpcpy (cp, mantissa);
230 /* Standard notation, D > 1. */
231 memcpy (cp, mantissa, exponent);
234 cp = stpcpy (cp, &mantissa[exponent]);
238 success = bufwrite (h, buf, cp - buf);
244 /* Write N to the portable file as an integer field, and return success. */
246 write_int (struct file_handle *h, int n)
264 /* PORTME: character codes. */
268 *--bp = r - 10 + 'A';
277 return bufwrite (h, bp, &buf[64] - bp);
280 /* Write S to the portable file as a string field. */
282 write_string (struct file_handle *h, const char *s)
284 size_t n = strlen (s);
285 return write_int (h, (int) n) && bufwrite (h, s, n);
288 /* Write file header. */
290 write_header (struct file_handle *h)
296 for (i = 0; i < 5; i++)
297 if (!bufwrite (h, "ASCII SPSS PORT FILE ", 40))
302 /* PORTME: Translation table from SPSS character code to this
303 computer's native character code (which is probably ASCII). */
304 static const unsigned char spss2ascii[256] =
306 "0000000000000000000000000000000000000000000000000000000000000000"
307 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
308 "<(+|&[]!$*);^-/|,%_>?`:$@'=\"000000~-0000123456789000-()0{}\\00000"
309 "0000000000000000000000000000000000000000000000000000000000000000"
312 if (!bufwrite (h, spss2ascii, 256))
316 if (!bufwrite (h, "SPSSPORT", 8))
322 /* Writes version, date, and identification records. */
324 write_version_data (struct file_handle *h)
326 if (!bufwrite (h, "A", 1))
336 if ((time_t) -1 == time (&t))
338 tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_mon = tm.tm_year = 0;
343 tmp = localtime (&t);
345 sprintf (date_str, "%04d%02d%02d",
346 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
347 sprintf (time_str, "%02d%02d%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
348 if (!write_string (h, date_str) || !write_string (h, time_str))
352 /* Product identification. */
353 if (!bufwrite (h, "1", 1) || !write_string (h, version))
356 /* Subproduct identification. */
357 if (!bufwrite (h, "3", 1) || !write_string (h, host_system))
363 /* Write format F to file H, and return success. */
365 write_format (struct file_handle *h, struct fmt_spec *f)
367 return (write_int (h, formats[f->type].spss)
368 && write_int (h, f->w)
369 && write_int (h, f->d));
372 /* Write value V for variable VV to file H, and return success. */
374 write_value (struct file_handle *h, union value *v, struct variable *vv)
376 if (vv->type == NUMERIC)
377 return write_float (h, v->f);
379 return write_int (h, vv->width) && bufwrite (h, v->s, vv->width);
382 /* Write variable records, and return success. */
384 write_variables (struct file_handle *h, struct dictionary *dict)
388 if (!bufwrite (h, "4", 1) || !write_int (h, dict_get_var_cnt (dict))
389 || !write_int (h, 161))
392 for (i = 0; i < dict_get_var_cnt (dict); i++)
394 static const char *miss_types[MISSING_COUNT] =
396 "", "8", "88", "888", "B ", "9", "A", "B 8", "98", "A8",
402 struct variable *v = dict_get_var (dict, i);
404 if (!bufwrite (h, "7", 1) || !write_int (h, v->width)
405 || !write_string (h, v->name)
406 || !write_format (h, &v->print) || !write_format (h, &v->write))
409 for (m = miss_types[v->miss_type], j = 0; j < (int) strlen (m); j++)
410 if ((m[j] != ' ' && !bufwrite (h, &m[j], 1))
411 || !write_value (h, &v->missing[j], v))
414 if (v->label && (!bufwrite (h, "C", 1) || !write_string (h, v->label)))
421 /* Write value labels to disk. FIXME: Inefficient. */
423 write_value_labels (struct file_handle *h, struct dictionary *dict)
427 for (i = 0; i < dict_get_var_cnt (dict); i++)
429 struct val_labs_iterator *j;
430 struct variable *v = dict_get_var (dict, i);
433 if (!val_labs_count (v->val_labs))
436 if (!bufwrite (h, "D", 1)
438 || !write_string (h, v->name)
439 || !write_int (h, val_labs_count (v->val_labs)))
442 for (vl = val_labs_first_sorted (v->val_labs, &j); vl != NULL;
443 vl = val_labs_next (v->val_labs, &j))
444 if (!write_value (h, &vl->value, v)
445 || !write_string (h, vl->label))
455 /* Writes case ELEM to the portable file represented by H. Returns
458 pfm_write_case (struct file_handle *h, const union value *elem)
460 struct pfm_fhuser_ext *ext = h->ext;
464 for (i = 0; i < ext->nvars; i++)
466 const int width = ext->vars[i];
470 if (!write_float (h, elem[i].f))
475 if (!write_int (h, width) || !bufwrite (h, elem->c, width))
483 /* Closes a portable file after we're done with it. */
485 pfm_close (struct file_handle *h)
487 struct pfm_fhuser_ext *ext = h->ext;
492 int n = 80 - ext->lc;
496 memset (buf, 'Z', n);
497 bufwrite (h, buf, n);
500 if (EOF == fclose (ext->file))
501 msg (ME, _("%s: Closing portable file: %s."), h->fn, strerror (errno));
507 static struct fh_ext_class pfm_w_class =
510 N_("writing as a portable file"),