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
21 #include "pfm-write.h"
32 #include "dictionary.h"
34 #include "file-handle.h"
39 #include "value-labels.h"
43 #include "debug-print.h"
45 /* Portable file writer. */
48 struct file_handle *fh; /* File handle. */
49 FILE *file; /* File stream. */
51 int lc; /* Number of characters on this line so far. */
53 size_t var_cnt; /* Number of variables. */
54 struct pfm_var *vars; /* Variables. */
57 /* A variable to write to the portable file. */
60 int width; /* 0=numeric, otherwise string var width. */
61 int fv; /* Starting case index. */
64 static int buf_write (struct pfm_writer *, const void *, size_t);
65 static int write_header (struct pfm_writer *);
66 static int write_version_data (struct pfm_writer *);
67 static int write_variables (struct pfm_writer *, const struct dictionary *);
68 static int write_value_labels (struct pfm_writer *, const struct dictionary *);
70 /* Writes the dictionary DICT to portable file HANDLE. Returns
71 nonzero only if successful. */
73 pfm_open_writer (struct file_handle *fh, const struct dictionary *dict)
75 struct pfm_writer *w = NULL;
78 if (!fh_open (fh, "portable file", "we"))
81 /* Open the physical disk file. */
82 w = xmalloc (sizeof *w);
84 w->file = fopen (handle_get_filename (fh), "wb");
89 /* Check that file create succeeded. */
92 msg (ME, _("An error occurred while opening \"%s\" for writing "
93 "as a portable file: %s."),
94 handle_get_filename (fh), strerror (errno));
99 w->var_cnt = dict_get_var_cnt (dict);
100 w->vars = xmalloc (sizeof *w->vars * w->var_cnt);
101 for (i = 0; i < w->var_cnt; i++)
103 const struct variable *dv = dict_get_var (dict, i);
104 struct pfm_var *pv = &w->vars[i];
105 pv->width = dv->width;
109 /* Write file header. */
110 if (!write_header (w)
111 || !write_version_data (w)
112 || !write_variables (w, dict)
113 || !write_value_labels (w, dict)
114 || !buf_write (w, "F", 1))
120 pfm_close_writer (w);
124 /* Write NBYTES starting at BUF to the portable file represented by
125 H. Break lines properly every 80 characters. */
127 buf_write (struct pfm_writer *w, const void *buf_, size_t nbytes)
129 const char *buf = buf_;
131 assert (buf != NULL);
132 while (nbytes + w->lc >= 80)
134 size_t n = 80 - w->lc;
136 if (n && fwrite (buf, n, 1, w->file) != 1)
139 if (fwrite ("\r\n", 2, 1, w->file) != 1)
147 if (nbytes && 1 != fwrite (buf, nbytes, 1, w->file))
154 msg (ME, _("%s: Writing portable file: %s."),
155 handle_get_filename (w->fh), strerror (errno));
159 /* Write D to the portable file as a floating-point field, and return
162 write_float (struct pfm_writer *w, double d)
177 if (d == fabs (SYSMIS) || d == HUGE_VAL)
178 return buf_write (w, "*.", 2);
180 /* Use GNU libgmp2 to convert D into base-30. */
184 mpf_init_set_d (f, d);
185 mantissa = mpf_get_str (NULL, &exponent, 30, 0, f);
188 for (cp = mantissa; *cp; cp++)
192 /* Choose standard or scientific notation. */
193 mantissa_len = (int) strlen (mantissa);
194 cp = buf = local_alloc (mantissa_len + 32);
197 if (mantissa_len == 0)
199 else if (exponent < -4 || exponent > (mp_exp_t) mantissa_len)
201 /* Scientific notation. */
204 cp = stpcpy (cp, &mantissa[1]);
205 cp = spprintf (cp, "%+ld", (long) (exponent - 1));
207 else if (exponent <= 0)
209 /* Standard notation, D <= 1. */
211 memset (cp, '0', -exponent);
213 cp = stpcpy (cp, mantissa);
217 /* Standard notation, D > 1. */
218 memcpy (cp, mantissa, exponent);
221 cp = stpcpy (cp, &mantissa[exponent]);
225 success = buf_write (w, buf, cp - buf);
231 /* Write N to the portable file as an integer field, and return success. */
233 write_int (struct pfm_writer *w, int n)
251 /* PORTME: character codes. */
255 *--bp = r - 10 + 'A';
264 return buf_write (w, bp, &buf[64] - bp);
267 /* Write S to the portable file as a string field. */
269 write_string (struct pfm_writer *w, const char *s)
271 size_t n = strlen (s);
272 return write_int (w, (int) n) && buf_write (w, s, n);
275 /* Write file header. */
277 write_header (struct pfm_writer *w)
283 for (i = 0; i < 5; i++)
284 if (!buf_write (w, "ASCII SPSS PORT FILE ", 40))
289 /* PORTME: Translation table from SPSS character code to this
290 computer's native character code (which is probably ASCII). */
291 static const unsigned char spss2ascii[256] =
293 "0000000000000000000000000000000000000000000000000000000000000000"
294 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
295 "<(+|&[]!$*);^-/|,%_>?`:$@'=\"000000~-0000123456789000-()0{}\\00000"
296 "0000000000000000000000000000000000000000000000000000000000000000"
299 if (!buf_write (w, spss2ascii, 256))
303 if (!buf_write (w, "SPSSPORT", 8))
309 /* Writes version, date, and identification records. */
311 write_version_data (struct pfm_writer *w)
313 if (!buf_write (w, "A", 1))
323 if ((time_t) -1 == time (&t))
325 tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_mon = tm.tm_year = 0;
330 tmp = localtime (&t);
332 sprintf (date_str, "%04d%02d%02d",
333 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
334 sprintf (time_str, "%02d%02d%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
335 if (!write_string (w, date_str) || !write_string (w, time_str))
339 /* Product identification. */
340 if (!buf_write (w, "1", 1) || !write_string (w, version))
343 /* Subproduct identification. */
344 if (!buf_write (w, "3", 1) || !write_string (w, host_system))
350 /* Write format F to file H, and return success. */
352 write_format (struct pfm_writer *w, struct fmt_spec *f)
354 return (write_int (w, formats[f->type].spss)
355 && write_int (w, f->w)
356 && write_int (w, f->d));
359 /* Write value V for variable VV to file H, and return success. */
361 write_value (struct pfm_writer *w, union value *v, struct variable *vv)
363 if (vv->type == NUMERIC)
364 return write_float (w, v->f);
366 return write_int (w, vv->width) && buf_write (w, v->s, vv->width);
369 /* Write variable records, and return success. */
371 write_variables (struct pfm_writer *w, const struct dictionary *dict)
375 if (!buf_write (w, "4", 1) || !write_int (w, dict_get_var_cnt (dict))
376 || !write_int (w, 161))
379 for (i = 0; i < dict_get_var_cnt (dict); i++)
381 static const char *miss_types[MISSING_COUNT] =
383 "", "8", "88", "888", "B ", "9", "A", "B 8", "98", "A8",
389 struct variable *v = dict_get_var (dict, i);
391 if (!buf_write (w, "7", 1) || !write_int (w, v->width)
392 || !write_string (w, v->name)
393 || !write_format (w, &v->print) || !write_format (w, &v->write))
396 for (m = miss_types[v->miss_type], j = 0; j < (int) strlen (m); j++)
397 if ((m[j] != ' ' && !buf_write (w, &m[j], 1))
398 || !write_value (w, &v->missing[j], v))
401 if (v->label && (!buf_write (w, "C", 1) || !write_string (w, v->label)))
408 /* Write value labels to disk. FIXME: Inefficient. */
410 write_value_labels (struct pfm_writer *w, const struct dictionary *dict)
414 for (i = 0; i < dict_get_var_cnt (dict); i++)
416 struct val_labs_iterator *j;
417 struct variable *v = dict_get_var (dict, i);
420 if (!val_labs_count (v->val_labs))
423 if (!buf_write (w, "D", 1)
425 || !write_string (w, v->name)
426 || !write_int (w, val_labs_count (v->val_labs)))
429 for (vl = val_labs_first_sorted (v->val_labs, &j); vl != NULL;
430 vl = val_labs_next (v->val_labs, &j))
431 if (!write_value (w, &vl->value, v)
432 || !write_string (w, vl->label))
442 /* Writes case ELEM to the portable file represented by H. Returns
445 pfm_write_case (struct pfm_writer *w, struct ccase *c)
449 for (i = 0; i < w->var_cnt; i++)
451 struct pfm_var *v = &w->vars[i];
455 if (!write_float (w, case_num (c, v->fv)))
460 if (!write_int (w, v->width)
461 || !buf_write (w, case_str (c, v->fv), v->width))
469 /* Closes a portable file after we're done with it. */
471 pfm_close_writer (struct pfm_writer *w)
476 fh_close (w->fh, "portable file", "we");
486 memset (buf, 'Z', n);
487 buf_write (w, buf, n);
489 if (fclose (w->file) == EOF)
490 msg (ME, _("%s: Closing portable file: %s."),
491 handle_get_filename (w->fh), strerror (errno));