diff options
-rw-r--r-- | Exception.cpp | 23 | ||||
-rw-r--r-- | Exception.h | 16 | ||||
-rw-r--r-- | Parser.cpp | 19 | ||||
-rw-r--r-- | Parser.h | 11 |
4 files changed, 41 insertions, 28 deletions
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 <cstdarg> +#include <cstdio> + +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 <exception> + +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; }; @@ -1,29 +1,10 @@ #include <cstring> #include <sstream> -#include <cstdarg> #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 @@ -2,22 +2,15 @@ #include <iostream> #include <istream> -#include <exception> + #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: |