diff --git a/CMakeLists.txt b/CMakeLists.txt index 88c44f2..259d866 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ add_executable(Graph "parser.c" "tree.c" "ps_graph.c" - "error_buffer.c" + "errors.c" "math_functions.c" ) @@ -24,10 +24,12 @@ if (MSVC) target_compile_options(Graph PRIVATE /W4) else() target_compile_options(Graph PRIVATE -Wall -Wextra -pedantic) + target_link_libraries(Graph PRIVATE m) endif() +target_compile_definitions(Graph PRIVATE ENABLE_GRAPHVIZ_EXPORT) + # link math -target_link_libraries(Graph PRIVATE m) # Optionally, set the output directory for the executable # set_target_properties(Graph PROPERTIES diff --git a/error_buffer.c b/errors.c similarity index 93% rename from error_buffer.c rename to errors.c index 2496a86..3484803 100644 --- a/error_buffer.c +++ b/errors.c @@ -1,45 +1,45 @@ -#include "error_buffer.h" -#include -#include -#include - -void error_buffer_init(struct error_buffer *eb) { - eb->err = ERR_NO_ERR; - eb->text_len = 0; - eb->text[0] = 0; -} - -void error_set(struct error_buffer *eb, enum error_code err) { - eb->err = err; -} - -enum error_code error_get(const struct error_buffer *eb) { - return eb->err; -} - -void error_printf(struct error_buffer *eb, const char *format, ...) { - va_list args; - int space = MAX_ERROR_MESSAGE_LENGTH - eb->text_len; - int write_size; - - if (space == 0) - return; - - va_start(args, format); - write_size = vsnprintf(eb->text + eb->text_len, MAX_ERROR_MESSAGE_LENGTH - eb->text_len, format, args); - va_end(args); - - if (write_size < 0) - return; - - if (write_size < space) { - eb->text_len += write_size; - } - else { - eb->text_len = MAX_ERROR_MESSAGE_LENGTH; - } -} - -const char *error_get_text(const struct error_buffer *eb) { - return eb->text; +#include "errors.h" +#include +#include +#include + +void error_buffer_init(struct error_buffer *eb) { + eb->err = ERR_NO_ERR; + eb->text_len = 0; + eb->text[0] = 0; +} + +void error_set(struct error_buffer *eb, enum error_code err) { + eb->err = err; +} + +enum error_code error_get(const struct error_buffer *eb) { + return eb->err; +} + +void error_printf(struct error_buffer *eb, const char *format, ...) { + va_list args; + int space = MAX_ERROR_MESSAGE_LENGTH - eb->text_len; + int write_size; + + if (space == 0) + return; + + va_start(args, format); + write_size = vsnprintf(eb->text + eb->text_len, MAX_ERROR_MESSAGE_LENGTH - eb->text_len, format, args); + va_end(args); + + if (write_size < 0) + return; + + if (write_size < space) { + eb->text_len += write_size; + } + else { + eb->text_len = MAX_ERROR_MESSAGE_LENGTH; + } +} + +const char *error_get_text(const struct error_buffer *eb) { + return eb->text; } \ No newline at end of file diff --git a/error_buffer.h b/errors.h similarity index 91% rename from error_buffer.h rename to errors.h index 60e34ef..59c0f4f 100644 --- a/error_buffer.h +++ b/errors.h @@ -1,67 +1,67 @@ -#ifndef ERROR_CODE_H -#define ERROR_CODE_H - -#include - -#define MAX_ERROR_MESSAGE_LENGTH 512 - -/** - * @brief Chybové kódy - */ -enum error_code { - ERR_NO_ERR = 0, /* Žádná chyba */ - ERR_INVALID_ARGS = 1, /* Neplatné argumenty programu */ - ERR_INVALID_FUNCTION = 2, /* Zadaná matematická funkce je neplatná */ - ERR_INVALID_FILENAME = 3, /* Zadaný název souboru není platný */ - ERR_INVALID_LIMITS = 4, /* Zadané hranice jsou ve špatném formátu */ - ERR_BAD_ALLOC = 5 /* Při alokaci paměti nastala chyba */ -}; - -/** - * @brief Zásobník pro chybový kód a řetězec popisující chybu - */ -struct error_buffer { - enum error_code err; - char text[MAX_ERROR_MESSAGE_LENGTH]; - size_t text_len; -}; - -/** - * @brief Inicializuje zásobník - * - * @param eb Zásobník - */ -void error_buffer_init(struct error_buffer *eb); - -/** - * @brief Nastaví chybový kód - * - * @param eb Zásobník - * @param err Chybový kód - */ -void error_set(struct error_buffer *eb, enum error_code err); - -/** - * @brief Přidá do zásobníku formátovaný řetězec - * - * @param eb Zásobník - */ -void error_printf(struct error_buffer *eb, const char *format, ...); - -/** - * Vrátí chybový kód - * - * @param eb Zásobník - * @return Chybový kód - */ -enum error_code error_get(const struct error_buffer *eb); - -/** - * Vrátí řetězec popisující chybu - * - * @param eb Zásobník - * @return Řetězec popisující chybu - */ -const char *error_get_text(const struct error_buffer *eb); - -#endif /* ERROR_CODE_H */ \ No newline at end of file +#ifndef ERRORS_H +#define ERRORS_H + +#include + +#define MAX_ERROR_MESSAGE_LENGTH 512 + +/** + * @brief Chybové kódy + */ +enum error_code { + ERR_NO_ERR = 0, /* Žádná chyba */ + ERR_INVALID_ARGS = 1, /* Neplatné argumenty programu */ + ERR_INVALID_FUNCTION = 2, /* Zadaná matematická funkce je neplatná */ + ERR_INVALID_FILENAME = 3, /* Zadaný název souboru není platný */ + ERR_INVALID_LIMITS = 4, /* Zadané hranice jsou ve špatném formátu */ + ERR_BAD_ALLOC = 5 /* Při alokaci paměti nastala chyba */ +}; + +/** + * @brief Zásobník pro chybový kód a řetězec popisující chybu + */ +struct error_buffer { + enum error_code err; + char text[MAX_ERROR_MESSAGE_LENGTH]; + size_t text_len; +}; + +/** + * @brief Inicializuje zásobník + * + * @param eb Zásobník + */ +void error_buffer_init(struct error_buffer *eb); + +/** + * @brief Nastaví chybový kód + * + * @param eb Zásobník + * @param err Chybový kód + */ +void error_set(struct error_buffer *eb, enum error_code err); + +/** + * @brief Přidá do zásobníku formátovaný řetězec + * + * @param eb Zásobník + */ +void error_printf(struct error_buffer *eb, const char *format, ...); + +/** + * Vrátí chybový kód + * + * @param eb Zásobník + * @return Chybový kód + */ +enum error_code error_get(const struct error_buffer *eb); + +/** + * Vrátí řetězec popisující chybu + * + * @param eb Zásobník + * @return Řetězec popisující chybu + */ +const char *error_get_text(const struct error_buffer *eb); + +#endif /* ERRORS_H */ \ No newline at end of file diff --git a/lex.h b/lex.h index 6f130ac..3314ec0 100644 --- a/lex.h +++ b/lex.h @@ -1,7 +1,7 @@ #ifndef LEX_H #define LEX_H -#include "error_buffer.h" +#include "errors.h" #define LEX_DEBUG @@ -114,4 +114,4 @@ const char *lex_get_error_text(const struct lexer *lex); */ const char *lex_token_str(enum token_type token); -#endif /* LEX_H */ \ No newline at end of file +#endif /* LEX_H */ diff --git a/main.c b/main.c index ab72f70..7f9cc75 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,7 @@ #include "lex.h" #include "parser.h" #include "ps_graph.h" -#include "error_buffer.h" +#include "errors.h" static void print_usage(FILE *f, const char *name) { fprintf(f, "Usage: %s []\n", name);