Commit 02b05163 authored by Laslo Hunhold's avatar Laslo Hunhold

Add efread() and efwrite()

Given we have a util.c anyway it does not make any sense to duplicate
code here. This way, error handling and output is in one place and one
does not have to change things in multiple different files. This also
reduces code duplication by a lot.

It also solves an older bug where the error reporting was not on point:

   $ echo "farb" | ff2png
   ff2png: fread: Success

(It also lacked a newline)

Now it properly reports

   $ echo "farb" | ff2png
   ff2png: fread: Unexpected end of file

I also fixed some other minor details, for instance that all error
messages should begin with a capital letter.
parent a9877de2
......@@ -93,16 +93,7 @@ main(int argc, char *argv[])
/* write data */
for (i = 0; i < height; ++i) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
if (ferror(stdin)) {
fprintf(stderr, "%s: fread: %s\n", argv0,
strerror(errno));
} else {
fprintf(stderr, "%s: unexpected end of file\n",
argv0);
}
return 1;
}
efread(row, sizeof(uint16_t), rowlen, stdin);
for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
a = ntohs(row[j + 3]);
for (l = 0; l < 3; l++) {
......
......@@ -48,18 +48,8 @@ main(int argc, char *argv[])
width, height);
for (i = 0; i < height; i++) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
if (ferror(stdin)) {
fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
} else {
fprintf(stderr, "%s: unexpected end of file\n", argv0);
}
return 1;
}
if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) {
fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
return 1;
}
efread(row, sizeof(uint16_t), rowlen, stdin);
efwrite(row, sizeof(uint16_t), rowlen, stdout);
}
return fshut(stdout, "<stdout>");
......
......@@ -68,14 +68,7 @@ main(int argc, char *argv[])
/* write data */
for (i = 0; i < height; ++i) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
if (ferror(stdin)) {
fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
} else {
fprintf(stderr, "%s: unexpected end of file\n", argv0);
}
return 1;
}
efread(row, sizeof(uint16_t), rowlen, stdin);
png_write_row(pngs, (uint8_t *)row);
}
......
......@@ -53,16 +53,8 @@ main(int argc, char *argv[])
printf("P6\n%" PRIu32 " %" PRIu32 "\n255\n", width, height);
for (i = 0; i < height; ++i) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
if (ferror(stdin)) {
fprintf(stderr, "%s: fread: %s\n", argv0,
strerror(errno));
} else {
fprintf(stderr, "%s: unexpected end of file\n",
argv0);
}
return 1;
}
efread(row, sizeof(uint16_t), rowlen, stdin);
for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
a = ntohs(row[j + 3]);
for (l = 0; l < 3; l++) {
......@@ -73,10 +65,8 @@ main(int argc, char *argv[])
(UINT16_MAX / UINT8_MAX));
}
}
if (fwrite(rowout, sizeof(uint8_t), rowoutlen, stdout) != rowoutlen) {
fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
return 1;
}
efwrite(rowout, sizeof(uint8_t), rowoutlen, stdout);
}
return fshut(stdout, "<stdout>");
......
......@@ -80,10 +80,7 @@ main(int argc, char *argv[])
row[4 * i + 3] = htons(65535);
}
if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) {
fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
return 1;
}
efwrite(row, sizeof(uint16_t), rowlen, stdout);
}
/* clean up */
......
......@@ -81,22 +81,12 @@ main(int argc, char *argv[])
for (i = 0; i < rowlen; i++) {
row[i] = htons(257 * pngrows[r][i]);
}
if (fwrite(row, sizeof(uint16_t), rowlen,
stdout) != rowlen) {
fprintf(stderr, "%s: fwrite: %s\n", argv0,
strerror(errno));
return 1;
}
efwrite(row, sizeof(uint16_t), rowlen, stdout);
}
break;
case 16:
for (r = 0; r < height; ++r) {
if (fwrite(pngrows[r], sizeof(uint16_t), rowlen,
stdout) != rowlen) {
fprintf(stderr, "%s: fwrite: %s\n", argv0,
strerror(errno));
return 1;
}
efwrite(pngrows[r], sizeof(uint16_t), rowlen, stdout);
}
break;
default:
......
......@@ -18,13 +18,10 @@ ff_read_header(uint32_t *width, uint32_t *height)
{
uint32_t hdr[4];
if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) {
fprintf(stderr, "%s: fread: %s", argv0, strerror(errno));
exit(1);
}
efread(hdr, sizeof(*hdr), LEN(hdr), stdin);
if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
fprintf(stderr, "%s: invalid magic value\n", argv0);
fprintf(stderr, "%s: Invalid magic value\n", argv0);
exit(1);
}
......@@ -40,16 +37,10 @@ ff_write_header(uint32_t width, uint32_t height)
fputs("farbfeld", stdout);
tmp = htonl(width);
if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
exit(1);
}
efwrite(&tmp, sizeof(tmp), 1, stdout);
tmp = htonl(height);
if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) {
fprintf(stderr, "%s: write: %s", argv0, strerror(errno));
exit(1);
}
efwrite(&tmp, sizeof(tmp), 1, stdout);
}
int
......@@ -93,13 +84,13 @@ fshut(FILE *fp, const char *fname)
fflush(fp);
if (ferror(fp) && !ret) {
fprintf(stderr, "%s: ferror %s: %s\n", argv0, fname,
fprintf(stderr, "%s: ferror '%s': %s\n", argv0, fname,
strerror(errno));
ret = 1;
}
if (fclose(fp) && !ret) {
fprintf(stderr, "%s: fclose %s: %s\n", argv0, fname,
fprintf(stderr, "%s: fclose '%s': %s\n", argv0, fname,
strerror(errno));
ret = 1;
}
......@@ -107,13 +98,37 @@ fshut(FILE *fp, const char *fname)
return ret;
}
void
efread(void *p, size_t s, size_t n, FILE *f)
{
if (fread(p, s, n, f) != n) {
if (ferror(f)) {
fprintf(stderr, "%s: fread: %s\n", argv0,
strerror(errno));
} else {
fprintf(stderr, "%s: fread: Unexpected end of file\n",
argv0);
}
exit(1);
}
}
void
efwrite(const void *p, size_t s, size_t n, FILE *f)
{
if (fwrite(p, s, n, f) != n) {
fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
exit(1);
}
}
void *
ereallocarray(void *optr, size_t nmemb, size_t size)
{
void *p;
if (!(p = reallocarray(optr, nmemb, size))) {
fprintf(stderr, "%s: reallocarray: out of memory\n", argv0);
fprintf(stderr, "%s: reallocarray: Out of memory\n", argv0);
exit(1);
}
......@@ -128,7 +143,7 @@ estrtonum(const char *numstr, long long minval, long long maxval)
ll = strtonum(numstr, minval, maxval, &errstr);
if (errstr) {
fprintf(stderr, "%s: strtonum %s: %s\n", argv0, numstr, errstr);
fprintf(stderr, "%s: strtonum '%s': %s\n", argv0, numstr, errstr);
exit(1);
}
......
......@@ -13,6 +13,9 @@ int parse_mask(const char *, uint16_t mask[3]);
int fshut(FILE *, const char *);
void efread(void *, size_t, size_t, FILE *);
void efwrite(const void *, size_t, size_t, FILE *);
#undef reallocarray
void *reallocarray(void *, size_t, size_t);
void *ereallocarray(void *optr, size_t nmemb, size_t size);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment