Add the option to output as plain PGM trunk
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 4 Aug 2025 19:03:21 +0000 (21:03 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Mon, 4 Aug 2025 19:05:07 +0000 (21:05 +0200)
Previously no options were supported and the PGM produced was raw
binary. Not consebit supports options for help and plain to display help
or generate the output as plain ASCII.
This may introduce additional unhandled inputs.

Makefile
main.c

index b1d22f826aa0efa086cfd780f7b5258a8e911121..b504903eb04ee2243b2477148d65f8c548fd06be 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CC := gcc
-CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic
+CFLAGS = -g -std=c99 -Wall -Wextra -Werror -Wpedantic
 
 RM := rm -f
 
diff --git a/main.c b/main.c
index 48d6bc7e3720dd456404e6c9290f3584115a4eb1..29467340294de5b09299a900814c2ae5e53626b9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -13,12 +13,34 @@ typedef unsigned long long  t_count;
 static const char      *default_out = "default.pgm";
 static const mode_t    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
 
+// I copied the checking logic here to main, because I thought the open_files
+// should not be doing input checking, but I will leave it here because
+// I feel it should stay here also
+// I just don't check the return value
+//
+// Another way would be to pass the index value from main to open_files through
+// the in_fd and out_fd integers (as input-output arguments) I don't think that
+// is terribly clean idea either though...
+int    get_file_index(int argc, char **argv)
+{
+       int     i;
+
+       i = 1;
+       while (i < argc && argv[i][0] == '-')
+               ++i;
+       if (argc == i || argc > i + 2)
+               return (0);
+       return (i);
+}
+
 int    open_files(int argc, char **argv, int *in_fd, int *out_fd)
 {
-       char            *line;
+       char    *line;
+       int             file_index;
 
-       if (argc == 3)
-               *out_fd = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, mode);
+       file_index = get_file_index(argc, argv);
+       if (argc > file_index)
+               *out_fd = open(argv[file_index + 1], O_WRONLY | O_CREAT | O_EXCL, mode);
        else
                *out_fd = open(default_out, O_WRONLY | O_CREAT, mode);
        while (*out_fd < 0 && errno == EEXIST)
@@ -32,22 +54,25 @@ int open_files(int argc, char **argv, int *in_fd, int *out_fd)
                }
                else if (!ft_strcmp(line, "y\n"))
                {
-                       *out_fd = open(argv[2], O_WRONLY | O_CREAT, mode);
+                       *out_fd = open(argv[file_index + 1], O_WRONLY | O_CREAT, mode);
                        free(line);
                        if (*out_fd < 0)
                                return (2);
                }
        }
-       *in_fd = open(argv[1], O_RDONLY);
+       *in_fd = open(argv[file_index], O_RDONLY);
        if (*in_fd >= 0)
                return (0);
        close(*out_fd);
        return (2);
 }
 
-void   print_pgm_header(int fd)
+void   print_pgm_header(int fd, int raw)
 {
-       ft_dprintf(fd, "P5\n256 256\n255\n");
+       if (raw)
+               ft_dprintf(fd, "P5\n256 256\n255\n");
+       else
+               ft_dprintf(fd, "P2\n256 256\n255\n");
        return ;
 }
 
@@ -99,7 +124,7 @@ t_count      mat_min(t_mat *mat)
        return (min);
 }
 
-void   print_image(int fd, t_mat *mat)
+void   print_image(int fd, t_mat *mat, int raw)
 {
        size_t                  i;
        size_t                  j;
@@ -114,15 +139,20 @@ void      print_image(int fd, t_mat *mat)
                while (j < 256)
                {
                        cell = *(t_count *)ft_mat_access(mat, i, j);
-                       ft_dprintf(fd, "%c", (char)(log(cell - min + 1) / log(max - min + 1) * 255));
+                       if (raw)
+                               ft_dprintf(fd, "%c", (char)(log(cell - min + 1) / log(max - min + 1) * 255));
+                       else
+                               ft_dprintf(fd, " %i", (int)(log(cell - min + 1) / log(max - min + 1) * 255));
                        ++j;
                }
+               if (!raw)
+                       ft_dprintf(fd, "\n");
                ++i;
        }
        return ;
 }
 
-void   file_processing(int in_fd, int out_fd)
+void   file_processing(int in_fd, int out_fd, int raw)
 {
        size_t                  i;
        unsigned char   buffer[BUFFER_SIZE];
@@ -139,8 +169,8 @@ void        file_processing(int in_fd, int out_fd)
                        ++i;
                }
        }
-       print_pgm_header(out_fd);
-       print_image(out_fd, &matrix);
+       print_pgm_header(out_fd, raw);
+       print_image(out_fd, &matrix, raw);
        ft_mat_free(&matrix, NULL);
        return ;
 }
@@ -149,7 +179,12 @@ void       print_help(void)
 {
        ft_printf("This is the consebit tool by Lukáš Jiřiště\n"
                        "This tool plots frequency of pairs of consecutive bits.\n\n"
-                       "consebit <input_file> [<output_file>]\n");
+                       "consebit [OPTIONS...] <input_file> [<output_file>]\n\n"
+                       "OPTIONS:\n"
+                               "\t-h, --help\n"
+                                       "\t\tprints this help\n\n"
+                               "\t-p, --plain\n"
+                                       "\t\toutputs the image in plain (ASCII) rather than raw (binary) PPM\n");
        return ;
 }
 
@@ -158,19 +193,34 @@ int       main(int argc, char **argv)
        int     in_fd;
        int     out_fd;
        int     res;
+       int     i;
+       int     raw;
 
-       if (argc < 2 || argc > 3)
+       if (argc < 2 || !get_file_index(argc, argv))
        {
                print_help();
                return (1);
        }
+       raw = 1;
+       i = 1;
+       while (i < argc)
+       {
+               if (!ft_strcmp(argv[i], "-h") || !ft_strcmp(argv[i], "--help"))
+               {
+                       print_help();
+                       return (0);
+               }
+               if (!ft_strcmp(argv[i], "-p") || !ft_strcmp(argv[i], "--plain"))
+                       raw = 0;
+               ++i;
+       }
        res = open_files(argc, argv, &in_fd, &out_fd);
        if (res != 0)
        {
                ft_printf("There was a problem opening a file.\n");
                return (res);
        }
-       file_processing(in_fd, out_fd);
+       file_processing(in_fd, out_fd, raw);
        close(in_fd);
        close(out_fd);
        return (0);