aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 15:14:17 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-12 15:14:17 +0200
commit79afbacfd2ce5c5b25bd38b9247977faf2cf5e34 (patch)
tree217411de5b990b099c95f24816f2ceca23a9f8a2
parent94ef7627456fd93c343c6516d1f235a27e54a082 (diff)
fix exception error messages
-rw-r--r--Exception.cpp41
-rw-r--r--Exception.h10
-rw-r--r--Parser.cpp4
3 files changed, 39 insertions, 16 deletions
diff --git a/Exception.cpp b/Exception.cpp
index c6dd60b..371aaa7 100644
--- a/Exception.cpp
+++ b/Exception.cpp
@@ -4,16 +4,6 @@
#include <cstdio>
#include <cstdlib>
-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);
@@ -22,3 +12,34 @@ Exception::~Exception() {
const char * Exception::what() {
return error;
}
+
+void Exception::va_format(va_list args, const char * fmt) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1;
+ if (error != NULL) free(error);
+ error = (char *) malloc(sz);
+ va_end(args_copy);
+
+ vsnprintf(error, sz, fmt, args);
+}
+
+Exception::Exception(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ va_format(args, fmt);
+ va_end(args);
+}
+CircuitException::CircuitException(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ va_format(args, fmt);
+ va_end(args);
+}
+ParserException::ParserException(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ va_format(args, fmt);
+ va_end(args);
+}
diff --git a/Exception.h b/Exception.h
index ef45522..8f4ba4f 100644
--- a/Exception.h
+++ b/Exception.h
@@ -1,5 +1,6 @@
#pragma once
+#include <cstdarg>
#include <exception>
class Exception : public std::exception {
@@ -8,16 +9,19 @@ public:
virtual ~Exception();
virtual const char * what();
-private:
+protected:
+ Exception() = default;
+ void va_format(va_list args, const char * fmt);
char * error = NULL;
};
class ParserException : public Exception {
public:
- ParserException(const char * fmt, ...) : Exception(fmt) {}
+ ParserException(const char * fmt, ...);
};
class CircuitException : public Exception {
public:
- CircuitException(const char * fmt, ...) : Exception(fmt) {}
+ CircuitException(const char * fmt, ...);
};
+
diff --git a/Parser.cpp b/Parser.cpp
index 50833d8..08ff2b0 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -5,7 +5,6 @@
using std::getline;
-
size_t Parser::filter(char * input) {
size_t
len = strlen(input),
@@ -64,12 +63,11 @@ void Parser::parse(istream & input) {
try {
circuit->create(label, nodes);
} catch(CircuitException & c) {
- throw ParserException("Circuit error on line %u: %s", linenum, c.what());
+ throw ParserException("line %u: %s", linenum, c.what());
}
}
}
-
istream & operator >> (istream & s, Parser & parser) {
parser.parse(s);
return s;