Procházet zdrojové kódy

Added an option for exeptions.

Eiyeron Fulmincendii před 9 roky
rodič
revize
d202d954fa
4 změnil soubory, kde provedl 36 přidání a 14 odebrání
  1. 1 1
      platform/nspire/rules.mk
  2. 1 1
      platform/sfml/rules.mk
  3. 32 10
      src/piaf/Archive.cpp
  4. 2 2
      src/piaf/Exceptions.cpp

+ 1 - 1
platform/nspire/rules.mk

@@ -4,7 +4,7 @@ SRCS_C += $(wildcard $(nspire_LOCAL_PATH)/platform/*.c)
 SRCS_CPP += $(wildcard $(nspire_LOCAL_PATH)/*.cpp)
 INCLUDE += $(nspire_LOCAL_PATH)/public
 
-CFLAGS_COMMON += -marm -DTARGET_NSPIRE=1
+CFLAGS_COMMON += -marm -DTARGET_NSPIRE=1 -DWRPG_EXCEPTIONS=1
 YCM_EXTRA_CFLAGS := -m32 -I$(NDLESS_GIT)/ndless-sdk/include -I$(HOME)/.ndless/include
 
 CC = nspire-gcc

+ 1 - 1
platform/sfml/rules.mk

@@ -6,7 +6,7 @@ INCLUDE += $(sfml_LOCAL_PATH)/public
 
 LIBS += -lstdc++ -lsfml-window -lsfml-graphics -lsfml-system
 
-CFLAGS_COMMON += -DTARGET_SFML=1
+CFLAGS_COMMON += -DTARGET_SFML=1 -DWRPG_EXCEPTIONS=1
 
 CC = clang
 CPP = clang++

+ 32 - 10
src/piaf/Archive.cpp

@@ -76,7 +76,9 @@ Archive::Archive(const char *filepath)
     // Null pointer exception trigger
     if (filepath == nullptr)
     {
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Null path given", __FILE__);
+#endif
     }
     // Solves the absolute path for given relative path.
     // Must be needed in targets like Ndless as it doesn't support environment
@@ -88,7 +90,9 @@ Archive::Archive(const char *filepath)
     // Again another null pointer trigger
     if (file == nullptr || file == NULL)
     {
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Missing file : %s", __FILE__, filepath);
+#endif
     }
 
     // Loading stuff happens NOW
@@ -99,8 +103,10 @@ Archive::Archive(const char *filepath)
     // File to small exception trigger
     if (filesize < 32)
     {
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: File too small (%s): %d", __FILE__, filepath,
                                   filesize);
+#endif
     }
 
     // Tempoary buffer to contain the header.
@@ -108,35 +114,43 @@ Archive::Archive(const char *filepath)
     // Read the headers and trigger exceptions on errors
     if (fread(header_container, sizeof(char), 32, file) != 32)
     {
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Errorneous header : %s", __FILE__, filepath);
+#endif
     }
     // Check if the magic cookie is the same.
     // It's a first way to detect if the file is correctly an archive.
     if (strncmp(header_container, "WRPGPIAF", 8) != 0)
     {
-        // TODO throw bad header
-        // fprintf(stderr, "Bad header magic word\n");
+// TODO throw bad header
+// fprintf(stderr, "Bad header magic word\n");
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Magic cookie mismatch : %s", __FILE__, filepath);
+#endif
     }
     // 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]);
     uint32_t calculated_checksum = crc32(0L, (unsigned char *) &header_container[16], 16);
     if (expected_checksum != calculated_checksum)
     {
-        // TODO throw bad checksum
-        // fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
-        // calculated_checksum);
+// TODO throw bad checksum
+// fprintf(stderr, "Bad header checksum : %x != %x\n", expected_checksum,
+// calculated_checksum);
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Bad checksum : %s", __FILE__, filepath);
+#endif
     }
 
     // TODO : version checking
     version = read_big_endian_value<uint32_t>(&header_container[16]);
     if (version != ARCHIVE_VERSION)
     {
-        // std::exception up;
-        // throw up; // haha
+// std::exception up;
+// throw up; // haha
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("%s: Wrong(%s) : %08x is not supported by %08x",
                                   __FILE__, filepath, version, ARCHIVE_VERSION);
+#endif
     }
 
 
@@ -151,9 +165,11 @@ Archive::Archive(const char *filepath)
     uint64_t calculated_data_size = filesize - 32 - 24 * nb_files;
     if (data_size != calculated_data_size)
     {
-        // fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
-        // calculated_data_size);
+// fprintf(stderr, "Bad data size : expected %u, got %lld\n", data_size,
+// calculated_data_size);
+#ifdef WRPG_EXCEPTIONS
         throw PIAF::PIAFException("Data size mismatch", __LINE__, filepath);
+#endif
     }
     // Check if there are files to manage.
     if (nb_files != 0)
@@ -166,14 +182,18 @@ Archive::Archive(const char *filepath)
         fseek(file, 32, SEEK_SET);
         if (fread(file_entry_data, sizeof(char), 24 * nb_files, file) < 24 * nb_files)
         {
+#ifdef WRPG_EXCEPTIONS
             throw PIAF::PIAFException("Can't read file entry data", __LINE__, filepath);
+#endif
         }
         // Compare and trigger an exception if the checksum doesn't match.
         if (expected_filetable_checksum !=
             crc32(0L, (unsigned char *) file_entry_data, 24 * nb_files))
         {
-            // fprintf(stderr, "Bad filetable checksum\n");
+// fprintf(stderr, "Bad filetable checksum\n");
+#ifdef WRPG_EXCEPTIONS
             throw PIAF::PIAFException("Bad Filetable checksum", __LINE__, filepath);
+#endif
         }
         // Create the filetable.
         entries = new File[nb_files];
@@ -248,8 +268,10 @@ File Archive::get(const char *filename)
                 if (fread(data, sizeof(uint8_t), entries[index].file_size, file) !=
                     entries[index].file_size)
                 {
+#ifdef WRPG_EXCEPTIONS
                     throw PIAF::PIAFException("%s: couldn't load %s from an archive.",
                                               filename);
+#endif
                 }
                 else
                 {

+ 2 - 2
src/piaf/Exceptions.cpp

@@ -13,7 +13,7 @@ PIAFException::PIAFException(const char *format, ...) : msg("")
     va_list list;
     va_start(list, format);
     vsnprintf(msg, 1024, format, list);
-    log(msg);
+    error(msg);
     va_end(list);
 }
 
@@ -24,4 +24,4 @@ PIAFException::~PIAFException()
 const char *PIAFException::what() const throw()
 {
     return msg;
-}
+}