#include <ctype.h>
#include <errno.h>
#include <math.h>
+#include <setjmp.h>
#include "alloc.h"
+#include "bool.h"
#include "case.h"
#include "dictionary.h"
#include "file-handle.h"
#include "hash.h"
#include "magic.h"
#include "misc.h"
+#include "pool.h"
#include "str.h"
#include "value-labels.h"
#include "var.h"
/* Portable file reader. */
struct pfm_reader
{
- struct file_handle *fh; /* File handle. */
- FILE *file; /* File stream. */
+ struct pool *pool; /* All the portable file state. */
- int weight_index; /* 0-based index of weight variable, or -1. */
+ jmp_buf bail_out; /* longjmp() target for error handling. */
+ struct file_handle *fh; /* File handle. */
+ FILE *file; /* File stream. */
+ char cc; /* Current character. */
unsigned char *trans; /* 256-byte character set translation table. */
int var_cnt; /* Number of variables. */
+ int weight_index; /* 0-based index of weight variable, or -1. */
int *widths; /* Variable widths, 0 for numeric. */
int value_cnt; /* Number of `value's per case. */
-
- unsigned char buf[83]; /* Input buffer. */
- unsigned char *bp; /* Buffer pointer. */
- int cc; /* Current character. */
};
-static int
-corrupt_msg (struct pfm_reader *r, const char *format,...)
+static void
+error (struct pfm_reader *r, const char *msg,...)
PRINTF_FORMAT (2, 3);
-/* Displays a corruption error. */
-static int
-corrupt_msg (struct pfm_reader *r, const char *format, ...)
+/* Displays MSG as an error message and aborts reading the
+ portable file via longjmp(). */
+static void
+error (struct pfm_reader *r, const char *msg, ...)
{
- char *title;
struct error e;
const char *filename;
+ char *title;
va_list args;
e.class = ME;
getl_location (&e.where.filename, &e.where.line_number);
filename = handle_get_filename (r->fh);
- e.title = title = local_alloc (strlen (filename) + 80);
- sprintf (title, _("portable file %s corrupt at offset %ld: "),
- filename, ftell (r->file) - (82 - (long) (r->bp - r->buf)));
+ e.title = title = pool_alloc (r->pool, strlen (filename) + 80);
+ sprintf (e.title, _("portable file %s corrupt at offset %ld: "),
+ filename, ftell (r->file));
- va_start (args, format);
- err_vmsg (&e, format, args);
+ va_start (args, msg);
+ err_vmsg (&e, msg, args);
va_end (args);
- local_free (title);
-
- return 0;
+ longjmp (r->bail_out, 1);
}
-static unsigned char * read_string (struct pfm_reader *r);
-
-/* Closes a portable file after we're done with it. */
+/* Closes portable file reader R, after we're done with it. */
void
pfm_close_reader (struct pfm_reader *r)
{
- if (r == NULL)
- return;
-
- read_string (NULL);
-
- if (r->fh != NULL)
- fh_close (r->fh, "portable file", "rs");
- if (fclose (r->file) == EOF)
- msg (ME, _("%s: Closing portable file: %s."),
- handle_get_filename (r->fh), strerror (errno));
- free (r->trans);
- free (r->widths);
- free (r);
+ if (r != NULL)
+ pool_destroy (r->pool);
}
-/* Displays the message X with corrupt_msg, then jumps to the error
- label. */
-#define lose(X) \
- do { \
- corrupt_msg X; \
- goto error; \
- } while (0)
-
-/* Read an 80-character line into handle H's buffer. Return
- success. */
-static int
-fill_buf (struct pfm_reader *r)
+/* Read a single character into cur_char. */
+static void
+advance (struct pfm_reader *r)
{
- if (80 != fread (r->buf, 1, 80, r->file))
- lose ((r, _("Unexpected end of file.")));
+ int c;
- /* PORTME: line ends. */
- {
- int c;
-
- c = getc (r->file);
- if (c != '\n' && c != '\r')
- lose ((r, _("Bad line end.")));
-
- c = getc (r->file);
- if (c != '\n' && c != '\r')
- ungetc (c, r->file);
- }
-
- if (r->trans)
- {
- int i;
-
- for (i = 0; i < 80; i++)
- r->buf[i] = r->trans[r->buf[i]];
- }
-
- r->bp = r->buf;
-
- return 1;
-
- error:
- return 0;
-}
+ while ((c = getc (r->file)) == '\r' || c == '\n')
+ continue;
+ if (c == EOF)
+ error (r, _("unexpected end of file"));
-/* Read a single character into cur_char. Return success; */
-static int
-read_char (struct pfm_reader *r)
-{
- if (r->bp >= &r->buf[80] && !fill_buf (r))
- return 0;
- r->cc = *r->bp++;
- return 1;
+ if (r->trans != NULL)
+ c = r->trans[c];
+ r->cc = c;
}
-/* Advance a single character. */
-#define advance() \
- do { \
- if (!read_char (r)) \
- goto error; \
- } while (0)
-
/* Skip a single character if present, and return whether it was
skipped. */
-static inline int
-skip_char (struct pfm_reader *r, int c)
+static inline bool
+match (struct pfm_reader *r, int c)
{
if (r->cc == c)
{
- advance ();
- return 1;
+ advance (r);
+ return true;
}
- error:
- return 0;
+ else
+ return false;
}
-/* Skip a single character if present, and return whether it was
- skipped. */
-#define match(C) skip_char (r, C)
-
-static int read_header (struct pfm_reader *);
-static int read_version_data (struct pfm_reader *, struct pfm_read_info *);
-static int read_variables (struct pfm_reader *, struct dictionary *);
-static int read_value_label (struct pfm_reader *, struct dictionary *);
+static void read_header (struct pfm_reader *);
+static void read_version_data (struct pfm_reader *, struct pfm_read_info *);
+static void read_variables (struct pfm_reader *, struct dictionary *);
+static void read_value_label (struct pfm_reader *, struct dictionary *);
void dump_dictionary (struct dictionary *);
/* Reads the dictionary from file with handle H, and returns it in a
pfm_open_reader (struct file_handle *fh, struct dictionary **dict,
struct pfm_read_info *info)
{
- struct pfm_reader *r = NULL;
+ struct pool *volatile pool = NULL;
+ struct pfm_reader *volatile r = NULL;
*dict = dict_create ();
if (!fh_open (fh, "portable file", "rs"))
goto error;
/* Create and initialize reader. */
- r = xmalloc (sizeof *r);
+ pool = pool_create ();
+ r = pool_alloc (pool, sizeof *r);
+ r->pool = pool;
+ if (setjmp (r->bail_out))
+ goto error;
r->fh = fh;
- r->file = fopen (handle_get_filename (r->fh), "rb");
+ r->file = pool_fopen (r->pool, handle_get_filename (r->fh), "rb");
r->weight_index = -1;
r->trans = NULL;
r->var_cnt = 0;
r->widths = NULL;
r->value_cnt = 0;
- r->bp = NULL;
/* Check that file open succeeded, prime reading. */
if (r->file == NULL)
err_cond_fail ();
goto error;
}
- if (!fill_buf (r))
- goto error;
- advance ();
-
+
/* Read header, version, date info, product id, variables. */
- if (!read_header (r)
- || !read_version_data (r, info)
- || !read_variables (r, *dict))
- goto error;
+ read_header (r);
+ read_version_data (r, info);
+ read_variables (r, *dict);
/* Read value labels. */
- while (match (77 /* D */))
- if (!read_value_label (r, *dict))
- goto error;
+ while (match (r, 'D'))
+ read_value_label (r, *dict);
/* Check that we've made it to the data. */
- if (!match (79 /* F */))
- lose ((r, _("Data record expected.")));
+ if (!match (r, 'F'))
+ error (r, _("Data record expected."));
return r;
return NULL;
}
\f
-/* Read a floating point value and return its value, or
- second_lowest_value on error. */
+/* Returns the value of base-30 digit C,
+ or -1 if C is not a base-30 digit. */
+static int
+base_30_value (unsigned char c)
+{
+ static const char base_30_digits[] = "0123456789ABCDEFGHIJKLMNOPQRST";
+ const char *p = strchr (base_30_digits, c);
+ return p != NULL ? p - base_30_digits : -1;
+}
+
+/* Read a floating point value and return its value. */
static double
read_float (struct pfm_reader *r)
{
double num = 0.;
- int got_dot = 0;
- int got_digit = 0;
int exponent = 0;
- int neg = 0;
+ bool got_dot = false; /* Seen a decimal point? */
+ bool got_digit = false; /* Seen any digits? */
+ bool negative = false; /* Number is negative? */
/* Skip leading spaces. */
- while (match (126 /* space */))
- ;
+ while (match (r, ' '))
+ continue;
- if (match (137 /* * */))
+ /* `*' indicates system-missing. */
+ if (match (r, '*'))
{
- advance (); /* Probably a dot (.) but doesn't appear to matter. */
+ advance (r); /* Probably a dot (.) but doesn't appear to matter. */
return SYSMIS;
}
- else if (match (141 /* - */))
- neg = 1;
+ negative = match (r, '-');
for (;;)
{
- if (r->cc >= 64 /* 0 */ && r->cc <= 93 /* T */)
+ int digit = base_30_value (r->cc);
+ if (digit != -1)
{
- got_digit++;
+ got_digit = true;
/* Make sure that multiplication by 30 will not overflow. */
if (num > DBL_MAX * (1. / 30.))
digit so that we can multiply by 10 later. */
++exponent;
else
- num = (num * 30.0) + (r->cc - 64);
+ num = (num * 30.0) + digit;
/* Keep track of the number of digits after the decimal point.
If we just divided by 30 here, we would lose precision. */
if (got_dot)
--exponent;
}
- else if (!got_dot && r->cc == 127 /* . */)
+ else if (!got_dot && r->cc == '.')
/* Record that we have found the decimal point. */
got_dot = 1;
else
/* Any other character terminates the number. */
break;
- advance ();
+ advance (r);
}
+ /* Check that we had some digits. */
if (!got_digit)
- lose ((r, "Number expected."));
-
- if (r->cc == 130 /* + */ || r->cc == 141 /* - */)
+ error (r, "Number expected.");
+
+ /* Get exponent if any. */
+ if (r->cc == '+' || r->cc == '-')
{
- /* Get the exponent. */
long int exp = 0;
- int neg_exp = r->cc == 141 /* - */;
+ bool negative_exponent = r->cc == '-';
+ int digit;
- for (;;)
+ for (advance (r); (digit = base_30_value (r->cc)) != -1; advance (r))
{
- advance ();
-
- if (r->cc < 64 /* 0 */ || r->cc > 93 /* T */)
- break;
-
if (exp > LONG_MAX / 30)
- goto overflow;
- exp = exp * 30 + (r->cc - 64);
+ {
+ exp = LONG_MAX;
+ break;
+ }
+ exp = exp * 30 + digit;
}
/* We don't check whether there were actually any digits, but we
probably should. */
- if (neg_exp)
+ if (negative_exponent)
exp = -exp;
exponent += exp;
}
-
- if (!match (142 /* / */))
- lose ((r, _("Missing numeric terminator.")));
- /* Multiply NUM by 30 to the EXPONENT power, checking for overflow. */
+ /* Numbers must end with `/'. */
+ if (!match (r, '/'))
+ error (r, _("Missing numeric terminator."));
+ /* Multiply `num' by 30 to the `exponent' power, checking for
+ overflow. */
if (exponent < 0)
num *= pow (30.0, (double) exponent);
else if (exponent > 0)
{
if (num > DBL_MAX * pow (30.0, (double) -exponent))
- goto overflow;
- num *= pow (30.0, (double) exponent);
+ num = DBL_MAX;
+ else
+ num *= pow (30.0, (double) exponent);
}
- if (neg)
- return -num;
- else
- return num;
-
- overflow:
- if (neg)
- return -DBL_MAX / 10.;
- else
- return DBL_MAX / 10;
-
- error:
- return second_lowest_value;
+ return negative ? -num : num;
}
-/* Read an integer and return its value, or NOT_INT on failure. */
+/* Read an integer and return its value. */
static int
read_int (struct pfm_reader *r)
{
double f = read_float (r);
-
- if (f == second_lowest_value)
- goto error;
if (floor (f) != f || f >= INT_MAX || f <= INT_MIN)
- lose ((r, _("Bad integer format.")));
+ error (r, _("Invalid integer."));
return f;
-
- error:
- return NOT_INT;
}
-/* Reads a string and returns its value in a static buffer, or NULL on
- failure. The buffer can be deallocated by calling with a NULL
- argument. */
-static unsigned char *
-read_string (struct pfm_reader *r)
+/* Reads a string into BUF, which must have room for 256
+ characters. */
+static void
+read_string (struct pfm_reader *r, char *buf)
{
- static char *buf;
- int n;
+ int n = read_int (r);
+ if (n < 0 || n > 255)
+ error (r, _("Bad string length %d."), n);
- if (r == NULL)
+ while (n-- > 0)
{
- free (buf);
- buf = NULL;
- return NULL;
+ *buf++ = r->cc;
+ advance (r);
}
- else if (buf == NULL)
- buf = xmalloc (256);
-
- n = read_int (r);
- if (n == NOT_INT)
- return NULL;
- if (n < 0 || n > 255)
- lose ((r, _("Bad string length %d."), n));
-
- {
- int i;
-
- for (i = 0; i < n; i++)
- {
- buf[i] = r->cc;
- advance ();
- }
- }
-
- buf[n] = 0;
- return buf;
+ *buf = '\0';
+}
- error:
- return NULL;
+/* Reads a string and returns a copy of it allocated from R's
+ pool. */
+static unsigned char *
+read_pool_string (struct pfm_reader *r)
+{
+ char string[256];
+ read_string (r, string);
+ return pool_strdup (r->pool, string);
}
\f
/* Reads the 464-byte file header. */
-int
+static void
read_header (struct pfm_reader *r)
{
- /* For now at least, just ignore the vanity splash strings. */
- {
- int i;
+ /* portable_to_local[PORTABLE] translates the given portable
+ character into the local character set. */
+ static const unsigned char portable_to_local[256] =
+ {
+ " "
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
+ "<(+|&[]!$*);^-/|,%_>?`:$@'=\" ~- 0123456789 -() {}\\ "
+ " "
+ };
- for (i = 0; i < 200; i++)
- advance ();
- }
-
- {
- unsigned char src[256];
- int trans_temp[256];
- int i;
-
- for (i = 0; i < 256; i++)
- {
- src[i] = (unsigned char) r->cc;
- advance ();
- }
-
- for (i = 0; i < 256; i++)
- trans_temp[i] = -1;
-
- /* 0 is used to mark untranslatable characters, so we have to mark
- it specially. */
- trans_temp[src[64]] = 64;
- for (i = 0; i < 256; i++)
- if (trans_temp[src[i]] == -1)
- trans_temp[src[i]] = i;
-
- r->trans = xmalloc (256);
- for (i = 0; i < 256; i++)
- r->trans[i] = trans_temp[i] == -1 ? 0 : trans_temp[i];
-
- /* Translate the input buffer. */
- for (i = 0; i < 80; i++)
- r->buf[i] = r->trans[r->buf[i]];
- r->cc = r->trans[r->cc];
- }
+ unsigned char *trans;
+ int i;
+
+ /* Read and ignore vanity splash strings. */
+ for (i = 0; i < 200; i++)
+ advance (r);
- {
- unsigned char sig[8] = {92, 89, 92, 92, 89, 88, 91, 93};
- int i;
+ /* Skip the first 64 characters of the translation table.
+ We don't care about these. They are probably all set to
+ '0', marking them as untranslatable, and that would screw
+ up our actual translation of the real '0'. */
+ for (i = 0; i < 64; i++)
+ advance (r);
+
+ /* Read the rest of the translation table. */
+ trans = pool_malloc (r->pool, 256);
+ memset (trans, 0, 256);
+ for (; i < 256; i++)
+ {
+ unsigned char c;
- for (i = 0; i < 8; i++)
- if (!match (sig[i]))
- lose ((r, "Missing SPSSPORT signature."));
- }
+ advance (r);
- return 1;
+ c = r->cc;
+ if (trans[c] == 0)
+ trans[c] = portable_to_local[i];
+ }
- error:
- return 0;
+ /* Set up the translation table, then read the first
+ translated character. */
+ r->trans = trans;
+ advance (r);
+
+ /* Skip and verify signature. */
+ for (i = 0; i < 8; i++)
+ if (!match (r, "SPSSPORT"[i]))
+ error (r, _("Missing SPSSPORT signature."));
}
/* Reads the version and date info record, as well as product and
subproduct identification records if present. */
-int
+static void
read_version_data (struct pfm_reader *r, struct pfm_read_info *info)
{
- /* Version. */
- if (!match (74 /* A */))
- lose ((r, "Unrecognized version code %d.", r->cc));
+ char *date, *time, *product, *subproduct;
+ int i;
- /* Date. */
- {
- static const int map[] = {6, 7, 8, 9, 3, 4, 0, 1};
- char *date = read_string (r);
- int i;
-
- if (!date)
- return 0;
- if (strlen (date) != 8)
- lose ((r, _("Bad date string length %d."), strlen (date)));
- for (i = 0; i < 8; i++)
- {
- if (date[i] < 64 /* 0 */ || date[i] > 73 /* 9 */)
- lose ((r, _("Bad character in date.")));
- if (info)
- info->creation_date[map[i]] = date[i] - 64 /* 0 */ + '0';
- }
- if (info)
- {
- info->creation_date[2] = info->creation_date[5] = ' ';
- info->creation_date[10] = 0;
- }
- }
-
- /* Time. */
- {
- static const int map[] = {0, 1, 3, 4, 6, 7};
- char *time = read_string (r);
- int i;
-
- if (!time)
- return 0;
- if (strlen (time) != 6)
- lose ((r, _("Bad time string length %d."), strlen (time)));
- for (i = 0; i < 6; i++)
- {
- if (time[i] < 64 /* 0 */ || time[i] > 73 /* 9 */)
- lose ((r, _("Bad character in time.")));
- if (info)
- info->creation_time[map[i]] = time[i] - 64 /* 0 */ + '0';
- }
- if (info)
- {
- info->creation_time[2] = info->creation_time[5] = ' ';
- info->creation_time[8] = 0;
- }
- }
-
- /* Product. */
- if (match (65 /* 1 */))
+ /* Read file. */
+ if (!match (r, 'A'))
+ error (r, "Unrecognized version code `%c'.", r->cc);
+ date = read_pool_string (r);
+ time = read_pool_string (r);
+ product = match (r, '1') ? read_pool_string (r) : (unsigned char *) "";
+ subproduct
+ = match (r, '3') ? read_pool_string (r) : (unsigned char *) "";
+
+ /* Validate file. */
+ if (strlen (date) != 8)
+ error (r, _("Bad date string length %d."), strlen (date));
+ if (strlen (time) != 6)
+ error (r, _("Bad time string length %d."), strlen (time));
+
+ /* Save file info. */
+ if (info != NULL)
{
- char *product;
-
- product = read_string (r);
- if (product == NULL)
- return 0;
- if (info)
- strncpy (info->product, product, 61);
- }
- else if (info)
- info->product[0] = 0;
+ /* Date. */
+ for (i = 0; i < 8; i++)
+ {
+ static const int map[] = {6, 7, 8, 9, 3, 4, 0, 1};
+ info->creation_date[map[i]] = date[i];
+ }
+ info->creation_date[2] = info->creation_date[5] = ' ';
+ info->creation_date[10] = 0;
- /* Subproduct. */
- if (match (67 /* 3 */))
- {
- char *subproduct;
+ /* Time. */
+ for (i = 0; i < 6; i++)
+ {
+ static const int map[] = {0, 1, 3, 4, 6, 7};
+ info->creation_time[map[i]] = time[i];
+ }
+ info->creation_time[2] = info->creation_time[5] = ' ';
+ info->creation_time[8] = 0;
- subproduct = read_string (r);
- if (subproduct == NULL)
- return 0;
- if (info)
- strncpy (info->subproduct, subproduct, 61);
+ /* Product. */
+ st_trim_copy (info->product, product, sizeof info->product);
+ st_trim_copy (info->subproduct, subproduct, sizeof info->subproduct);
}
- else if (info)
- info->subproduct[0] = 0;
- return 1;
-
- error:
- return 0;
}
-static int
-convert_format (struct pfm_reader *r, int fmt[3], struct fmt_spec *v,
- struct variable *vv)
-{
- v->type = translate_fmt (fmt[0]);
- if (v->type == -1)
- lose ((r, _("%s: Bad format specifier byte (%d)."), vv->name, fmt[0]));
- v->w = fmt[1];
- v->d = fmt[2];
-
- /* FIXME? Should verify the resulting specifier more thoroughly. */
-
- if (v->type == -1)
- lose ((r, _("%s: Bad format specifier byte (%d)."), vv->name, fmt[0]));
- if ((vv->type == ALPHA) ^ ((formats[v->type].cat & FCAT_STRING) != 0))
- lose ((r, _("%s variable %s has %s format specifier %s."),
- vv->type == ALPHA ? _("String") : _("Numeric"),
- vv->name,
- formats[v->type].cat & FCAT_STRING ? _("string") : _("numeric"),
- formats[v->type].name));
- return 1;
-
- error:
- return 0;
-}
-
-/* Translation table from SPSS character code to this computer's
- native character code (which is probably ASCII). */
-static const unsigned char spss2ascii[256] =
- {
- " "
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ."
- "<(+|&[]!$*);^-/|,%_>?`:$@'=\" ~- 0123456789 -() {}\\ "
- " "
- };
-
-/* Translate string S into ASCII. */
+/* Translates a format specification read from portable file R as
+ the three integers INTS into a normal format specifier FORMAT,
+ checking that the format is appropriate for variable V. */
static void
-asciify (char *s)
+convert_format (struct pfm_reader *r, const int portable_format[3],
+ struct fmt_spec *format, struct variable *v)
{
- for (; *s; s++)
- *s = spss2ascii[(unsigned char) *s];
+ format->type = translate_fmt (portable_format[0]);
+ if (format->type == -1)
+ error (r, _("%s: Bad format specifier byte (%d)."),
+ v->name, portable_format[0]);
+ format->w = portable_format[1];
+ format->d = portable_format[2];
+
+ if (!check_output_specifier (format, false)
+ || !check_specifier_width (format, v->width, false))
+ error (r, _("%s variable %s has invalid format specifier %s."),
+ v->type == NUMERIC ? _("Numeric") : _("String"),
+ v->name, fmt_to_string (format));
}
-static int parse_value (struct pfm_reader *, union value *, struct variable *);
+static union value parse_value (struct pfm_reader *, struct variable *);
/* Read information on all the variables. */
-static int
+static void
read_variables (struct pfm_reader *r, struct dictionary *dict)
{
char *weight_name = NULL;
int i;
- if (!match (68 /* 4 */))
- lose ((r, _("Expected variable count record.")));
+ if (!match (r, '4'))
+ error (r, _("Expected variable count record."));
r->var_cnt = read_int (r);
if (r->var_cnt <= 0 || r->var_cnt == NOT_INT)
- lose ((r, _("Invalid number of variables %d."), r->var_cnt));
- r->widths = xmalloc (sizeof *r->widths * r->var_cnt);
+ error (r, _("Invalid number of variables %d."), r->var_cnt);
+ r->widths = pool_alloc (r->pool, sizeof *r->widths * r->var_cnt);
/* Purpose of this value is unknown. It is typically 161. */
- {
- int x = read_int (r);
-
- if (x == NOT_INT)
- goto error;
- if (x != 161)
- corrupt_msg (r, _("Unexpected flag value %d."), x);
- }
+ read_int (r);
- if (match (70 /* 6 */))
+ if (match (r, '6'))
{
- weight_name = read_string (r);
- if (!weight_name)
- goto error;
-
- asciify (weight_name);
+ weight_name = read_pool_string (r);
if (strlen (weight_name) > 8)
- {
- corrupt_msg (r, _("Weight variable name (%s) truncated."),
- weight_name);
- weight_name[8] = '\0';
- }
+ error (r, _("Weight variable name (%s) truncated."), weight_name);
}
for (i = 0; i < r->var_cnt; i++)
{
int width;
- unsigned char *name;
+ char name[256];
int fmt[6];
struct variable *v;
int j;
- if (!match (71 /* 7 */))
- lose ((r, _("Expected variable record.")));
+ if (!match (r, '7'))
+ error (r, _("Expected variable record."));
width = read_int (r);
- if (width == NOT_INT)
- goto error;
if (width < 0)
- lose ((r, _("Invalid variable width %d."), width));
+ error (r, _("Invalid variable width %d."), width);
r->widths[i] = width;
-
- name = read_string (r);
- if (name == NULL)
- goto error;
- for (j = 0; j < 6; j++)
- {
- fmt[j] = read_int (r);
- if (fmt[j] == NOT_INT)
- goto error;
- }
-
- /* Verify first character of variable name.
-
- Weirdly enough, there is no # character in the SPSS portable
- character set, so we can't check for it. */
- if (strlen (name) > 8)
- lose ((r, _("position %d: Variable name has %u characters."),
- i, strlen (name)));
- if ((name[0] < 74 /* A */ || name[0] > 125 /* Z */)
- && name[0] != 152 /* @ */)
- lose ((r, _("position %d: Variable name begins with invalid "
- "character."), i));
- if (name[0] >= 100 /* a */ && name[0] <= 125 /* z */)
- {
- corrupt_msg (r, _("position %d: Variable name begins with "
- "lowercase letter %c."),
- i, name[0] - 100 + 'a');
- name[0] -= 26 /* a - A */;
- }
- /* Verify remaining characters of variable name. */
- for (j = 1; j < (int) strlen (name); j++)
- {
- int c = name[j];
+ read_string (r, name);
+ for (j = 0; j < 6; j++)
+ fmt[j] = read_int (r);
- if (c >= 100 /* a */ && c <= 125 /* z */)
- {
- corrupt_msg (r, _("position %d: Variable name character %d "
- "is lowercase letter %c."),
- i, j + 1, c - 100 + 'a');
- name[j] -= 26 /* z - Z */;
- }
- else if ((c >= 64 /* 0 */ && c <= 99 /* Z */)
- || c == 127 /* . */ || c == 152 /* @ */
- || c == 136 /* $ */ || c == 146 /* _ */)
- name[j] = c;
- else
- lose ((r, _("position %d: character `\\%03o' is not "
- "valid in a variable name."), i, c));
- }
+ if (!var_is_valid_name (name, false) || *name == '#')
+ error (r, _("position %d: Invalid variable name `%s'."), name);
+ st_uppercase (name);
- asciify (name);
if (width < 0 || width > 255)
- lose ((r, "Bad width %d for variable %s.", width, name));
+ error (r, "Bad width %d for variable %s.", width, name);
v = dict_create_var (dict, name, width);
if (v == NULL)
- lose ((r, _("Duplicate variable name %s."), name));
- if (!convert_format (r, &fmt[0], &v->print, v))
- goto error;
- if (!convert_format (r, &fmt[3], &v->write, v))
- goto error;
+ error (r, _("Duplicate variable name %s."), name);
+
+ convert_format (r, &fmt[0], &v->print, v);
+ convert_format (r, &fmt[3], &v->write, v);
/* Range missing values. */
- if (match (75 /* B */))
+ if (match (r, 'B'))
{
v->miss_type = MISSING_RANGE;
- if (!parse_value (r, &v->missing[0], v)
- || !parse_value (r, &v->missing[1], v))
- goto error;
+ v->missing[0] = parse_value (r, v);
+ v->missing[1] = parse_value (r, v);
}
- else if (match (74 /* A */))
+ else if (match (r, 'A'))
{
v->miss_type = MISSING_HIGH;
- if (!parse_value (r, &v->missing[0], v))
- goto error;
+ v->missing[0] = parse_value (r, v);
}
- else if (match (73 /* 9 */))
+ else if (match (r, '9'))
{
v->miss_type = MISSING_LOW;
- if (!parse_value (r, &v->missing[0], v))
- goto error;
+ v->missing[0] = parse_value (r, v);
}
/* Single missing values. */
- while (match (72 /* 8 */))
+ while (match (r, '8'))
{
static const int map_next[MISSING_COUNT] =
{
v->miss_type = map_next[v->miss_type];
if (v->miss_type == -1)
- lose ((r, _("Bad missing values for %s."), v->name));
+ error (r, _("Bad missing values for %s."), v->name);
assert (map_ofs[v->miss_type] != -1);
- if (!parse_value (r, &v->missing[map_ofs[v->miss_type]], v))
- goto error;
+ v->missing[map_ofs[v->miss_type]] = parse_value (r, v);
}
- if (match (76 /* C */))
- {
- char *label = read_string (r);
-
- if (label == NULL)
- goto error;
-
- v->label = xstrdup (label);
- asciify (v->label);
- }
+ if (match (r, 'C'))
+ {
+ char label[256];
+ read_string (r, label);
+ v->label = xstrdup (label);
+ }
}
if (weight_name != NULL)
{
struct variable *weight_var = dict_lookup_var (dict, weight_name);
if (weight_var == NULL)
- lose ((r, _("Weighting variable %s not present in dictionary."),
- weight_name));
- free (weight_name);
+ error (r, _("Weighting variable %s not present in dictionary."),
+ weight_name);
dict_set_weight (dict, weight_var);
}
-
- return 1;
-
- error:
- free (weight_name);
- return 0;
}
-/* Parse a value for variable VV into value V. Returns success. */
-static int
-parse_value (struct pfm_reader *r, union value *v, struct variable *vv)
+/* Parse a value for variable VV into value V. */
+static union value
+parse_value (struct pfm_reader *r, struct variable *vv)
{
- if (vv->type == ALPHA)
+ union value v;
+
+ if (vv->type == ALPHA)
{
- char *mv = read_string (r);
- int j;
-
- if (mv == NULL)
- return 0;
-
- strncpy (v->s, mv, 8);
- for (j = 0; j < 8; j++)
- if (v->s[j])
- v->s[j] = spss2ascii[v->s[j]];
- else
- /* Value labels are always padded with spaces. */
- v->s[j] = ' ';
+ char string[256];
+ read_string (r, string);
+ st_bare_pad_copy (v.s, string, 8);
}
else
- {
- v->f = read_float (r);
- if (v->f == second_lowest_value)
- return 0;
- }
+ v.f = read_float (r);
- return 1;
+ return v;
}
/* Parse a value label record and return success. */
-static int
+static void
read_value_label (struct pfm_reader *r, struct dictionary *dict)
{
/* Variables. */
int i;
nv = read_int (r);
- if (nv == NOT_INT)
- return 0;
-
- v = xmalloc (sizeof *v * nv);
+ v = pool_alloc (r->pool, sizeof *v * nv);
for (i = 0; i < nv; i++)
{
- char *name = read_string (r);
- if (name == NULL)
- goto error;
- asciify (name);
+ char name[256];
+ read_string (r, name);
v[i] = dict_lookup_var (dict, name);
if (v[i] == NULL)
- lose ((r, _("Unknown variable %s while parsing value labels."), name));
+ error (r, _("Unknown variable %s while parsing value labels."), name);
if (v[0]->width != v[i]->width)
- lose ((r, _("Cannot assign value labels to %s and %s, which "
+ error (r, _("Cannot assign value labels to %s and %s, which "
"have different variable types or widths."),
- v[0]->name, v[i]->name));
+ v[0]->name, v[i]->name);
}
n_labels = read_int (r);
- if (n_labels == NOT_INT)
- goto error;
-
for (i = 0; i < n_labels; i++)
{
union value val;
- char *label;
-
+ char label[256];
int j;
-
- if (!parse_value (r, &val, v[0]))
- goto error;
-
- label = read_string (r);
- if (label == NULL)
- goto error;
- asciify (label);
+
+ val = parse_value (r, v[0]);
+ read_string (r, label);
/* Assign the value_label's to each variable. */
for (j = 0; j < nv; j++)
continue;
if (var->type == NUMERIC)
- lose ((r, _("Duplicate label for value %g for variable %s."),
- val.f, var->name));
+ error (r, _("Duplicate label for value %g for variable %s."),
+ val.f, var->name);
else
- lose ((r, _("Duplicate label for value `%.*s' for variable %s."),
- var->width, val.s, var->name));
+ error (r, _("Duplicate label for value `%.*s' for variable %s."),
+ var->width, val.s, var->name);
}
}
- free (v);
- return 1;
-
- error:
- free (v);
- return 0;
}
-/* Reads one case from portable file R into C. Returns nonzero
- only if successful. */
-int
+/* Reads one case from portable file R into C. */
+bool
pfm_read_case (struct pfm_reader *r, struct ccase *c)
{
size_t i;
size_t idx;
- /* Check for end of file. */
- if (r->cc == 99 /* Z */)
- return 0;
+ if (setjmp (r->bail_out))
+ return false;
+ /* Check for end of file. */
+ if (r->cc == 'Z')
+ return false;
+
idx = 0;
for (i = 0; i < r->var_cnt; i++)
{
if (width == 0)
{
- double f = read_float (r);
- if (f == second_lowest_value)
- goto unexpected_eof;
-
- case_data_rw (c, idx)->f = f;
+ case_data_rw (c, idx)->f = read_float (r);
idx++;
}
else
{
- char *s = read_string (r);
- if (s == NULL)
- goto unexpected_eof;
- asciify (s);
-
- st_bare_pad_copy (case_data_rw (c, idx)->s, s, width);
+ char string[256];
+ read_string (r, string);
+ st_bare_pad_copy (case_data_rw (c, idx)->s, string, width);
idx += DIV_RND_UP (width, MAX_SHORT_STRING);
}
}
- return 1;
-
- unexpected_eof:
- lose ((r, _("End of file midway through case.")));
-
- error:
- return 0;
+ return true;
}