From 83b83ab7a23587493fa8503d6cbdf3bde8e961de Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 15 Apr 2025 10:14:23 +0200 Subject: [PATCH] full adaptation to c99 --- color.c | 3 +- display.c | 5 ++-- image.c | 18 +++++------ image.h | 2 +- makefile | 21 ++++++++----- ndisplay.c | 88 ++++++++++++++++++++++++++---------------------------- pnm.c | 58 ++++++++++++++++++----------------- 7 files changed, 98 insertions(+), 97 deletions(-) diff --git a/color.c b/color.c index c8385c8..e49fea8 100644 --- a/color.c +++ b/color.c @@ -309,8 +309,7 @@ color_t *_findcolor (float red, float green, float blue, color_t *colors, int nb { float dist = 3; color_t *color = NULL; - int i; - for (i = 0; i < nbcolors; i++) { + for (int i = 0; i < nbcolors; i++) { float r = (colors + i)->red - red; float g = (colors + i)->green - green; float b = (colors + i)->blue - blue; diff --git a/display.c b/display.c index b818bd7..ae4d968 100644 --- a/display.c +++ b/display.c @@ -129,9 +129,8 @@ int main (int argc, char *argv[]) return 1; } - int k, l; - for (l = 0; l < image->height; l++) { - for (k = 0; k < image->width; k++) { + for (int l = 0; l < image->height; l++) { + for (int k = 0; k < image->width; k++) { int ind = k + image->width * l; color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap); switch (colormap) { diff --git a/image.c b/image.c index 45d1aec..e72dd0f 100644 --- a/image.c +++ b/image.c @@ -25,7 +25,7 @@ void freeimage (image_t *image) free (image); } -int contain (char *str, char c) +int contain (char *str, int c) { while (*str != c) { if (*str++ == '\0') { @@ -44,9 +44,8 @@ image_t *expandimage (image_t *image, int scale) { image_t *nimage = newimage (image->width * scale, image->height * scale, image->maxvalue); - int i, j; - for (i = 0; i < nimage->width; i++) { - for (j = 0; j < nimage->height; j++) { + for (int i = 0; i < nimage->width; i++) { + for (int j = 0; j < nimage->height; j++) { int pos = i / scale + (j / scale) * image->width; int Pos = i + j * nimage->width; nimage->red[Pos] = image->red[pos]; @@ -62,15 +61,14 @@ image_t *shrinkimage (image_t *image, int scale) { image_t *nimage = newimage ((image->width + scale - 1) / scale, (image->height + scale - 1) / scale, image->maxvalue); - int i, j; - for (i = 0; i < nimage->width; i++) { - for (j = 0; j < nimage->height; j++) { + for (int i = 0; i < nimage->width; i++) { + for (int j = 0; j < nimage->height; j++) { float red = 0; float green = 0; float blue = 0; - int k, l, n = 0; - for (k = 0; k < scale; k++) { - for (l = 0; l < scale; l++) { + int n = 0; + for (int k = 0; k < scale; k++) { + for (int l = 0; l < scale; l++) { int I = i * scale + k; int J = j * scale + l; if ((I < image->width) && (J < image->height)) { diff --git a/image.h b/image.h index 01f67f5..610a0a1 100644 --- a/image.h +++ b/image.h @@ -16,7 +16,7 @@ image_t *newimage (int width, int height, int maxvalue); void freeimage (image_t *image); -int contain (char *str, char c); +int contain (char *str, int c); float correction (float value, float factor); diff --git a/makefile b/makefile index 4bd0238..09ecb8b 100644 --- a/makefile +++ b/makefile @@ -5,12 +5,14 @@ CC = gcc #INCLUDES = -I../debug -D__MEMORY_ALLOCATION__ INCLUDES = OFLAGS = -O4 -Os +#OFLAGS = -O0 #OFLAGS = -O4 -ffast-math -finline-functions #OFLAGS = -O4 -finline-functions #OFLAGS += -mtune=pentium3 -mmmx -msse -msse2 -m3dnow #OFLAGS += -minline-all-stringops -fsingle-precision-constant #OFLAGS += -malign-double CFLAGS += -W -Wall -Wextra -g +#CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 CFLAGS += $(OFLAGS) $(INCLUDES) $(OPTIONS) LDFLAGS += -g $(LDOPTS) $(OPTIONS) @@ -18,15 +20,14 @@ LDOPT = linker MV = mv ifneq (, $(findstring linux, $(MAKE_HOST))) # Linux -CFLAGS += -std=c99 -D_XOPEN_SOURCE=500 else ifneq (, $(findstring mingw, $(MAKE_HOST))) # Windows MinGw -CFLAGS += -DPDC_WIDE +CFLAGS += -DWIN32 #LDLIBS += -lws2_32 LDOPT = winlnk else ifneq (, $(findstring cygwin, $(MAKE_HOST))) +CFLAGS += -DWIN32 # Windows CygWin -CFLAGS += -DPDC_WIDE LDOPT = winlnk else ifneq (, $(findstring msdos, $(MAKE_HOST))) # MSDOS @@ -36,9 +37,7 @@ endif # Targets -ALLEXE = -ALLEXE += display -ALLEXE += ndisplay +ALLEXE = $(shell for f in *.c; do grep -q '/\*\slinker:' $$f && echo $${f/.c}; done) SHELL = bash @@ -67,6 +66,11 @@ GETCOMMENTS = awk '/\/\*\s*$(1):/,/\*\// { sub(/.*\/\*\s*$(1):/, ""); sub (/\s*\ all: depends $(MAKE) $(ALLEXE:%=%.exe) +analyze: + make purge + scan-build make + #scan-build -stats make + count: wc $(wildcard *.c *.h) $(MAKEFILE_LIST) @@ -119,7 +123,7 @@ gcov_%: touch gcov rm -f gcov $(wildcard *.gcda *.gcno) $(MAKE) purge - grep '#####' *.c.gcov || true + grep '^ *#####' *.c.gcov || true gprof_%: $(MAKE) purge @@ -153,6 +157,7 @@ test_%: %.test %.exe eval $$test; \ [ $$? -eq 0 ] && echo -e "\033[1;32mSUCCESS\033[0;0m" \ || { echo -e "\033[1;31mFAILED\033[0;0m"; RC=1; }; \ + test "$$RC" = 1 -a "$(STOP)" = 1 && break; \ done; \ test "$$RC" -ne 1 @@ -186,7 +191,7 @@ valgrind_%: %.exe ## Phony -.PHONY: all clean count depends gcovs purge tests +.PHONY: all analyze clean count depends gcovs purge tests ## Precious diff --git a/ndisplay.c b/ndisplay.c index 1eec4d4..e2a5f72 100644 --- a/ndisplay.c +++ b/ndisplay.c @@ -1,5 +1,5 @@ /* depend: */ -/* cflags: */ +/* cflags: -D_XOPEN_SOURCE=500 */ /* linker: color.o debug.o image.o pnm.o -lncursesw -lm */ /* doslnk: color.o debug.o image.o pnm.o -lpdc~2 -lm */ /* winlnk: color.o debug.o image.o pnm.o -lpdcursesw -lm */ @@ -55,13 +55,12 @@ int usage (int ret) int main (int argc, char *argv[]) { char *filename = NULL; - int i; /* get basename */ char *pt = progname = argv[0]; while (*pt) { if ((*pt == '/') || (*pt == '\\')) { - progname = pt + 1; + progname = pt + 1; } pt++; } @@ -75,43 +74,43 @@ int main (int argc, char *argv[]) } char c = arg[1]; switch (c) { - case 'g': - arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; - if (arg == NULL) { - VERBOSE (ERROR, fprintf (stderr, "%s: no gamma specified\n", progname)); - return usage (1); - } - gf[0] = strtof (arg, &pt); - if (*pt == '\0') { - gf[1] = gf[2] = gf[0]; - } else if (*pt == ':') { - gf[1] = strtof (pt + 1, &pt); - if (*pt == ':') { - gf[2] = strtof (pt + 1, &pt); + case 'g': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, fprintf (stderr, "%s: no gamma specified\n", progname)); + return usage (1); + } + gf[0] = strtof (arg, &pt); + if (*pt == '\0') { + gf[1] = gf[2] = gf[0]; + } else if (*pt == ':') { + gf[1] = strtof (pt + 1, &pt); + if (*pt == ':') { + gf[2] = strtof (pt + 1, &pt); + } else { + gf[2] = 0; + } } else { - gf[2] = 0; + gf[1] = gf[2] = 0; } - } else { - gf[1] = gf[2] = 0; - } - break; - case 't': - mode = 1; - break; - case 'v': - arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; - if (arg == NULL) { - VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname)); - return usage (1); - } - verbose = atoi (arg); - break; - case 'w': - mode = 2; - break; - case 'h': - default: - return usage (c != 'h'); + break; + case 't': + mode = 1; + break; + case 'v': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname)); + return usage (1); + } + verbose = atoi (arg); + break; + case 'w': + mode = 2; + break; + case 'h': + default: + return usage (c != 'h'); } } @@ -134,7 +133,7 @@ int main (int argc, char *argv[]) } /* pre-calculate expanded and shrinked images */ - for (i = 2; i <= MAXSCALE; i++) { + for (int i = 2; i <= MAXSCALE; i++) { images[i] = shrinkimage (images[1], i); } images[0] = expandimage (images[1], 2); @@ -160,8 +159,7 @@ int main (int argc, char *argv[]) int ycursor = 0; /* init colormap */ - int c; - for (c = 0; c < MAXCOLORS; c++) { + for (int c = 0; c < MAXCOLORS; c++) { init_pair (c, 0, c); } @@ -169,8 +167,6 @@ int main (int argc, char *argv[]) int stop = 0; int change = 1; while (!stop) { - int x, y; - /* check cursor location */ if (images[scale]->width - xcursor < COLS / mode) { int newxcursor = max (0, images[scale]->width - COLS / mode); @@ -196,8 +192,8 @@ int main (int argc, char *argv[]) int width = min (images[scale]->width - xcursor, COLS / mode); int height = min (images[scale]->height - ycursor, LINES); VERBOSE (DEBUG, FILE *fd = fopen ("my.log", "a"); fprintf (fd, "(%d, %d)\n", width, height); fclose (fd)); - for (x = 0; x < width; x++) { - for (y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { int ind = x + xcursor + images[scale]->width * (y + ycursor); //if (ind >= images[scale]->width * images[scale]->height) { @@ -317,7 +313,7 @@ int main (int argc, char *argv[]) /* cleaning before quiting */ endwin (); - for (i = 0; i <= MAXSCALE; i++) { + for (int i = 0; i <= MAXSCALE; i++) { freeimage (images[i]); } diff --git a/pnm.c b/pnm.c index 9751168..551181a 100644 --- a/pnm.c +++ b/pnm.c @@ -9,8 +9,7 @@ int testpnm (FILE *fd) int mode = 0; /* check magic number */ char magic[3] = {0}; - fread (magic, 3, 1, fd); - if ((contain ("P", magic[0])) && (contain (" \t\n\r", magic[2]))) { + if ((fread (magic, 3, 1, fd) == 1) && (contain ("P", magic[0])) && (contain (" \t\n\r", magic[2]))) { if ((magic[1] >= '1') && (magic[1] <= '6')) { mode = magic[1] - '0'; } @@ -22,15 +21,14 @@ int getparam (FILE *fd, int *value) { int start = 0; *value = 0; - while (!feof (fd)) { - int c = fgetc (fd); + int c; + while ((c = fgetc (fd)) != EOF) { if (contain ("#", c)) { - while (!feof (fd)) { - c = fgetc (fd); - if (contain ("\n\r", c)) { - break; + do { + if ((c = fgetc (fd)) == EOF) { + return 0; } - } + } while (!contain ("\n\r", c)); } else if (contain ("0123456789", c)) { *value = 10 * *value + c - '0'; start = 1; @@ -48,11 +46,9 @@ int getparam (FILE *fd, int *value) int getrawvalue (FILE *fd, int nbbytes) { int value = fgetc (fd); - if (value != EOF) { - if (nbbytes == 2) { - int value0 = fgetc (fd); - value = (value0 != EOF) ? value * 256 + value0 : EOF; - } + if ((value != EOF) && (nbbytes == 2)) { + int value0 = fgetc (fd); + value = (value0 != EOF) ? value * 256 + value0 : EOF; } return value; } @@ -74,14 +70,15 @@ int readpbma (FILE *fd, image_t *image) { int i = 0; while (i < image->width * image->height) { - int c = fgetc (fd); - if (contain ("#", c)) { - while (!feof (fd)) { - c = fgetc (fd); - if (contain ("\n\r", c)) { - break; + int c; + if ((c = fgetc (fd)) == EOF) { + return 0; + } else if (contain ("#", c)) { + do { + if ((c = fgetc (fd)) == EOF) { + return 0; } - } + } while (!contain ("\n\r", c)); } else if (contain ("01", c)) { image->red[i] = image->green[i] = image->blue[i] = '1' - c; i++; @@ -99,7 +96,9 @@ int readpbmr (FILE *fd, image_t *image) int value = EOF; while (i < image->width * image->height) { if (k == 0) { - value = fgetc (fd); + if ((value = fgetc (fd)) == EOF) { + break; + } k = 8; } image->red[i] = image->green[i] = image->blue[i] = !((value >> --k) & 1); @@ -116,8 +115,7 @@ int readpgm (FILE *fd, image_t *image, int mode) int i = 0; mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0; int value = EOF; - while (i < image->width * image->height) { - value = getvalue (fd, mode); + while ((i < image->width * image->height) && ((value = getvalue (fd, mode)) != EOF)) { image->red[i] = image->green[i] = image->blue[i] = value / (float)image->maxvalue; i++; } @@ -130,11 +128,17 @@ int readppm (FILE *fd, image_t *image, int mode) mode = (mode) ? ((image->maxvalue > 255) ? 2 : 1) : 0; int value = EOF; while (i < image->width * image->height) { - value = getvalue (fd, mode); + if ((value = getvalue (fd, mode)) == EOF) { + break; + } image->red[i] = value / (float)image->maxvalue; - value = getvalue (fd, mode); + if ((value = getvalue (fd, mode)) == EOF) { + break; + } image->green[i] = value / (float)image->maxvalue; - value = getvalue (fd, mode); + if ((value = getvalue (fd, mode)) == EOF) { + break; + } image->blue[i] = value / (float)image->maxvalue; i++; } -- 2.30.2