From b93c2d642d7c9f94f36f408f3614741b29161c14 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:50:08 +0200 Subject: de big exception --- Exception.cpp | 23 +++++++++++++++++++++++ Exception.h | 16 ++++++++++++++++ Parser.cpp | 19 ------------------- Parser.h | 11 ++--------- 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 Exception.cpp create mode 100644 Exception.h diff --git a/Exception.cpp b/Exception.cpp new file mode 100644 index 0000000..52e30dc --- /dev/null +++ b/Exception.cpp @@ -0,0 +1,23 @@ +#include "Exception.h" + +#include +#include + +Exception::Exception(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; + if (error != NULL) free(error); + error = (char *) malloc(sz); + vsnprintf(error, sz, fmt, args); + va_end(args); +} + +Exception::~Exception() { + if (error != NULL) + free(error); +} + +const char * Exception::what() { + return error; +} \ No newline at end of file diff --git a/Exception.h b/Exception.h new file mode 100644 index 0000000..818f3f0 --- /dev/null +++ b/Exception.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class Exception : public std::exception { +public: + Exception(const char * fmt, ...); + virtual ~Exception(); + virtual const char * what(); + +private: + char * error = NULL; +}; + +class ParserException : public Exception { using Exception::Exception; }; +class CircuitException : public Exception { using Exception::Exception; }; diff --git a/Parser.cpp b/Parser.cpp index 84c3217..c8a90b4 100644 --- a/Parser.cpp +++ b/Parser.cpp @@ -1,29 +1,10 @@ #include #include -#include #include "Parser.h" using std::getline; -ParserException::ParserException(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - size_t sz = vsnprintf(NULL, 0, fmt, args) + 1; - if (error != NULL) free(error); - error = (char *) malloc(sz); - vsnprintf(error, sz, fmt, args); - va_end(args); -} - -ParserException::~ParserException() { - if (error != NULL) - free(error); -} - -const char * ParserException::what() { - return error; -} size_t Parser::filter(char * input) { size_t diff --git a/Parser.h b/Parser.h index 3a86eec..b6d7bd5 100644 --- a/Parser.h +++ b/Parser.h @@ -2,22 +2,15 @@ #include #include -#include + #include "Circuit.h" +#include "Exception.h" using std::istream; using std::string; -class ParserException : public std::exception { -public: - ParserException(const char * fmt, ...); - virtual ~ParserException(); - virtual const char * what(); -private: - char * error = NULL; -}; class Parser { public: -- cgit v1.2.3