diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2021-12-27 17:33:56 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2021-12-27 17:33:56 +0000 |
commit | 6e629f983aa205c3eaa5f339f1c71bb5e7938049 (patch) | |
tree | a89f594443ac2330138f0bd0f19ee59135c2213f /lib/pmfileio.c | |
parent | ec52f41aabc9de9aac203c2f462252e403c7374a (diff) | |
download | netpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.tar.gz netpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.tar.xz netpbm-mirror-6e629f983aa205c3eaa5f339f1c71bb5e7938049.zip |
Promote Development to Advanced to make Release 10.97.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@4222 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/pmfileio.c')
-rw-r--r-- | lib/pmfileio.c | 112 |
1 files changed, 75 insertions, 37 deletions
diff --git a/lib/pmfileio.c b/lib/pmfileio.c index 4048e74d..1ed71f18 100644 --- a/lib/pmfileio.c +++ b/lib/pmfileio.c @@ -452,27 +452,16 @@ pm_tmpfile_fd(void) { } - -FILE * -pm_openr_seekable(const char name[]) { +static bool +isSeekable(FILE * const fP) { /*---------------------------------------------------------------------------- - Open the file named by name[] such that it is seekable (i.e. it can be - rewound and read in multiple passes with fseek()). + The file is seekable -- we can set its read/write position to anything we + want. - If the file is actually seekable, this reduces to the same as - pm_openr(). If not, we copy the named file to a temporary file - and return that file's stream descriptor. - - We use a file that the operating system recognizes as temporary, so - it picks the filename and deletes the file when Caller closes it. + If we can't tell if it is seekable, we return false. -----------------------------------------------------------------------------*/ - int stat_rc; - int seekable; /* logical: file is seekable */ + int statRc; struct stat statbuf; - FILE * original_file; - FILE * seekable_file; - - original_file = pm_openr((char *) name); /* I would use fseek() to determine if the file is seekable and be a little more general than checking the type of file, but I @@ -486,41 +475,62 @@ pm_openr_seekable(const char name[]) { some other file is, it doesn't hurt much to assume it isn't. */ - stat_rc = fstat(fileno(original_file), &statbuf); - if (stat_rc == 0 && S_ISREG(statbuf.st_mode)) - seekable = TRUE; - else - seekable = FALSE; + statRc = fstat(fileno(fP), &statbuf); + + return statRc == 0 && S_ISREG(statbuf.st_mode); +} + + + +FILE * +pm_openr_seekable(const char name[]) { +/*---------------------------------------------------------------------------- + Open the file named by name[] such that it is seekable (i.e. it can be + rewound and read in multiple passes with fseek()). + + If the file is actually seekable, this reduces to the same as + pm_openr(). If not, we copy the named file to a temporary file + and return that file's stream descriptor. - if (seekable) { - seekable_file = original_file; + We use a file that the operating system recognizes as temporary, so + it picks the filename and deletes the file when Caller closes it. +-----------------------------------------------------------------------------*/ + FILE * originalFileP; + FILE * seekableFileP; + + originalFileP = pm_openr((char *) name); + + if (isSeekable(originalFileP)) { + seekableFileP = originalFileP; } else { - seekable_file = pm_tmpfile(); + seekableFileP = pm_tmpfile(); /* Copy the input into the temporary seekable file */ - while (!feof(original_file) && !ferror(original_file) - && !ferror(seekable_file)) { + while (!feof(originalFileP) && !ferror(originalFileP) + && !ferror(seekableFileP)) { char buffer[4096]; - int bytes_read; - bytes_read = fread(buffer, 1, sizeof(buffer), original_file); - fwrite(buffer, 1, bytes_read, seekable_file); + size_t nBytesRead; + + nBytesRead = fread(buffer, 1, sizeof(buffer), originalFileP); + fwrite(buffer, 1, nBytesRead, seekableFileP); } - if (ferror(original_file)) + if (ferror(originalFileP)) pm_error("Error reading input file into temporary file. " "Errno = %s (%d)", strerror(errno), errno); - if (ferror(seekable_file)) + if (ferror(seekableFileP)) pm_error("Error writing input into temporary file. " "Errno = %s (%d)", strerror(errno), errno); - pm_close(original_file); + pm_close(originalFileP); { - int seek_rc; - seek_rc = fseek(seekable_file, 0, SEEK_SET); - if (seek_rc != 0) + int seekRc; + + seekRc = fseek(seekableFileP, 0, SEEK_SET); + if (seekRc != 0) pm_error("fseek() failed to rewind temporary file. " "Errno = %s (%d)", strerror(errno), errno); } } - return seekable_file; + return seekableFileP; } @@ -968,6 +978,20 @@ pm_bs_long(long const l) { +int +pm_is_seekable(FILE * const fP) { + + return isSeekable(fP) ? 1 : 0; +} + + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wduplicated-cond" + + + void pm_tell2(FILE * const fileP, void * const fileposP, @@ -1008,6 +1032,10 @@ pm_tell2(FILE * const fileP, +#pragma GCC diagnostic pop + + + unsigned int pm_tell(FILE * const fileP) { @@ -1020,6 +1048,12 @@ pm_tell(FILE * const fileP) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wduplicated-cond" + + + void pm_seek2(FILE * const fileP, const pm_filepos * const fileposP, @@ -1047,6 +1081,10 @@ pm_seek2(FILE * const fileP, +#pragma GCC diagnostic pop + + + void pm_seek(FILE * const fileP, unsigned long filepos) { /*---------------------------------------------------------------------------- |