From 674b575367c06052e9b4f522fa7aded9cdbe79fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Sun, 28 Apr 2024 23:20:49 +0200 Subject: [PATCH] Implement the functionality It is quite a small project so it has only taken a single commit. --- .gitignore | 1 + .gitmodules | 3 ++ Libft | 1 + Makefile | 54 +++++++++++++++++++ README.txt | 2 +- main.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 Libft create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore index 6f71831..72027af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +consebit *.[oa] tags *.pgm diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..626d139 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Libft"] + path = Libft + url = git://ljiriste.work/Libft diff --git a/Libft b/Libft new file mode 160000 index 0000000..580d3d9 --- /dev/null +++ b/Libft @@ -0,0 +1 @@ +Subproject commit 580d3d9325f4105b5448a5f0e5503e6d105c6117 diff --git a/Makefile b/Makefile new file mode 100644 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 diff --git a/README.txt b/README.txt index b659ecf..3affb04 100644 --- a/README.txt +++ b/README.txt @@ -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 [] diff --git a/main.c b/main.c new file mode 100644 index 0000000..c3df925 --- /dev/null +++ b/main.c @@ -0,0 +1,147 @@ +#include "libft.h" +#include +#include +#include +#include +#include +#include + +#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 []\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); +} -- 2.30.2