From 99a039898a804144f4835cf5d87c8c2b6ea62ec1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 27 Jan 2023 17:49:36 +0100 Subject: [PATCH] cleanup --- TP1/C-Cpp/includes/mysok.h | 6 ++---- TP1/C-Cpp/src/mysok.cpp | 16 +++++++++++++--- TP1/C-Cpp/src/r0.cpp | 7 +++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index 4046350..ee0d31c 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -1,8 +1,6 @@ #ifndef MYSOK_H #define MYSOK_H -#include -#include -#include + #include #define NBL 20 @@ -29,7 +27,7 @@ enum board_str { END_OF_LINE = 'a' }; -const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"}; +// const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"}; struct sok_board_t { int board[NBL][NBC]; diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index ad62a82..16a4048 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -1,29 +1,38 @@ #include "../includes/mysok.h" sok_board_t::sok_board_t() { - for (int i = 0; i < NBL; i++) - for (int j = 0; j < NBC; j++) + for (int i = 0; i < NBL; i++) { + for (int j = 0; j < NBC; j++) { board[i][j] = FREE; + } + } } + void sok_board_t::print_board() { for (int i = 0; i < board_nbl; i++) { for (int j = 0; j < NBC; j++) { - if (board[i][j] == END_OF_LINE) + if (board[i][j] == END_OF_LINE) { break; + } + printf("%c", board[i][j]); } + printf("\n"); } } + void sok_board_t::load(char *_file) { FILE *fp = fopen(_file, "r"); char *line = NULL; size_t len = 0; ssize_t nread; + if (fp == NULL) { perror("fopen"); exit(EXIT_FAILURE); } + board_nbl = 0; while ((nread = getline(&line, &len, fp)) != -1) { if ((static_cast(nread)) > 0) { @@ -83,6 +92,7 @@ void sok_board_t::load(char *_file) { } } } + free(line); fclose(fp); } diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 5e28dcf..746df83 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -1,16 +1,15 @@ #include "../includes/mysok.h" -#include -#include -#include -#include int main(int _ac, char **_av) { if (_ac != 2) { printf("usage: %s SOK_FILE\n", _av[0]); + return 0; } + sok_board_t S; S.load(_av[1]); S.print_board(); + return 0; }