diff options
Diffstat (limited to 'converter/ppm/xvminitoppm.c')
-rw-r--r-- | converter/ppm/xvminitoppm.c | 64 |
1 files changed, 31 insertions, 33 deletions
diff --git a/converter/ppm/xvminitoppm.c b/converter/ppm/xvminitoppm.c index d76bea87..ad207ae9 100644 --- a/converter/ppm/xvminitoppm.c +++ b/converter/ppm/xvminitoppm.c @@ -13,12 +13,13 @@ #include <assert.h> #include <string.h> #include <errno.h> +#include <stdbool.h> #include "pm_c_util.h" #include "nstring.h" +#include "pm.h" #include "ppm.h" -#define BUFSIZE 256 typedef struct xvPalette { @@ -53,25 +54,6 @@ parseCommandLine(int const argc, static void -getLine(FILE * const ifP, - char * const buf, - size_t const size) { - - char * rc; - - rc = fgets(buf, size, ifP); - if (rc == NULL) { - if (ferror(ifP)) - pm_error("read error. fgets() failed, errno=%d (%s)", - errno, strerror(errno)); - else - pm_error("unexpected EOF"); - } -} - - - -static void makeXvPalette(xvPalette * const xvPaletteP) { unsigned int paletteIndex; @@ -101,37 +83,53 @@ readXvHeader(FILE * const ifP, unsigned int * const colsP, unsigned int * const rowsP, unsigned int * const maxvalP) { - - char buf[256]; + + char * buf; + size_t bufferSz; + int eof; + size_t lineLen; unsigned int cols, rows, maxval; int rc; bool endOfComments; - - getLine(ifP, buf, sizeof(buf)); - if (!strneq(buf, "P7 332", 6)) + buf = NULL; /* initial value */ + bufferSz = 0; /* initial value */ + + pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen); + + if (eof || !strneq(buf, "P7 332", 6)) pm_error("Input is not a XV thumbnail picture. It does not " "begin with the characters 'P7 332'."); - endOfComments = FALSE; - while (!endOfComments) { - getLine(ifP, buf, sizeof(buf)); + for (endOfComments = false; !endOfComments; ) { + int eof; + size_t lineLen; + pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen); + if (eof) + pm_error("EOF before #END_OF_COMMENTS line"); if (strneq(buf, "#END_OF_COMMENTS", 16)) - endOfComments = TRUE; + endOfComments = true; else if (strneq(buf, "#BUILTIN", 8)) pm_error("This program does not know how to " "convert builtin XV thumbnail pictures"); } - getLine(ifP, buf, sizeof(buf)); + pm_getline(ifP, &buf, &bufferSz, &eof, &lineLen); + if (eof) + pm_error("EOF where cols/rows/maxval line expected"); + rc = sscanf(buf, "%u %u %u", &cols, &rows, &maxval); if (rc != 3) pm_error("error parsing dimension info '%s'. " "It does not consist of 3 decimal numbers.", buf); if (maxval != 255) pm_error("bogus XV thumbnail maxval %u. Should be 255", maxval); + *colsP = cols; *rowsP = rows; *maxvalP = maxval; + + if (buf) + free(buf); } @@ -166,7 +164,7 @@ writePpm(FILE * const ifP, else { unsigned int const paletteIndex = byte; assert(byte >= 0); - + PPM_ASSIGN(pixrow[col], xvPaletteP->red[paletteIndex], xvPaletteP->grn[paletteIndex], @@ -181,7 +179,7 @@ writePpm(FILE * const ifP, -int +int main(int argc, char * argv[]) { @@ -190,7 +188,7 @@ main(int argc, unsigned int cols, rows; pixval maxval; xvPalette xvPalette; - + ppm_init(&argc, argv); parseCommandLine(argc, argv, &cmdline); |