shithub: choc

Download patch

ref: 5547ba06ca0e267fa82a6b9b7953a980e0876e61
parent: 25f6da1861deedeb2d0650ecf0ebe42a83b682c8
author: Simon Howard <[email protected]>
date: Sat Oct 8 16:54:16 EDT 2005

Proper dehacked error/warning framework. Catch a load more errors.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 175

--- a/src/deh_ammo.c
+++ b/src/deh_ammo.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_ammo.c 163 2005-10-04 22:04:06Z fraggle $
+// $Id: deh_ammo.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.4  2005/10/08 20:54:15  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.3  2005/10/04 22:04:06  fraggle
 // Parse dehacked "Ammo" sections properly
 //
@@ -45,6 +48,7 @@
 #include "doomdef.h"
 #include "doomtype.h"
 #include "deh_defs.h"
+#include "deh_io.h"
 #include "deh_main.h"
 #include "p_local.h"
 
@@ -52,11 +56,18 @@
 {
     int ammo_number = 0;
 
-    sscanf(line, "Ammo %i", &ammo_number);
+    if (sscanf(line, "Ammo %i", &ammo_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     if (ammo_number < 0 || ammo_number >= NUMAMMO)
+    {
+        DEH_Warning(context, "Invalid ammo number: %i", ammo_number);
         return NULL;
-
+    }
+    
     return &maxammo[ammo_number];
 }
 
@@ -77,6 +88,7 @@
     {
         // Failed to parse
 
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
 
@@ -90,8 +102,7 @@
         maxammo[ammo_number] = ivalue;
     else
     {
-        fprintf(stderr, "DEH_AmmoParseLine: field named '%s' not found\n",
-                variable_name);
+        DEH_Warning(context, "Field named '%s' not found", variable_name);
     }
 }
 
--- a/src/deh_cheat.c
+++ b/src/deh_cheat.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_cheat.c 162 2005-10-04 21:41:42Z fraggle $
+// $Id: deh_cheat.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.2  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.1  2005/10/04 21:41:42  fraggle
 // Rewrite cheats code.  Add dehacked cheat replacement.
 //
@@ -40,7 +43,9 @@
 
 #include "doomdef.h"
 #include "doomtype.h"
+
 #include "deh_defs.h"
+#include "deh_io.h"
 #include "deh_main.h"
 #include "am_map.h"
 #include "st_stuff.h"
@@ -97,10 +102,11 @@
     unsigned char *value;
     int i;
 
-    if (!DEH_ParseAssignment(line, &variable_name, (char *) &value))
+    if (!DEH_ParseAssignment(line, &variable_name, (char **) &value))
     {
         // Failed to parse
-        
+
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
 
@@ -108,8 +114,7 @@
 
     if (cheat == NULL)
     {
-        fprintf(stderr, "DEH_ParseCheatLine: Unknown cheat '%s'\n", 
-                variable_name);
+        DEH_Warning(context, "Unknown cheat '%s'", variable_name);
         return;
     }
 
--- a/src/deh_frame.c
+++ b/src/deh_frame.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_frame.c 157 2005-10-03 11:08:16Z fraggle $
+// $Id: deh_frame.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.5  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.4  2005/10/03 11:08:16  fraggle
 // Replace end of section functions with NULLs as they arent currently being
 // used for anything.
@@ -49,6 +52,7 @@
 #include "info.h"
 
 #include "deh_defs.h"
+#include "deh_io.h"
 #include "deh_main.h"
 #include "deh_mapping.h"
 
@@ -66,10 +70,17 @@
     int frame_number = 0;
     state_t *state;
     
-    sscanf(line, "Frame %i", &frame_number);
+    if (sscanf(line, "Frame %i", &frame_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
     
     if (frame_number < 0 || frame_number >= NUMSTATES)
+    {
+        DEH_Warning(context, "Invalid frame number: %i", frame_number);
         return NULL;
+    }
 
     state = &states[frame_number];
 
@@ -93,11 +104,10 @@
     {
         // Failed to parse
 
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
     
-//    printf("Set %s to %s for state\n", variable_name, value);
-
     // all values are integers
 
     ivalue = atoi(value);
@@ -104,7 +114,7 @@
     
     // set the appropriate field
 
-    DEH_SetMapping(&state_mapping, state, variable_name, ivalue);
+    DEH_SetMapping(context, &state_mapping, state, variable_name, ivalue);
 }
 
 deh_section_t deh_section_frame =
--- a/src/deh_io.c
+++ b/src/deh_io.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_io.c 153 2005-10-02 23:49:01Z fraggle $
+// $Id: deh_io.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.2  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.1  2005/10/02 23:49:01  fraggle
 // The beginnings of dehacked support
 //
@@ -31,10 +34,12 @@
 //
 //-----------------------------------------------------------------------------
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "i_system.h"
 #include "z_zone.h"
 
 #include "deh_defs.h"
@@ -43,6 +48,9 @@
 struct deh_context_s
 {
     FILE *stream;
+    char *filename;
+    int linenum;
+    boolean last_was_newline;
     char *readbuffer;
     int readbuffer_size;
 };
@@ -67,6 +75,9 @@
 
     context->readbuffer_size = 128;
     context->readbuffer = Z_Malloc(context->readbuffer_size, PU_STATIC, NULL);
+    context->filename = filename;
+    context->linenum = 0;
+    context->last_was_newline = true;
 
     return context;
 }
@@ -104,6 +115,15 @@
 
     } while (result == '\r');
 
+    // Track the current line number
+
+    if (context->last_was_newline)
+    {
+        ++context->linenum;
+    }
+    
+    context->last_was_newline = result == '\n';
+    
     return result;
 }
 
@@ -129,7 +149,6 @@
 
 char *DEH_ReadLine(deh_context_t *context)
 {
-    char *p;
     int c;
     int pos;
 
@@ -166,4 +185,33 @@
     
     return context->readbuffer;
 }
+
+void DEH_Warning(deh_context_t *context, char *msg, ...)
+{
+    va_list args;
+
+    va_start(args, msg);
+    
+    fprintf(stderr, "%s:%i: warning: ", context->filename, context->linenum);
+    vfprintf(stderr, msg, args);
+    fprintf(stderr, "\n");
+
+    va_end(args);
+}
+
+void DEH_Error(deh_context_t *context, char *msg, ...)
+{
+    va_list args;
+
+    va_start(args, msg);
+    
+    fprintf(stderr, "%s:%i: ", context->filename, context->linenum);
+    vfprintf(stderr, msg, args);
+    fprintf(stderr, "\n");
+
+    va_end(args);
+
+    I_Error("Error parsing dehacked file");
+}
+
 
--- a/src/deh_io.h
+++ b/src/deh_io.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_io.h 153 2005-10-02 23:49:01Z fraggle $
+// $Id: deh_io.h 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.2  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.1  2005/10/02 23:49:01  fraggle
 // The beginnings of dehacked support
 //
@@ -40,6 +43,8 @@
 void DEH_CloseFile(deh_context_t *context);
 int DEH_GetChar(deh_context_t *context);
 char *DEH_ReadLine(deh_context_t *context);
+void DEH_Error(deh_context_t *context, char *msg, ...);
+void DEH_Warning(deh_context_t *context, char *msg, ...);
 
 #endif /* #ifndef DEH_IO_H */
 
--- a/src/deh_mapping.c
+++ b/src/deh_mapping.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_mapping.c 173 2005-10-08 20:14:24Z fraggle $
+// $Id: deh_mapping.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.3  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.2  2005/10/08 20:14:24  fraggle
 // Add the ability to specify unsupported fields
 //
@@ -47,7 +50,7 @@
 // Set the value of a particular field in a structure by name
 //
 
-boolean DEH_SetMapping(deh_mapping_t *mapping, 
+boolean DEH_SetMapping(deh_context_t *context, deh_mapping_t *mapping, 
                        void *structptr, char *name, int value)
 {
     int i;
@@ -62,8 +65,7 @@
 
             if (entry->location == NULL)
             {
-                fprintf(stderr, "DEH_SetMapping: Field '%s' is unsupported.\n",
-                        name);
+                DEH_Warning(context, "Field '%s' is unsupported", name);
                 return false;
             }
 
@@ -87,7 +89,7 @@
                     * ((unsigned long long *) location) = value;
                     break;
                 default:
-                    fprintf(stderr, "DEH_SetMapping: Unknown field type for %s\n", name);
+                    DEH_Error(context, "Unknown field type for '%s' (BUG)", name);
                     return false;
             }
 
@@ -97,8 +99,7 @@
 
     // field with this name not found
 
-    fprintf(stderr, "DEH_SetMapping: field named '%s' not found\n",
-            name);
+    DEH_Warning(context, "Field named '%s' not found", name);
 
     return false;
 }
--- a/src/deh_mapping.h
+++ b/src/deh_mapping.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_mapping.h 173 2005-10-08 20:14:24Z fraggle $
+// $Id: deh_mapping.h 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.3  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.2  2005/10/08 20:14:24  fraggle
 // Add the ability to specify unsupported fields
 //
@@ -41,6 +44,7 @@
 #define DEH_MAPPING_H
 
 #include "doomtype.h"
+#include "deh_io.h"
 
 #define DEH_BEGIN_MAPPING(mapping_name, structname)           \
     static structname deh_mapping_base;                       \
@@ -90,7 +94,8 @@
     deh_mapping_entry_t entries[MAX_MAPPING_ENTRIES];
 };
 
-boolean DEH_SetMapping(deh_mapping_t *mapping, void *structptr, char *name, int value);
+boolean DEH_SetMapping(deh_context_t *context, deh_mapping_t *mapping, 
+                       void *structptr, char *name, int value);
 
 #endif /* #ifndef DEH_MAPPING_H */
 
--- a/src/deh_misc.c
+++ b/src/deh_misc.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_misc.c 164 2005-10-04 22:10:32Z fraggle $
+// $Id: deh_misc.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.2  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.1  2005/10/04 22:10:32  fraggle
 // Dehacked "Misc" section parser (currently a dummy)
 //
@@ -34,11 +37,11 @@
 #include "doomdef.h"
 #include "doomtype.h"
 #include "deh_defs.h"
+#include "deh_io.h"
 
 static void *DEH_MiscStart(deh_context_t *context, char *line)
 {
-    fprintf(stderr, "DEH_MiscStart: Warning: dehacked 'Misc' sections are "
-                    "not yet supported.\n");
+    DEH_Warning(context, "Dehacked 'Misc' sections are not supported yet.");
     return NULL;
 }
 
--- a/src/deh_ptr.c
+++ b/src/deh_ptr.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_ptr.c 157 2005-10-03 11:08:16Z fraggle $
+// $Id: deh_ptr.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.4  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.3  2005/10/03 11:08:16  fraggle
 // Replace end of section functions with NULLs as they arent currently being
 // used for anything.
@@ -46,6 +49,7 @@
 #include "info.h"
 
 #include "deh_defs.h"
+#include "deh_io.h"
 #include "deh_main.h"
 
 static actionf_t codeptrs[NUMSTATES];
@@ -67,10 +71,17 @@
     // FIXME: can the third argument here be something other than "Frame"
     // or are we ok?
 
-    sscanf(line, "%*s %*i (%*s %i)", &frame_number);
+    if (sscanf(line, "Pointer %*i (%*s %i)", &frame_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     if (frame_number < 0 || frame_number >= NUMSTATES)
+    {
+        DEH_Warning(context, "Invalid frame number: %i", frame_number);
         return NULL;
+    }
 
     return &states[frame_number];
 }
@@ -91,7 +102,7 @@
     if (!DEH_ParseAssignment(line, &variable_name, &value))
     {
         // Failed to parse
-
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
     
@@ -107,8 +118,7 @@
     {
         if (ivalue < 0 || ivalue >= NUMSTATES)
         {
-            fprintf(stderr, "DEH_PointerParseLine: Invalid state %i\n",
-                    ivalue);
+            DEH_Warning(context, "Invalid state '%i'", ivalue);
         }
         else
         {        
@@ -117,8 +127,7 @@
     }
     else
     {
-        fprintf(stderr, "DEH_PointerParseLine: Unknown variable name '%s'\n",
-                variable_name);
+        DEH_Warning(context, "Unknown variable name '%s'", variable_name);
     }
 }
 
--- a/src/deh_sound.c
+++ b/src/deh_sound.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_sound.c 174 2005-10-08 20:14:38Z fraggle $
+// $Id: deh_sound.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.2  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.1  2005/10/08 20:14:38  fraggle
 // Dehacked "Sound" section support
 //
@@ -60,10 +63,17 @@
     int sound_number = 0;
     sfxinfo_t *sfx;
     
-    sscanf(line, "Sound %i", &sound_number);
+    if (sscanf(line, "Sound %i", &sound_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     if (sound_number < 0 || sound_number >= NUMSFX)
+    {
+        DEH_Warning(context, "Invalid sound number: %i", sound_number);
         return NULL;
+    }
     
     sfx = &S_sfx[sound_number];
     
@@ -86,7 +96,7 @@
     if (!DEH_ParseAssignment(line, &variable_name, &value))
     {
         // Failed to parse
-
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
     
@@ -96,7 +106,7 @@
     
     // Set the field value
 
-    DEH_SetMapping(&sound_mapping, sfx, variable_name, ivalue);
+    DEH_SetMapping(context, &sound_mapping, sfx, variable_name, ivalue);
 
 }
 
--- a/src/deh_text.c
+++ b/src/deh_text.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_text.c 160 2005-10-03 21:39:39Z fraggle $
+// $Id: deh_text.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.4  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.3  2005/10/03 21:39:39  fraggle
 // Dehacked text substitutions
 //
@@ -174,7 +177,11 @@
     int fromlen, tolen;
     int i;
     
-    sscanf(line, "Text %i %i", &fromlen, &tolen);
+    if (sscanf(line, "Text %i %i", &fromlen, &tolen) != 2)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     sub = Z_Malloc(sizeof(deh_substitution_t), PU_STATIC, NULL);
     sub->from_text = Z_Malloc(fromlen + 1, PU_STATIC, NULL);
--- a/src/deh_thing.c
+++ b/src/deh_thing.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_thing.c 157 2005-10-03 11:08:16Z fraggle $
+// $Id: deh_thing.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.4  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.3  2005/10/03 11:08:16  fraggle
 // Replace end of section functions with NULLs as they arent currently being
 // used for anything.
@@ -81,13 +84,20 @@
     int thing_number = 0;
     mobjinfo_t *mobj;
     
-    sscanf(line, "Thing %i", &thing_number);
+    if (sscanf(line, "Thing %i", &thing_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     // dehacked files are indexed from 1
     --thing_number;
 
     if (thing_number < 0 || thing_number >= NUMMOBJTYPES)
+    {
+        DEH_Warning("Invalid thing number: %i", thing_number);
         return NULL;
+    }
     
     mobj = &mobjinfo[thing_number];
     
@@ -111,6 +121,7 @@
     {
         // Failed to parse
 
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
     
@@ -122,7 +133,7 @@
     
     // Set the field value
 
-    DEH_SetMapping(&thing_mapping, mobj, variable_name, ivalue);
+    DEH_SetMapping(context, &thing_mapping, mobj, variable_name, ivalue);
 }
 
 deh_section_t deh_section_thing =
--- a/src/deh_weapon.c
+++ b/src/deh_weapon.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: deh_weapon.c 158 2005-10-03 13:21:11Z fraggle $
+// $Id: deh_weapon.c 175 2005-10-08 20:54:16Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.5  2005/10/08 20:54:16  fraggle
+// Proper dehacked error/warning framework.  Catch a load more errors.
+//
 // Revision 1.4  2005/10/03 13:21:11  fraggle
 // Weapons mapping code
 //
@@ -66,11 +69,18 @@
 {
     int weapon_number = 0;
 
-    sscanf(line, "Weapon %i", &weapon_number);
+    if (sscanf(line, "Weapon %i", &weapon_number) != 1)
+    {
+        DEH_Warning(context, "Parse error on section start");
+        return NULL;
+    }
 
     if (weapon_number < 0 || weapon_number >= NUMWEAPONS)
+    {
+        DEH_Warning(context, "Invalid weapon number: %i", weapon_number);
         return NULL;
-
+    }
+    
     return &weaponinfo[weapon_number];
 }
 
@@ -88,13 +98,14 @@
     if (!DEH_ParseAssignment(line, &variable_name, &value))
     {
         // Failed to parse
-       
+
+        DEH_Warning(context, "Failed to parse assignment");
         return;
     }
 
     ivalue = atoi(value);
 
-    DEH_SetMapping(&weapon_mapping, weapon, variable_name, ivalue);
+    DEH_SetMapping(context, &weapon_mapping, weapon, variable_name, ivalue);
 }
 
 deh_section_t deh_section_weapon =