Quellcode durchsuchen

Using va_args to make the exception flexible enough

Eiyeron Fulmincendii vor 10 Jahren
Ursprung
Commit
d2b3849f31
3 geänderte Dateien mit 17 neuen und 29 gelöschten Zeilen
  1. 7 16
      src/piaf/Archive.cpp
  2. 3 2
      src/piaf/Archive.h
  3. 7 11
      src/piaf/Exceptions.cpp

+ 7 - 16
src/piaf/Archive.cpp

@@ -73,9 +73,7 @@ Archive::Archive(const char *filepath)
     // Null pointer exception trigger
     if (filepath == nullptr)
     {
-        // TODO : throw NPE
-        // fprintf(stderr, "Null filepath\n");
-        throw PIAF::PIAFException(__FILE__, __LINE__, "Null path given");
+        throw PIAF::PIAFException("%s: Null path given", __FILE__);
     }
     // Solves the absolute path for given relative path.
     // Must be needed in targets like Ndless as it doesn't support environment
@@ -87,9 +85,7 @@ Archive::Archive(const char *filepath)
     // Again another null pointer trigger
     if (file == nullptr || file == NULL)
     {
-        // TODO : throw Couldn't open
-        // fprintf(stderr, "Unable to open %s\n", filepath);
-        throw PIAF::PIAFException("Missing file", __LINE__, (const char*)filepath);
+        throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
     }
 
     // Loading stuff happens NOW
@@ -100,9 +96,7 @@ Archive::Archive(const char *filepath)
     // File to small exception trigger
     if (filesize < 32)
     {
-        throw PIAF::PIAFException("File too small", filesize, filepath);
-        // TODO : throw file too small
-        // fprintf(stderr, "File too small\n");
+        throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath, filesize);
     }
 
     // Tempoary buffer to contain the header.
@@ -110,10 +104,7 @@ Archive::Archive(const char *filepath)
     // Read the headers and trigger exceptions on errors
     if (fread(header_container, sizeof(char), 32, file) != 32)
     {
-        // So we coudln't load the whole header.
-        // TODO : check flags and return correct exceptions
-        // fprintf(stderr, "Error loading header\n");
-        throw PIAF::PIAFException("Errorneous header", __LINE__, filepath);
+        throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
     }
     // Check if the magic cookie is the same.
     // It's a first way to detect if the file is correctly an archive.
@@ -121,7 +112,7 @@ Archive::Archive(const char *filepath)
     {
         // TODO throw bad header
         // fprintf(stderr, "Bad header magic word\n");
-        throw PIAF::PIAFException("Wrong header", __LINE__, filepath);
+        throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__, filepath);
     }
     // Checksum time! Let's check if the header hasn"t been altered.
     uint32_t expected_checksum = read_big_endian_value<uint32_t>(&header_container[8]);
@@ -131,7 +122,7 @@ Archive::Archive(const char *filepath)
         // TODO throw bad checksum
         // fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
         // calculated_checksum);
-        throw PIAF::PIAFException("Bad checksum", __LINE__, filepath);
+        throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
     }
 
     // TODO : version checking
@@ -140,7 +131,7 @@ Archive::Archive(const char *filepath)
     {
         // std::exception up;
         // throw up; // haha
-        throw PIAF::PIAFException("Wrong archive version", version, filepath);
+        throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x", __FILE__, filepath, version, ARCHIVE_VERSION);
     }
 
 

+ 3 - 2
src/piaf/Archive.h

@@ -4,6 +4,7 @@
 #include <exception>
 #include <cstdint>
 #include <cstdio>
+#include <cstdarg>
 #include <TINYSTL/string.h>
 
 namespace WalrusRPG
@@ -72,10 +73,10 @@ namespace WalrusRPG
         class PIAFException : public std::exception
         {
             private:
-                char *msg;
+                char msg[1024];
 
             public:
-            PIAFException(const char *file, const unsigned line, const char *message);
+            PIAFException(const char *format, ...);
             virtual ~PIAFException();
 
             const char* what() const throw();

+ 7 - 11
src/piaf/Exceptions.cpp

@@ -1,22 +1,18 @@
 #include "Archive.h"
 #include <cmath>
 #include <cstring>
+#include <cstdarg>
 
 using WalrusRPG::PIAF::PIAFException;
 using namespace WalrusRPG::PIAF;
 
-PIAFException::PIAFException(const char *file, const unsigned line, const char *message)
-	: msg(nullptr)
+PIAFException::PIAFException(const char *format, ...)
+	: msg("")
 {
-	unsigned length_of_digit = ((line != 0) ? log10(line) + 1 : 1);
-	unsigned length_of_string = strlen(file) + 3 + length_of_digit + 3 + strlen(message);
-
-	msg = new char[length_of_string + 1];
-	if(msg != nullptr)
-	{
-		snprintf(msg, length_of_string+1, "%s - %d : %s", file, line, message);
-		msg[length_of_string] = '\0';		
-	}
+	va_list list;
+	va_start(list, format);
+	vsnprintf(msg, 1024, format, list);
+	va_end(list);
 }
 
 PIAFException::~PIAFException()