X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fpc%2B-file-reader.c;h=72d9fe1d012c3dcdf84a91d8eb803748ccf082e7;hb=114cd2dc25959a7927730e502300321624f8c07b;hp=0aa4587d38ca3ebe81fe2b3edd10f1edd2272be1;hpb=197c17c92ac8124ae389434afa105bee90b96ad8;p=pspp diff --git a/src/data/pc+-file-reader.c b/src/data/pc+-file-reader.c index 0aa4587d38..72d9fe1d01 100644 --- a/src/data/pc+-file-reader.c +++ b/src/data/pc+-file-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-2000, 2006-2007, 2009-2014 Free Software Foundation, Inc. + Copyright (C) 1997-2000, 2006-2007, 2009-2016 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -190,11 +190,10 @@ static bool parse_variable_records (struct pcp_reader *, struct dictionary *, static struct any_reader * pcp_open (struct file_handle *fh) { - struct pcp_reader *r; struct stat s; /* Create and initialize reader. */ - r = xzalloc (sizeof *r); + struct pcp_reader *r = XZALLOC (struct pcp_reader); r->any_reader.klass = &pcp_file_reader_class; r->pool = pool_create (); pool_register (r->pool, free, r); @@ -209,7 +208,7 @@ pcp_open (struct file_handle *fh) goto error; /* Open file. */ - r->file = fn_open (fh_get_file_name (fh), "rb"); + r->file = fn_open (fh, "rb"); if (r->file == NULL) { msg (ME, _("Error opening `%s' for reading as an SPSS/PC+ " @@ -221,13 +220,13 @@ pcp_open (struct file_handle *fh) /* Fetch file size. */ if (fstat (fileno (r->file), &s)) { - pcp_error (ME, 0, _("%s: stat failed (%s)."), + pcp_error (r, 0, _("%s: stat failed (%s)."), fh_get_file_name (r->fh), strerror (errno)); goto error; } if (s.st_size > UINT_MAX) { - pcp_error (ME, 0, _("%s: file too large."), fh_get_file_name (r->fh)); + pcp_error (r, 0, _("%s: file too large."), fh_get_file_name (r->fh)); goto error; } r->file_size = s.st_size; @@ -461,7 +460,7 @@ pcp_decode (struct any_reader *r_, const char *encoding, error: pcp_close (&r->any_reader); - dict_destroy (dict); + dict_unref (dict); *dictp = NULL; return NULL; } @@ -478,7 +477,7 @@ pcp_close (struct any_reader *r_) if (r->file) { - if (fn_close (fh_get_file_name (r->fh), r->file) == EOF) + if (fn_close (r->fh, r->file) == EOF) { msg (ME, _("Error closing system file `%s': %s."), fh_get_file_name (r->fh), strerror (errno)); @@ -505,18 +504,20 @@ pcp_file_casereader_destroy (struct casereader *reader UNUSED, void *r_) pcp_close (&r->any_reader); } -/* Returns true if FILE is an SPSS/PC+ system file, - false otherwise. */ +/* Detects whether FILE is an SPSS/PC+ system file. Returns 1 if so, 0 if + not, and a negative errno value if there is an error reading FILE. */ static int pcp_detect (FILE *file) { static const char signature[4] = "SPSS"; char buf[sizeof signature]; - if (fseek (file, 0x104, SEEK_SET) - || (fread (buf, sizeof buf, 1, file) != 1 && !feof (file))) + if (fseek (file, 0x104, SEEK_SET)) return -errno; + if (fread (buf, sizeof buf, 1, file) != 1) + return ferror (file) ? -errno : 0; + return !memcmp (buf, signature, sizeof buf); } @@ -639,8 +640,9 @@ read_value_labels (struct pcp_reader *r, struct pcp_var_record *var, uint8_t len; if (var->n_val_labs >= allocated_val_labs) - var->val_labs = x2nrealloc (var->val_labs, &allocated_val_labs, - sizeof *var->val_labs); + var->val_labs = pool_2nrealloc (r->pool, var->val_labs, + &allocated_val_labs, + sizeof *var->val_labs); vl = &var->val_labs[var->n_val_labs]; if (!read_bytes (r, vl->value, sizeof vl->value) @@ -808,7 +810,7 @@ parse_header (struct pcp_reader *r, const struct pcp_main_header *header, info->integer_format = INTEGER_LSB_FIRST; info->float_format = FLOAT_IEEE_DOUBLE_LE; info->compression = r->compressed ? ANY_COMP_SIMPLE : ANY_COMP_NONE; - info->case_cnt = r->n_cases; + info->n_cases = r->n_cases; /* Convert file label to UTF-8 and put it into DICT. */ label = recode_and_trim_string (r->pool, dict_encoding, header->file_label); @@ -897,8 +899,7 @@ parse_variable_records (struct pcp_reader *r, struct dictionary *dict, if (var_is_numeric (var)) value.f = parse_float (rec->val_labs[i].value); else - memcpy (value_str_rw (&value, rec->width), - rec->val_labs[i].value, rec->width); + memcpy (value.s, rec->val_labs[i].value, rec->width); utf8_label = recode_string ("UTF-8", dict_encoding, rec->val_labs[i].label, -1); @@ -964,8 +965,7 @@ pcp_file_casereader_read (struct casereader *reader, void *r_) if (var->width == 0) retval = read_case_number (r, &v->f); else - retval = read_case_string (r, value_str_rw (v, var->width), - var->width); + retval = read_case_string (r, v->s, var->width); if (retval != 1) { @@ -1149,9 +1149,7 @@ static void pcp_msg (struct pcp_reader *r, off_t offset, int class, const char *format, va_list args) { - struct msg m; struct string text; - ds_init_empty (&text); if (offset >= 0) ds_put_format (&text, _("`%s' near offset 0x%llx: "), @@ -1160,16 +1158,13 @@ pcp_msg (struct pcp_reader *r, off_t offset, ds_put_format (&text, _("`%s': "), fh_get_file_name (r->fh)); ds_put_vformat (&text, format, args); - m.category = msg_class_to_category (class); - m.severity = msg_class_to_severity (class); - m.file_name = NULL; - m.first_line = 0; - m.last_line = 0; - m.first_column = 0; - m.last_column = 0; - m.text = ds_cstr (&text); - - msg_emit (&m); + struct msg *m = xmalloc (sizeof *m); + *m = (struct msg) { + .category = msg_class_to_category (class), + .severity = msg_class_to_severity (class), + .text = ds_steal_cstr (&text), + }; + msg_emit (m); } /* Displays a warning for offset OFFSET in the file. */ @@ -1205,11 +1200,11 @@ pcp_error (struct pcp_reader *r, off_t offset, const char *format, ...) an error. */ static inline int read_bytes_internal (struct pcp_reader *r, bool eof_is_ok, - void *buf, size_t byte_cnt) + void *buf, size_t n_bytes) { - size_t bytes_read = fread (buf, 1, byte_cnt, r->file); + size_t bytes_read = fread (buf, 1, n_bytes, r->file); r->pos += bytes_read; - if (bytes_read == byte_cnt) + if (bytes_read == n_bytes) return 1; else if (ferror (r->file)) { @@ -1229,9 +1224,9 @@ read_bytes_internal (struct pcp_reader *r, bool eof_is_ok, Returns true if successful. Returns false upon I/O error or if end-of-file is encountered. */ static bool -read_bytes (struct pcp_reader *r, void *buf, size_t byte_cnt) +read_bytes (struct pcp_reader *r, void *buf, size_t n_bytes) { - return read_bytes_internal (r, false, buf, byte_cnt) == 1; + return read_bytes_internal (r, false, buf, n_bytes) == 1; } /* Reads BYTE_CNT bytes into BUF. @@ -1239,9 +1234,9 @@ read_bytes (struct pcp_reader *r, void *buf, size_t byte_cnt) Returns 0 if an immediate end-of-file is encountered. Returns -1 if an I/O error or a partial read occurs. */ static int -try_read_bytes (struct pcp_reader *r, void *buf, size_t byte_cnt) +try_read_bytes (struct pcp_reader *r, void *buf, size_t n_bytes) { - return read_bytes_internal (r, true, buf, byte_cnt); + return read_bytes_internal (r, true, buf, n_bytes); } /* Reads a 16-bit signed integer from R and stores its value in host format in