X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdata-io%2Fdata-reader.c;h=70ff2756dfaf8449d9a91f1c80f9cec9bbf66645;hb=e86d3e8623564b379e6097a3df9e7232e8087160;hp=ea95bc983298d54acc24df5c55b62f1495436c07;hpb=2814862a2c45a39f9822cf4c64ca3884822d064d;p=pspp diff --git a/src/language/data-io/data-reader.c b/src/language/data-io/data-reader.c index ea95bc9832..70ff2756df 100644 --- a/src/language/data-io/data-reader.c +++ b/src/language/data-io/data-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-2004, 2006, 2010, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 1997-2004, 2006, 2010, 2011, 2012, 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 @@ -23,7 +23,6 @@ #include #include #include -#include #include "data/casereader.h" #include "data/dataset.h" @@ -67,9 +66,8 @@ struct dfm_reader struct string scratch; /* Extra line buffer. */ enum dfm_reader_flags flags; /* Zero or more of DFM_*. */ FILE *file; /* Associated file. */ - off_t file_size; /* File size, or -1 if unavailable. */ size_t pos; /* Offset in line of current character. */ - unsigned eof_cnt; /* # of attempts to advance past EOF. */ + unsigned n_eofs; /* # of attempts to advance past EOF. */ struct lexer *lexer; /* The lexer reading the file */ char *encoding; /* Current encoding. */ @@ -95,7 +93,7 @@ dfm_close_reader (struct dfm_reader *r) /* This was the last client, so close the underlying file. */ if (fh_get_referent (r->fh) != FH_REF_INLINE) - fn_close (fh_get_file_name (r->fh), r->file); + fn_close (r->fh, r->file); else { /* Skip any remaining data on the inline file. */ @@ -151,22 +149,19 @@ dfm_open_reader (struct file_handle *fh, struct lexer *lexer, ds_init_empty (&r->line); ds_init_empty (&r->scratch); r->flags = DFM_ADVANCE; - r->eof_cnt = 0; + r->n_eofs = 0; r->block_left = 0; if (fh_get_referent (fh) != FH_REF_INLINE) { - struct stat s; r->line_number = 0; - r->file = fn_open (fh_get_file_name (fh), "rb"); + r->file = fn_open (fh, "rb"); if (r->file == NULL) { msg (ME, _("Could not open `%s' for reading as a data file: %s."), fh_get_file_name (r->fh), strerror (errno)); + goto error; } - r->file_size = fstat (fileno (r->file), &s) == 0 ? s.st_size : -1; } - else - r->file_size = -1; fh_lock_set_aux (lock, r); if (encoding == NULL) @@ -235,11 +230,11 @@ read_inline_record (struct dfm_reader *r) { if (!lex_match_id (r->lexer, "END") || !lex_match_id (r->lexer, "DATA")) { - msg (SE, _("Missing END DATA while reading inline data. " + msg (SE, _("Missing %s while reading inline data. " "This probably indicates a missing or incorrectly " - "formatted END DATA command. END DATA must appear " + "formatted %s command. %s must appear " "by itself on a single line with exactly one space " - "between words.")); + "between words."), "END DATA", "END DATA", "END DATA"); lex_discard_rest_of_command (r->lexer); } return false; @@ -559,24 +554,24 @@ dfm_eof (struct dfm_reader *r) { r->flags &= ~DFM_ADVANCE; - if (r->eof_cnt == 0 && read_record (r) ) + if (r->n_eofs == 0 && read_record (r)) { r->pos = 0; return 0; } - r->eof_cnt++; - if (r->eof_cnt == 2) + r->n_eofs++; + if (r->n_eofs == 2) { if (r->fh != fh_inline_file ()) msg (ME, _("Attempt to read beyond end-of-file on file %s."), fh_get_name (r->fh)); else - msg (ME, _("Attempt to read beyond END DATA.")); + msg (ME, _("Attempt to read beyond %s."), "END DATA"); } } - return r->eof_cnt; + return r->n_eofs; } /* Returns the current record in the file corresponding to @@ -586,7 +581,7 @@ struct substring dfm_get_record (struct dfm_reader *r) { assert ((r->flags & DFM_ADVANCE) == 0); - assert (r->eof_cnt == 0); + assert (r->n_eofs == 0); return ds_substr (&r->line, r->pos, SIZE_MAX); } @@ -601,7 +596,7 @@ dfm_expand_tabs (struct dfm_reader *r) size_t ofs, new_pos, tab_width; assert ((r->flags & DFM_ADVANCE) == 0); - assert (r->eof_cnt == 0); + assert (r->n_eofs == 0); if (r->flags & DFM_TABS_EXPANDED) return; @@ -657,31 +652,6 @@ dfm_reader_get_encoding (const struct dfm_reader *reader) return reader->encoding; } -/* Returns a number between 0 and 100 that approximates the - percentage of the data in READER that has already been read, - or -1 if this value cannot be estimated. - - ftello is slow in glibc (it flushes the read buffer), so don't - call this function unless you need to. */ -int -dfm_get_percent_read (const struct dfm_reader *reader) -{ - if (reader->file_size >= 0) - { - off_t position; - - position = (reader->line_reader != NULL - ? line_reader_tell (reader->line_reader) - : ftello (reader->file)); - if (position >= 0) - { - double p = 100.0 * position / reader->file_size; - return p < 0 ? 0 : p > 100 ? 100 : p; - } - } - return -1; -} - /* Causes dfm_get_record() or dfm_get_whole_record() to read in the next record the next time it is executed on file HANDLE. */ @@ -738,15 +708,27 @@ dfm_get_column (const struct dfm_reader *r, const char *p) const char * dfm_get_file_name (const struct dfm_reader *r) { - return (fh_get_referent (r->fh) == FH_REF_FILE - ? fh_get_file_name (r->fh) + enum fh_referent referent = fh_get_referent (r->fh); + return (referent == FH_REF_FILE ? fh_get_file_name (r->fh) + : referent == FH_REF_INLINE ? lex_get_file_name (r->lexer) : NULL); } int dfm_get_line_number (const struct dfm_reader *r) { - return fh_get_referent (r->fh) == FH_REF_FILE ? r->line_number : -1; + switch (fh_get_referent (r->fh)) + { + case FH_REF_FILE: + return r->line_number; + + case FH_REF_INLINE: + return lex_ofs_start_point (r->lexer, lex_ofs (r->lexer)).line; + + case FH_REF_DATASET: + default: + return -1; + } } /* BEGIN DATA...END DATA procedure. */