shithub: choc

Download patch

ref: cce6e2644265e45ba140b5a2c5d8cbeccf093874
parent: 02c50cf81858d900e8f30299eb24d9aa04eb3f71
author: Simon Howard <[email protected]>
date: Sat Jul 23 14:50:34 EDT 2005

Use standard C file functions for WAD code

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

--- a/src/w_wad.c
+++ b/src/w_wad.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: w_wad.c 8 2005-07-23 16:44:57Z fraggle $
+// $Id: w_wad.c 15 2005-07-23 18:50:34Z fraggle $
 //
 // Copyright(C) 1993-1996 Id Software, Inc.
 // Copyright(C) 2005 Simon Howard
@@ -22,6 +22,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.3  2005/07/23 18:50:34  fraggle
+// Use standard C file functions for WAD code
+//
 // Revision 1.2  2005/07/23 16:44:57  fraggle
 // Update copyright to GNU GPL
 //
@@ -36,20 +39,11 @@
 
 
 static const char
-rcsid[] = "$Id: w_wad.c 8 2005-07-23 16:44:57Z fraggle $";
+rcsid[] = "$Id: w_wad.c 15 2005-07-23 18:50:34Z fraggle $";
 
 
-#ifdef NORMALUNIX
-#include <ctype.h>
-#include <sys/types.h>
-#include <string.h>
-#include <unistd.h>
-#include <malloc.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <alloca.h>
-#define O_BINARY		0
-#endif
+#include <stdio.h>
+#include <stdlib.h>
 
 #include "doomtype.h"
 #include "m_swap.h"
@@ -56,9 +50,6 @@
 #include "i_system.h"
 #include "z_zone.h"
 
-#ifdef __GNUG__
-#pragma implementation "w_wad.h"
-#endif
 #include "w_wad.h"
 
 
@@ -84,14 +75,21 @@
     while (*s) { *s = toupper(*s); s++; }
 }
 
-int filelength (int handle) 
+int filelength (FILE *handle)
 { 
-    struct stat	fileinfo;
+    long savedpos;
+    long length;
+    // save the current position in the file
+    savedpos = ftell(handle);
     
-    if (fstat (handle,&fileinfo) == -1)
-	I_Error ("Error fstating");
+    // jump to the end and find the length
+    fseek(handle, 0, SEEK_END);
+    length = ftell(handle);
 
-    return fileinfo.st_size;
+    // go back to the old location
+    fseek(handle, savedpos, SEEK_SET);
+
+    return length;
 }
 
 
@@ -156,12 +154,12 @@
     wadinfo_t		header;
     lumpinfo_t*		lump_p;
     unsigned		i;
-    int			handle;
+    FILE               *handle;
     int			length;
     int			startlump;
     filelump_t*		fileinfo;
     filelump_t		singleinfo;
-    int			storehandle;
+    FILE               *storehandle;
     
     // open the file and add to directory
 
@@ -173,7 +171,7 @@
 	reloadlump = numlumps;
     }
 		
-    if ( (handle = open (filename,O_RDONLY | O_BINARY)) == -1)
+    if ( (handle = fopen(filename,"rb")) == NULL)
     {
 	printf (" couldn't open %s\n",filename);
 	return;
@@ -194,7 +192,7 @@
     else 
     {
 	// WAD file
-	read (handle, &header, sizeof(header));
+	fread (&header, sizeof(header), 1, handle);
 	if (strncmp(header.identification,"IWAD",4))
 	{
 	    // Homebrew levels?
@@ -210,8 +208,8 @@
 	header.infotableofs = LONG(header.infotableofs);
 	length = header.numlumps*sizeof(filelump_t);
 	fileinfo = alloca (length);
-	lseek (handle, header.infotableofs, SEEK_SET);
-	read (handle, fileinfo, length);
+	fseek(handle, header.infotableofs, SEEK_SET);
+	fread(fileinfo, length, 1, handle);
 	numlumps += header.numlumps;
     }
 
@@ -224,7 +222,7 @@
 
     lump_p = &lumpinfo[startlump];
 	
-    storehandle = reloadname ? -1 : handle;
+    storehandle = reloadname ? NULL : handle;
 	
     for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
     {
@@ -235,7 +233,7 @@
     }
 	
     if (reloadname)
-	close (handle);
+	fclose (handle);
 }
 
 
@@ -252,7 +250,7 @@
     int			lumpcount;
     lumpinfo_t*		lump_p;
     unsigned		i;
-    int			handle;
+    FILE               *handle;
     int			length;
     filelump_t*		fileinfo;
 	
@@ -259,16 +257,16 @@
     if (!reloadname)
 	return;
 		
-    if ( (handle = open (reloadname,O_RDONLY | O_BINARY)) == -1)
+    if ( (handle = fopen(reloadname,"rb")) == NULL)
 	I_Error ("W_Reload: couldn't open %s",reloadname);
 
-    read (handle, &header, sizeof(header));
+    fread(&header, sizeof(header), 1, handle);
     lumpcount = LONG(header.numlumps);
     header.infotableofs = LONG(header.infotableofs);
     length = lumpcount*sizeof(filelump_t);
     fileinfo = alloca (length);
-    lseek (handle, header.infotableofs, SEEK_SET);
-    read (handle, fileinfo, length);
+    fseek(handle, header.infotableofs, SEEK_SET);
+    fread(fileinfo, length, 1, handle);
     
     // Fill in lumpinfo
     lump_p = &lumpinfo[reloadlump];
@@ -284,7 +282,7 @@
 	lump_p->size = LONG(fileinfo->size);
     }
 	
-    close (handle);
+    fclose(handle);
 }
 
 
@@ -448,7 +446,7 @@
 {
     int		c;
     lumpinfo_t*	l;
-    int		handle;
+    FILE       *handle;
 	
     if (lump >= numlumps)
 	I_Error ("W_ReadLump: %i >= numlumps",lump);
@@ -457,24 +455,24 @@
 	
     // ??? I_BeginRead ();
 	
-    if (l->handle == -1)
+    if (l->handle == NULL)
     {
 	// reloadable file, so use open / read / close
-	if ( (handle = open (reloadname,O_RDONLY | O_BINARY)) == -1)
+	if ( (handle = fopen(reloadname,"rb")) == NULL)
 	    I_Error ("W_ReadLump: couldn't open %s",reloadname);
     }
     else
 	handle = l->handle;
 		
-    lseek (handle, l->position, SEEK_SET);
-    c = read (handle, dest, l->size);
+    fseek(handle, l->position, SEEK_SET);
+    c = fread (dest, 1, l->size, handle);
 
     if (c < l->size)
 	I_Error ("W_ReadLump: only read %i of %i on lump %i",
 		 c,l->size,lump);	
 
-    if (l->handle == -1)
-	close (handle);
+    if (l->handle == NULL)
+	fclose (handle);
 		
     // ??? I_EndRead ();
 }