Bläddra i källkod

Started implmenting some kind of exceptions

Eiyeron Fulmincendii 10 år sedan
förälder
incheckning
f74ba1b048
3 ändrade filer med 62 tillägg och 0 borttagningar
  1. 2 0
      src/piaf/Archive.cpp
  2. 22 0
      src/piaf/Archive.h
  3. 38 0
      src/piaf/Exceptions.cpp

+ 2 - 0
src/piaf/Archive.cpp

@@ -15,6 +15,7 @@ using WalrusRPG::PIAF::Archive;
 using WalrusRPG::PIAF::File;
 using WalrusRPG::PIAF::FileType;
 using WalrusRPG::PIAF::CompressionType;
+using namespace WalrusRPG::PIAF::Exceptions;
 
 #if NSPIRE
 using namespace Nspire;
@@ -87,6 +88,7 @@ Archive::Archive(const char *filepath)
     {
         // TODO : throw Couldn't open
         // fprintf(stderr, "Unable to open %s\n", filepath);
+        throw PIAF::Exceptions::FileNotFound(__FILE__, __LINE__, (const char*)filepath);
     }
 
     // Loading stuff happens NOW

+ 22 - 0
src/piaf/Archive.h

@@ -1,6 +1,7 @@
 #ifndef INCLUDE_ARCHIVE_H
 #define INCLUDE_ARCHIVE_H
 
+#include <exception>
 #include <cstdint>
 #include <cstdio>
 #include <TINYSTL/string.h>
@@ -67,6 +68,27 @@ namespace WalrusRPG
             bool has(const char *filename);
             File get(const char *filename);
         };
+
+        namespace Exceptions
+        {
+            class PIAFException : public std::exception
+            {
+                private:
+                    char *msg;
+
+                public:
+                PIAFException(const char *file, const unsigned line, const char *message);
+                virtual ~PIAFException();
+
+                const char* what() const throw();
+            };
+
+            class FileNotFound : public PIAFException
+            {
+                public:
+                 FileNotFound(const char *file, const unsigned line, const char *message);
+            };
+        }
     }
 }
 

+ 38 - 0
src/piaf/Exceptions.cpp

@@ -0,0 +1,38 @@
+#include "Archive.h"
+#include <cmath>
+#include <cstring>
+
+using WalrusRPG::PIAF::Exceptions::PIAFException;
+using WalrusRPG::PIAF::Exceptions::FileNotFound;
+using namespace WalrusRPG::PIAF;
+
+PIAFException::PIAFException(const char *file, const unsigned line, const char *message)
+	: msg(nullptr)
+{
+	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';		
+	}
+}
+
+PIAFException::~PIAFException()
+{
+	if(msg != nullptr)
+		delete msg;
+}
+
+const char* PIAFException::what() const throw()
+{
+	return msg;
+}
+
+FileNotFound::FileNotFound(const char *file, const unsigned line, const char *message)
+: PIAFException(file, line, message)
+{
+
+}