home · contact · privacy
In read_map_objects() and write_map_objects(), return error code right on error,...
[plomrogue] / src / map_objects.c
index 43406947afecebd8e1d838d8fe1ccff80e1b6396..4beb4fdefa174d412c340fc1309f546b30d77ca1 100644 (file)
@@ -2,9 +2,10 @@
 
 #include "map_objects.h"
 #include <stdlib.h> /* for malloc(), calloc(), free(), atoi() */
+#include <stdint.h> /* for uint8_t */
 #include <stdio.h> /* for FILE typedef */
 #include <string.h> /* for strchr(), strlen(), memcpy()  */
-#include "readwrite.h" /* for all the map object (def) loading/saving */
+#include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */
 #include "misc.h" /* for textfile_sizes(), find_passable_pos() */
 #include "main.h" /* for World struct */
 
@@ -23,8 +24,8 @@ static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
                                        void * start);
 static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
                                           void * start);
-static void write_map_objects_monsterdata(void * start, FILE * file);
-static void read_map_objects_monsterdata( void * start, FILE * file);
+static uint8_t write_map_objects_monsterdata(void * start, FILE * file);
+static uint8_t read_map_objects_monsterdata( void * start, FILE * file);
 
 
 
@@ -68,18 +69,18 @@ static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
 
 
 
-static void write_map_objects_monsterdata(void * start, FILE * file)
+static uint8_t write_map_objects_monsterdata(void * start, FILE * file)
 {
     struct Monster * m = (struct Monster *) start;
-    fputc(m->hitpoints, file);
+    return write_uint8(m->hitpoints, file);
 }
 
 
 
-static void read_map_objects_monsterdata (void * start, FILE * file)
+static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
 {
     struct Monster * m = (struct Monster *) start;
-    m->hitpoints = fgetc(file);
+    return read_uint8(file, &m->hitpoints);
 }
 
 
@@ -137,43 +138,58 @@ extern void init_map_object_defs(struct World * world, char * filename)
 
 
 
-extern void write_map_objects(struct World * world, void * start, FILE * file)
+extern uint8_t write_map_objects(struct World * world, void * start,
+                                 FILE * file)
 {
     struct MapObj * map_obj;
     struct MapObjDef * mod;
     for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
     {
-        fputc(map_obj->type, file);
-        write_uint16_bigendian(map_obj->pos.y + 1, file);
-        write_uint16_bigendian(map_obj->pos.x + 1, file);
+        if (   write_uint8(map_obj->type, file)
+            || write_uint16_bigendian(map_obj->pos.y + 1, file)
+            || write_uint16_bigendian(map_obj->pos.x + 1, file))
+        {
+            return 1;
+        }
         mod = get_map_obj_def(world, map_obj->type);
         if ('m' == mod->m_or_i)
         {
-            write_map_objects_monsterdata(map_obj, file);
+            if (write_map_objects_monsterdata(map_obj, file))
+            {
+                return 1;
+            }
         }
     }
-    write_uint16_bigendian(0, file);
+    return write_uint16_bigendian(0, file);
 }
 
 
 
-extern void read_map_objects(struct World * world, void * start, FILE * file)
+extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
 {
     struct MapObj * map_obj;
     struct MapObjDef * mod;
     size_t size;
-    char type;
+    uint8_t type;
     char first = 1;
     long pos;
+    uint16_t read_uint16 = 0;
     while (1)
     {
         pos = ftell(file);
-        if (0 == read_uint16_bigendian(file))
+        if (read_uint16_bigendian(file, &read_uint16))
+        {
+            return 1;
+        }
+        if (0 == read_uint16)
         {
             break;
         }
         fseek(file, pos, SEEK_SET);
-        type = fgetc(file);
+        if (read_uint8(file, &type))
+        {
+            return 1;
+        }
         mod = get_map_obj_def(world, type);
         if ('m' == mod->m_or_i)
         {
@@ -185,25 +201,34 @@ extern void read_map_objects(struct World * world, void * start, FILE * file)
         }
         map_obj = get_next_map_obj(start, &first, size, map_obj);
         map_obj->type = type;
-        map_obj->pos.y = read_uint16_bigendian(file) - 1;
-        map_obj->pos.x = read_uint16_bigendian(file) - 1;
+        if (   read_uint16_bigendian(file, &map_obj->pos.y)
+            || read_uint16_bigendian(file, &map_obj->pos.x))
+        {
+            return 1;
+        }
+        map_obj->pos.y--;
+        map_obj->pos.x--;
         if ('m' == mod->m_or_i)
         {
-            read_map_objects_monsterdata(map_obj, file);
+            if (read_map_objects_monsterdata(map_obj, file))
+            {
+                return 1;
+            }
         }
     }
     if (!first)
     {
         map_obj->next = 0;
     }
+    return 0;
 }
 
 
 
 extern void * build_map_objects(struct World * world, void * start, char def_id,
-                                unsigned char n)
+                                uint8_t n)
 {
-    unsigned char i;
+    uint8_t i;
     struct MapObj * mo;
     char first = 1;
     struct MapObjDef * mod = get_map_obj_def(world, def_id);