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., 51 Franklin Street, Fifth Floor, 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 *, 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. DICT will not be modified, except
72 to assign short names. */
74 pfm_open_writer (struct file_handle *fh, struct dictionary *dict)
76 struct pfm_writer *w = NULL;
79 if (!fh_open (fh, "portable file", "we"))
82 /* Open the physical disk file. */
83 w = xmalloc (sizeof *w);
85 w->file = fopen (handle_get_filename (fh), "wb");
90 /* Check that file create succeeded. */
93 msg (ME, _("An error occurred while opening \"%s\" for writing "
94 "as a portable file: %s."),
95 handle_get_filename (fh), strerror (errno));
100 w->var_cnt = dict_get_var_cnt (dict);
101 w->vars = xmalloc (sizeof *w->vars * w->var_cnt);
102 for (i = 0; i < w->var_cnt; i++)
104 const struct variable *dv = dict_get_var (dict, i);
105 struct pfm_var *pv = &w->vars[i];
106 pv->width = dv->width;
110 /* Write file header. */
111 if (!write_header (w)
112 || !write_version_data (w)
113 || !write_variables (w, dict)
114 || !write_value_labels (w, dict)
115 || !buf_write (w, "F", 1))
121 pfm_close_writer (w);
125 /* Write NBYTES starting at BUF to the portable file represented by
126 H. Break lines properly every 80 characters. */
128 buf_write (struct pfm_writer *w, const void *buf_, size_t nbytes)
130 const char *buf = buf_;
132 assert (buf != NULL);
133 while (nbytes + w->lc >= 80)
135 size_t n = 80 - w->lc;
137 if (n && fwrite (buf, n, 1, w->file) != 1)
140 if (fwrite ("\r\n", 2, 1, w->file) != 1)
148 if (nbytes && 1 != fwrite (buf, nbytes, 1, w->file))
155 msg (ME, _("%s: Writing portable file: %s."),
156 handle_get_filename (w->fh), strerror (errno));
160 /* Write D to the portable file as a floating-point field, and return
163 write_float (struct pfm_writer *w, double d)
178 if (d == fabs (SYSMIS) || d == HUGE_VAL)
179 return buf_write (w, "*.", 2);
181 /* Use GNU libgmp2 to convert D into base-30. */
185 mpf_init_set_d (f, d);
186 mantissa = mpf_get_str (NULL, &exponent, 30, 0, f);
189 for (cp = mantissa; *cp; cp++)
193 /* Choose standard or scientific notation. */
194 mantissa_len = (int) strlen (mantissa);
195 cp = buf = local_alloc (mantissa_len + 32);
198 if (mantissa_len == 0)
200 else if (exponent < -4 || exponent > (mp_exp_t) mantissa_len)
202 /* Scientific notation. */
205 cp = stpcpy (cp, &mantissa[1]);
206 cp = spprintf (cp, "%+ld", (long) (exponent - 1));
208 else if (exponent <= 0)
210 /* Standard notation, D <= 1. */
212 memset (cp, '0', -exponent);
214 cp = stpcpy (cp, mantissa);
218 /* Standard notation, D > 1. */
219 memcpy (cp, mantissa, exponent);
222 cp = stpcpy (cp, &mantissa[exponent]);
226 success = buf_write (w, buf, cp - buf);
232 /* Write N to the portable file as an integer field, and return success. */
234 write_int (struct pfm_writer *w, int n)
252 /* PORTME: character codes. */
256 *--bp = r - 10 + 'A';
265 return buf_write (w, bp, &buf[64] - bp);
268 /* Write S to the portable file as a string field. */
270 write_string (struct pfm_writer *w, const char *s)
272 size_t n = strlen (s);
273 return write_int (w, (int) n) && buf_write (w, s, n);
276 /* Write file header. */
278 write_header (struct pfm_writer *w)
284 for (i = 0; i < 5; i++)
285 if (!buf_write (w, "ASCII SPSS PORT FILE ", 40))
290 /* PORTME: Translation table from SPSS character code to this
291 computer's native character code (which is probably ASCII). */
292 static const unsigned char spss2ascii[256] =
294 "0000000000000000000000000000000000000000000000000000000000000000"
295 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
296 "<(+|&[]!$*);^-/|,%_>?`:$@'=\"000000~-0000123456789000-()0{}\\00000"
297 "0000000000000000000000000000000000000000000000000000000000000000"
300 if (!buf_write (w, spss2ascii, 256))
304 if (!buf_write (w, "SPSSPORT", 8))
310 /* Writes version, date, and identification records. */
312 write_version_data (struct pfm_writer *w)
314 if (!buf_write (w, "A", 1))
324 if ((time_t) -1 == time (&t))
326 tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_mon = tm.tm_year = 0;
331 tmp = localtime (&t);
333 sprintf (date_str, "%04d%02d%02d",
334 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday);
335 sprintf (time_str, "%02d%02d%02d", tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
336 if (!write_string (w, date_str) || !write_string (w, time_str))
340 /* Product identification. */
341 if (!buf_write (w, "1", 1) || !write_string (w, version))
344 /* Subproduct identification. */
345 if (!buf_write (w, "3", 1) || !write_string (w, host_system))
351 /* Write format F to file H, and return success. */
353 write_format (struct pfm_writer *w, struct fmt_spec *f)
355 return (write_int (w, formats[f->type].spss)
356 && write_int (w, f->w)
357 && write_int (w, f->d));
360 /* Write value V for variable VV to file H, and return success. */
362 write_value (struct pfm_writer *w, union value *v, struct variable *vv)
364 if (vv->type == NUMERIC)
365 return write_float (w, v->f);
367 return write_int (w, vv->width) && buf_write (w, v->s, vv->width);
370 /* Write variable records, and return success. */
372 write_variables (struct pfm_writer *w, struct dictionary *dict)
376 dict_assign_short_names (dict);
378 if (!buf_write (w, "4", 1) || !write_int (w, dict_get_var_cnt (dict))
379 || !write_int (w, 161))
382 for (i = 0; i < dict_get_var_cnt (dict); i++)
384 static const char *miss_types[MISSING_COUNT] =
386 "", "8", "88", "888", "B ", "9", "A", "B 8", "98", "A8",
392 struct variable *v = dict_get_var (dict, i);
394 if (!buf_write (w, "7", 1) || !write_int (w, v->width)
395 || !write_string (w, v->short_name)
396 || !write_format (w, &v->print) || !write_format (w, &v->write))
399 for (m = miss_types[v->miss_type], j = 0; j < (int) strlen (m); j++)
400 if ((m[j] != ' ' && !buf_write (w, &m[j], 1))
401 || !write_value (w, &v->missing[j], v))
404 if (v->label && (!buf_write (w, "C", 1) || !write_string (w, v->label)))
411 /* Write value labels to disk. FIXME: Inefficient. */
413 write_value_labels (struct pfm_writer *w, const struct dictionary *dict)
417 for (i = 0; i < dict_get_var_cnt (dict); i++)
419 struct val_labs_iterator *j;
420 struct variable *v = dict_get_var (dict, i);
423 if (!val_labs_count (v->val_labs))
426 if (!buf_write (w, "D", 1)
428 || !write_string (w, v->short_name)
429 || !write_int (w, val_labs_count (v->val_labs)))
432 for (vl = val_labs_first_sorted (v->val_labs, &j); vl != NULL;
433 vl = val_labs_next (v->val_labs, &j))
434 if (!write_value (w, &vl->value, v)
435 || !write_string (w, vl->label))
445 /* Writes case ELEM to the portable file represented by H. Returns
448 pfm_write_case (struct pfm_writer *w, struct ccase *c)
452 for (i = 0; i < w->var_cnt; i++)
454 struct pfm_var *v = &w->vars[i];
458 if (!write_float (w, case_num (c, v->fv)))
463 if (!write_int (w, v->width)
464 || !buf_write (w, case_str (c, v->fv), v->width))
472 /* Closes a portable file after we're done with it. */
474 pfm_close_writer (struct pfm_writer *w)
479 fh_close (w->fh, "portable file", "we");
489 memset (buf, 'Z', n);
490 buf_write (w, buf, n);
492 if (fclose (w->file) == EOF)
493 msg (ME, _("%s: Closing portable file: %s."),
494 handle_get_filename (w->fh), strerror (errno));