From: Ben Pfaff Date: Wed, 5 Dec 2007 06:04:59 +0000 (+0000) Subject: * src/language/data-io/data-reader.c (struct dfm_reader): New X-Git-Tag: v0.6.0~175 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8dfafee045cf6ef58db3cc6647922ddc74abe1a;p=pspp-builds.git * src/language/data-io/data-reader.c (struct dfm_reader): New `file_size' member to support dfm_get_percent_read. (dfm_open_reader): Initialize file_size. (dfm_get_percent_read): New function. * Smake (GNULIB_MODULES): Add ftello module. --- diff --git a/ChangeLog b/ChangeLog index 1c6fee02..3ddc5a27 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2007-12-04 Ben Pfaff + + * Smake (GNULIB_MODULES): Add ftello module. + 2007-11-05 Ben Pfaff * configure.ac: No need for check for off_t or for the size of diff --git a/Smake b/Smake index 43536f9d..6d41a6a3 100644 --- a/Smake +++ b/Smake @@ -18,6 +18,7 @@ GNULIB_MODULES = \ full-read \ full-write \ fseeko \ + ftello \ fwriteerror \ gethostname \ getline \ diff --git a/src/language/data-io/ChangeLog b/src/language/data-io/ChangeLog index dcc0961f..ed22c9a4 100644 --- a/src/language/data-io/ChangeLog +++ b/src/language/data-io/ChangeLog @@ -1,3 +1,10 @@ +2007-12-04 Ben Pfaff + + * src/language/data-io/data-reader.c (struct dfm_reader): New + `file_size' member to support dfm_get_percent_read. + (dfm_open_reader): Initialize file_size. + (dfm_get_percent_read): New function. + 2007-11-08 Ben Pfaff Patch #6256: add support for binary, 360 file formats. Reviewed diff --git a/src/language/data-io/data-reader.c b/src/language/data-io/data-reader.c index e3ef8ca1..9b08625e 100644 --- a/src/language/data-io/data-reader.c +++ b/src/language/data-io/data-reader.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -63,6 +64,7 @@ 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. */ struct lexer *lexer; /* The lexer reading the file */ @@ -137,6 +139,7 @@ dfm_open_reader (struct file_handle *fh, struct lexer *lexer) r->block_left = 0; if (fh_get_referent (fh) != FH_REF_INLINE) { + struct stat s; r->where.file_name = fh_get_file_name (fh); r->where.line_number = 0; r->file = fn_open (fh_get_file_name (fh), @@ -150,7 +153,10 @@ dfm_open_reader (struct file_handle *fh, struct lexer *lexer) free (r); return NULL; } + r->file_size = fstat (fileno (r->file), &s) == 0 ? s.st_size : -1; } + else + r->file_size = -1; fh_lock_set_aux (lock, r); return r; @@ -596,6 +602,27 @@ dfm_reader_get_legacy_encoding (const struct dfm_reader *reader) return fh_get_legacy_encoding (reader->fh); } +/* 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 = 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. */ diff --git a/src/language/data-io/data-reader.h b/src/language/data-io/data-reader.h index 029dacdf..c7fee613 100644 --- a/src/language/data-io/data-reader.h +++ b/src/language/data-io/data-reader.h @@ -40,6 +40,7 @@ struct substring dfm_get_record (struct dfm_reader *); void dfm_expand_tabs (struct dfm_reader *); enum legacy_encoding dfm_reader_get_legacy_encoding ( const struct dfm_reader *); +int dfm_get_percent_read (const struct dfm_reader *); /* Line control. */ void dfm_forward_record (struct dfm_reader *);