Implement the functionality
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 28 Apr 2024 21:20:49 +0000 (23:20 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Sun, 28 Apr 2024 21:20:49 +0000 (23:20 +0200)
It is quite a small project so it has only taken a single commit.

.gitignore
.gitmodules [new file with mode: 0644]
Libft [new submodule]
Makefile [new file with mode: 0644]
README.txt
main.c [new file with mode: 0644]

index 6f71831cb5bef51e3e56cd0fbf82bfd29e404067..72027af7b12d11f724f13ed354a213eb356f2d7b 100644 (file)
@@ -1,3 +1,4 @@
+consebit
 *.[oa]
 tags
 *.pgm
diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..626d139
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "Libft"]
+       path = Libft
+       url = git://ljiriste.work/Libft
diff --git a/Libft b/Libft
new file mode 160000 (submodule)
index 0000000..580d3d9
--- /dev/null
+++ b/Libft
@@ -0,0 +1 @@
+Subproject commit 580d3d9325f4105b5448a5f0e5503e6d105c6117
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..b1d22f8
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,54 @@
+CC := gcc
+CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic
+
+RM := rm -f
+
+EXT_LIBS := m
+EXT_LINKS := $(addprefix -l, $(EXT_LIBS))
+
+SUBPROJECTS := Libft
+
+INCDIR := .
+INCDIR += $(addsuffix /inc, $(SUBPROJECTS));
+INCLUDE := $(addprefix -I, $(INCDIR))
+
+SRCDIR := .
+
+SOURCES :=     main.c                  \
+
+SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
+
+OBJECTS := $(SOURCES:.c=.o)
+
+NAME := consebit
+
+all : $(NAME)
+
+debug : CFLAGS += -g
+debug : $(NAME)
+
+$(NAME) : $(OBJECTS) Libft/libft.a
+       $(CC) $(CFLAGS) -o $@ $^ $(EXT_LINKS)
+
+Libft/libft.a : | Libft/Makefile
+ifneq (,$(findstring debug, $(MAKECMDGOALS)))
+       $(MAKE) -C Libft debug
+else
+       $(MAKE) -C Libft
+endif
+
+%.o : %.c | Libft/Makefile
+       $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE)
+
+%/Makefile :
+       git submodule update --init $($@%/Makefile=%)
+
+clean :
+       $(RM) $(OBJECTS)
+
+fclean : clean
+       $(MAKE) -C Libft/ fclean
+       $(RM) $(NAME)
+
+re : fclean
+       $(MAKE) all
index b659ecf2c34636dd73c93e8c495f92659e9d2215..3affb04e0c2c2c17e5197c16bcff0e0d3d03dd91 100644 (file)
@@ -11,5 +11,5 @@ who in turn found the concept in the video
 by Adrian Crenshaw
 https://www.youtube.com/watch?v=4bM3Gut1hIk
 
-The program should be quite simple to use, I imagine something like
+The program is quite simple to use
 ./consebit <file_name> [<output_file_name>]
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..c3df925
--- /dev/null
+++ b/main.c
@@ -0,0 +1,147 @@
+#include "libft.h"
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <math.h>
+
+#define BUFFER_SIZE 100
+
+static const char      *default_out = "default.pgm";
+static const mode_t    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+
+int    open_files(int argc, char **argv, int *in_fd, int *out_fd)
+{
+       char            *line;
+
+       if (argc == 3)
+               *out_fd = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, mode);
+       else
+               *out_fd = open(default_out, O_WRONLY | O_CREAT, mode);
+       while (*out_fd < 0 && errno == EEXIST)
+       {
+               ft_printf("The file %s already exist. Do you want to overwrite? (y or [n]) ", argv[2]);
+               line = get_next_line(STDIN_FILENO);
+               if (!ft_strcmp(line, "\n") || !ft_strcmp(line, "n\n"))
+               {
+                       free(line);
+                       return (1);
+               }
+               else if (!ft_strcmp(line, "y\n"))
+               {
+                       *out_fd = open(argv[2], O_WRONLY | O_CREAT, mode);
+                       free(line);
+                       if (*out_fd < 0)
+                               return (2);
+               }
+       }
+       *in_fd = open(argv[1], O_RDONLY);
+       if (*in_fd >= 0)
+               return (0);
+       close(*out_fd);
+       return (2);
+}
+
+void   print_pgm_header(int fd)
+{
+       ft_dprintf(fd, "P5\n256 256\n255\n");
+       return ;
+}
+
+int    mat_max(t_mat *mat)
+{
+       size_t  i;
+       size_t  j;
+       int             tmp;
+       int             max;
+
+       i = 0;
+       max = 0;
+       while (i < 256)
+       {
+               j = 0;
+               while (j < 256)
+               {
+                       tmp = *(int *)ft_mat_access(mat, i, j);
+                       if (tmp > max)
+                               max = tmp;
+                       ++j;
+               }
+               ++i;
+       }
+       return (max);
+}
+
+void   print_image(int fd, t_mat *mat)
+{
+       size_t          i;
+       size_t          j;
+       int                     cell;
+       const int       max = mat_max(mat);
+
+       i = 0;
+       while (i < 256)
+       {
+               j = 0;
+               while (j < 256)
+               {
+                       cell = *(int *)ft_mat_access(mat, i, j);
+                       ft_dprintf(fd, "%c", (char)(log(cell + 1) / log(max + 1) * 255));
+                       ++j;
+               }
+               ++i;
+       }
+       return ;
+}
+
+void   file_processing(int in_fd, int out_fd)
+{
+       size_t                  i;
+       unsigned char   buffer[BUFFER_SIZE];
+       t_mat                   matrix;
+
+       if (ft_mat_init(&matrix, sizeof(int)) != success || ft_mat_zeros(&matrix, 256, 256) != success)
+               return ;
+       while (read(in_fd, buffer, BUFFER_SIZE) > 0)
+       {
+               i = 1;
+               while (i < BUFFER_SIZE)
+               {
+                       ++*(int *)ft_mat_access(&matrix, buffer[i - 1], buffer[i]);
+                       ++i;
+               }
+       }
+       print_pgm_header(out_fd);
+       print_image(out_fd, &matrix);
+       ft_mat_free(&matrix, NULL);
+       return ;
+}
+
+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");
+       return ;
+}
+
+int    main(int argc, char **argv)
+{
+       int     in_fd;
+       int     out_fd;
+       int     res;
+
+       if (argc < 2 || argc > 3)
+       {
+               print_help();
+               return (1);
+       }
+       res = open_files(argc, argv, &in_fd, &out_fd);
+       if (res != 0)
+               return (res);
+       file_processing(in_fd, out_fd);
+       close(in_fd);
+       close(out_fd);
+       return (0);
+}