Example #1
0
bool ReadObject(HSQUIRRELVM v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &o)
{
	SQObjectType t;
	_CHECK_IO(SafeRead(v,read,up,&t,sizeof(SQObjectType)));
	switch(t){
	case OT_STRING:{
		SQInteger len;
		_CHECK_IO(SafeRead(v,read,up,&len,sizeof(SQInteger)));
		_CHECK_IO(SafeRead(v,read,up,_ss(v)->GetScratchPad(rsl(len)),rsl(len)));
		o=SQString::Create(_ss(v),_ss(v)->GetScratchPad(-1),len);
				   }
		break;
	case OT_INTEGER:{
		SQInteger i;
		_CHECK_IO(SafeRead(v,read,up,&i,sizeof(SQInteger))); o = i; break;
					}
	case OT_FLOAT:{
		SQFloat f;
		_CHECK_IO(SafeRead(v,read,up,&f,sizeof(SQFloat))); o = f; break;
				  }
	case OT_NULL:
		o=_null_;
		break;
	default:
		v->Raise_Error(_SC("cannot serialize a %s"),IdType2Name(t));
		return false;
	}
	return true;
}
Example #2
0
/*
=================
TEX_InitFromWad
=================
*/
void	TEX_InitFromWad (char *path)
{
  int			i, j = 0;
  char *word, buf[2048];
  
  while ((word = (char *) strsep(&path, ";")) && j < NUM_WADS) {
    //    sprintf(buf, "%s/%s", gamedir, word);
    strcpy(buf, word);
    printf("reading wadfile: '%s'\n", buf);
    texfile[j] = SafeOpenRead (buf);

    SafeRead (texfile[j], &wadinfo[j], sizeof(wadinfo_t));
    if (strncmp (wadinfo[j].identification, "WAD2", 4))
      Error ("TEX_InitFromWad: %s isn't a wadfile",buf);
    wadinfo[j].numlumps = LittleLong(wadinfo[j].numlumps);
    wadinfo[j].infotableofs = LittleLong(wadinfo[j].infotableofs);
    fseek (texfile[j], wadinfo[j].infotableofs, SEEK_SET);
    lumpinfo[j] = malloc(wadinfo[j].numlumps*sizeof(lumpinfo_t));
    SafeRead (texfile[j], lumpinfo[j], wadinfo[j].numlumps*sizeof(lumpinfo_t));
    
    for (i=0 ; i<wadinfo[j].numlumps ; i++)
      {
	CleanupName (lumpinfo[j][i].name, lumpinfo[j][i].name);
	lumpinfo[j][i].filepos = LittleLong(lumpinfo[j][i].filepos);
	lumpinfo[j][i].disksize = LittleLong(lumpinfo[j][i].disksize);
      }
    j++;
  }
}
Example #3
0
/*
 * ====================
 * W_OpenWad
 * ====================
 */
void
W_OpenWad(char *filename)
{
    lumpinfo_t *lump_p;
    unsigned i;
    int length;

    /* open the file and add to directory */
    wadhandle = SafeOpenRead(filename);
    SafeRead(wadhandle, &header, sizeof(header));

    if (strncmp(header.identification, "WAD2", 4))
        Error("Wad file %s doesn't have WAD2 id\n", filename);

    header.numlumps = LittleLong(header.numlumps);
    header.infotableofs = LittleLong(header.infotableofs);

    numlumps = header.numlumps;

    length = numlumps * sizeof(lumpinfo_t);
    lumpinfo = malloc(length);
    lump_p = lumpinfo;

    fseek(wadhandle, header.infotableofs, SEEK_SET);
    SafeRead(wadhandle, lumpinfo, length);

    /* Fill in lumpinfo */
    for (i = 0; i < numlumps; i++, lump_p++) {
        lump_p->filepos = LittleLong(lump_p->filepos);
        lump_p->size = LittleLong(lump_p->size);
    }
}
char* ReadLine(int fd)
{
	char buf[N];
	char* result;

	int i = 0, bytesRead = 0;
	for(; i<N; i++)
	{
		buf[i] = 0;
	}
	bytesRead = SafeRead(fd, buf, N);
	if(bytesRead <= 0) return NULL;
	
	for(i=0; i<bytesRead; i++)
		if(buf[i]=='\n')
		{
			buf[i]=0;
			break;
		}
	result = malloc(i+1);
	if(result==NULL)
        FILE_ERR("malloc failed!");
	result[i]=0;
	memcpy(result, buf, i);
	if(-1 == lseek(fd, i - bytesRead + 1, SEEK_CUR))
		FILE_ERR("Error occured using lseek in ReadLine function!");
	return result;
}
Example #5
0
/*
===========
PackFile

Copy a file into the pak file
===========
*/
void PackFile (char *src, char *name)
{
	FILE	*in;
	int		remaining, count;
	char	buf[4096];
	
	if ( (byte *)pf - (byte *)pfiles > sizeof(pfiles) )
		Error ("Too many files in pak file");
	
	in = SafeOpenRead (src);
	remaining = filelength (in);

	pf->filepos = LittleLong (ftell (packhandle));
	pf->filelen = LittleLong (remaining);
	strcpy (pf->name, name);
	printf ("%64s : %7i\n", pf->name, remaining);

	packbytes += remaining;
	
	while (remaining)
	{
		if (remaining < sizeof(buf))
			count = remaining;
		else
			count = sizeof(buf);
		SafeRead (in, buf, count);
		SafeWrite (packhandle, buf, count);
		remaining -= count;
	}

	fclose (in);
	pf++;
}
Example #6
0
//===========================================================================
//
// Parameter:			-
// Returns:				-
// Changes Globals:		-
//===========================================================================
int ReadQuakeFile(quakefile_t *qf, void *buffer, int length)
{
	FILE *fp;
	unzFile zf;

	if (qf->zipfile)
	{
		//open the zip file
		zf = unzOpen(qf->pakfile);
		//set the file position in the zip file (also sets the current file info)
		unzSetOffset(zf, qf->pos);
		//open the Quake file in the zip file
		unzOpenCurrentFile(zf);
		//read the Quake file from the zip file
		length = unzReadCurrentFile(zf, buffer, length);
		//close the Quake file in the zip file
		unzCloseCurrentFile(zf);
		//close the zip file
		unzClose(zf);

		return length;
	} //end if
	else
	{
		fp = SafeOpenRead(qf->filename);
		SafeRead(fp, buffer, length);
		fclose(fp);

		return length;
	} //end else
} //end of the function ReadQuakeFile
Example #7
0
    int Ksock::Receive(Message& ioMessage)
    {
        /* valgrind believes that msgLen is uninitialised if Receive() returns 0;
         * this is, as far as I can tell, untrue, but I've added a spurious 
         * initialisation here to stop the complaints so I can concentrate on
         * more serious issues - rrw 2010-12-09
         */
        uint32_t msgLen(0);

        if (!ioMessage.IsEmpty())
            return Error::MessageIsNotEmpty;

        int rv = ioctl(mDevice.mFd, KBUS_IOC_NEXTMSG, &msgLen);
        if (rv < 0) return -errno;

        if (!msgLen)
        {
            // There was no next message.
            return 0;
        }


        ioMessage.mData.resize(msgLen);
        rv = SafeRead(mDevice.mFd, (uint8_t *)(&ioMessage.mData[0]), msgLen);
        if (rv  < 0) return rv;

        kbus_message_header *hdr = (kbus_message_header *)(&ioMessage.mData[0]);

        ioMessage.mName = std::string(kbus_msg_name_ptr(hdr), hdr->name_len);
        ioMessage.mIsEntire = true;
        ioMessage.mIsEmpty = false;
        ioMessage.mPointyData = NULL;
        ioMessage.mPointyLen = 0;
        return 1;
    }
Example #8
0
int32_t SCRIPT_Load(char * filename)
{
    int32_t s,h,l;
    char *b;

    h = SafeOpenRead(filename, filetype_binary);
    l = SafeFileLength(h)+1;
    b = (char *)Bmalloc(l);
    SafeRead(h,b,l-1);
    b[l-1] = '\n';	// JBF 20040111: evil nasty hack to trick my evil nasty parser
    SafeClose(h);

    s = SCRIPT_Init(filename);
    if (s<0)
    {
        Bfree(b);
        return -1;
    }

    SCRIPT_ParseBuffer(s,b,l);

    Bfree(b);

    return s;
}
Example #9
0
//===========================================================================
//
// Parameter:           -
// Returns:             -
// Changes Globals:     -
//===========================================================================
int ReadQuakeFile(quakefile_t *qf, void *buffer, int offset, int length)
{
	FILE *fp;
	int read;
	unzFile zf;
	char tmpbuf[1024];

	if(qf->zipfile)
	{
		//open the zip file
		zf = unzOpen(qf->pakfile);
		//set the file pointer
		qf->zipinfo.file = ((unz_s *) zf)->file;
		//open the Quake file in the zip file
		unzOpenCurrentFile(&qf->zipinfo);

		//
		while(offset > 0)
		{
			read = offset;

			if(read > sizeof(tmpbuf))
			{
				read = sizeof(tmpbuf);
			}

			unzReadCurrentFile(&qf->zipinfo, tmpbuf, read);
			offset -= read;
		} //end while

		//read the Quake file from the zip file
		length = unzReadCurrentFile(&qf->zipinfo, buffer, length);
		//close the Quake file in the zip file
		unzCloseCurrentFile(&qf->zipinfo);
		//close the zip file
		unzClose(zf);

		return length;
	} //end if
	else
	{
		fp = SafeOpenRead(qf->filename);

		if(qf->offset)
		{
			fseek(fp, qf->offset, SEEK_SET);
		}

		if(offset)
		{
			fseek(fp, offset, SEEK_CUR);
		}

		SafeRead(fp, buffer, length);
		fclose(fp);

		return length;
	} //end else
} //end of the function ReadQuakeFile
Example #10
0
void RTS_AddFile (char *filename)
   {
   wadinfo_t  header;
   lumpinfo_t *lump_p;
   uint32     i;
   int32      handle, length;
   int32      startlump;
   filelump_t *fileinfo;

//
// read the entire file in
//      FIXME: shared opens

   handle = SafeOpenRead( filename, filetype_binary );

   startlump = numlumps;

   // WAD file
   printf("    Adding %s.\n",filename);
   SafeRead( handle, &header, sizeof( header ) );
   if (strncmp(header.identification,"IWAD",4))
      Error ("RTS file %s doesn't have IWAD id\n",filename);
   header.numlumps = IntelLong(header.numlumps);
   header.infotableofs = IntelLong(header.infotableofs);
   length = header.numlumps*sizeof(filelump_t);
   fileinfo = alloca (length);
   if (!fileinfo)
      Error ("RTS file could not allocate header info on stack");
   lseek (handle, header.infotableofs, SEEK_SET);
   SafeRead (handle, fileinfo, length);
   numlumps += header.numlumps;

//
// Fill in lumpinfo
//
   SafeRealloc(&lumpinfo,numlumps*sizeof(lumpinfo_t));
   lump_p = &lumpinfo[startlump];

   for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
      {
      lump_p->handle = handle;
      lump_p->position = IntelLong(fileinfo->filepos);
      lump_p->size = IntelLong(fileinfo->size);
      strncpy (lump_p->name, fileinfo->name, 8);
      }
   }
Example #11
0
// =====================================================================================
//  LoadLump
// =====================================================================================
int             LoadLump(const lumpinfo_t* const source, byte* dest, int* texsize)
{
    //Log("** PnFNFUNC: LoadLump\n");

    *texsize = 0;
    if (source->filepos)
    {
        if (fseek(texfiles[source->iTexFile], source->filepos, SEEK_SET))
        {
            Warning("fseek to %d failed\n", source->filepos);
        }
        *texsize = source->disksize;

        bool wadinclude = false;
        std::map< int, bool >::iterator it;
        it = s_WadIncludeMap.find(source->iTexFile);
        if (it != s_WadIncludeMap.end())
        {
            wadinclude = it->second;
        }

        // Should we just load the texture header w/o the palette & bitmap?
        if ((g_wadtextures && !wadinclude) || !TestIncludeTextureNamed(source->name))
        {
            // Just read the miptex header and zero out the data offsets.
            // We will load the entire texture from the WAD at engine runtime
            int             i;
            miptex_t*       miptex = (miptex_t*)dest;
            SafeRead(texfiles[source->iTexFile], dest, sizeof(miptex_t));

            for (i = 0; i < MIPLEVELS; i++)
                miptex->offsets[i] = 0;
            return sizeof(miptex_t);
        }
        else
        {
			Developer(DEVELOPER_LEVEL_MESSAGE,"Including texture %s\n",source->name);
            // Load the entire texture here so the BSP contains the texture
            SafeRead(texfiles[source->iTexFile], dest, source->disksize);
            return source->disksize;
        }
    }

    Warning("::LoadLump() texture %s not found!", source->name);
    return 0;
}
Example #12
0
bool CheckTag( HSQUIRRELVM v, SQWRITEFUNC read, SQUserPointer up, SQInteger tag ) {
	SQInteger t;
	_CHECK_IO( SafeRead( v, read, up, &t, sizeof( t ) ) );
	if ( t != tag ) {
		v->Raise_Error( _SC( "invalid or corrupted closure stream" ) );
		return false;
	}
	return true;
}
Example #13
0
/*
====================
W_ReadLumpNum

Loads the lump into the given buffer, which must be >= W_LumpLength()
====================
*/
void W_ReadLumpNum (int lump, void *dest)
{
	lumpinfo_t	*l;
	
	if (lump >= numlumps)
		Error ("W_ReadLump: %i >= numlumps",lump);
	l = lumpinfo+lump;
	
	fseek (wadhandle, l->filepos, SEEK_SET);
	SafeRead (wadhandle, dest, l->size);
}
Example #14
0
/*
=================
TEX_InitFromWad
=================
*/
void	TEX_InitFromWad (char *path)
{
	int			i;
	
	texfile = SafeOpenRead (path);
	SafeRead (texfile, &wadinfo, sizeof(wadinfo));
	if (strncmp (wadinfo.identification, "WAD2", 4))
		Error ("TEX_InitFromWad: %s isn't a wadfile",path);
	wadinfo.numlumps = LittleLong(wadinfo.numlumps);
	wadinfo.infotableofs = LittleLong(wadinfo.infotableofs);
	fseek (texfile, wadinfo.infotableofs, SEEK_SET);
	lumpinfo = malloc(wadinfo.numlumps*sizeof(lumpinfo_t));
	SafeRead (texfile, lumpinfo, wadinfo.numlumps*sizeof(lumpinfo_t));
	
	for (i=0 ; i<wadinfo.numlumps ; i++)
	{
		CleanupName (lumpinfo[i].name, lumpinfo[i].name);
		lumpinfo[i].filepos = LittleLong(lumpinfo[i].filepos);
		lumpinfo[i].disksize = LittleLong(lumpinfo[i].disksize);
	}
}
Example #15
0
/*
====================
=
= RTS_ReadLump
=
= Loads the lump into the given buffer, which must be >= RTS_SoundLength()
=
====================
*/
void RTS_ReadLump (int32 lump, void *dest)
   {
   lumpinfo_t *l;

   if (lump >= numlumps)
      Error ("RTS_ReadLump: %i >= numlumps",lump);
   if (lump < 0)
      Error ("RTS_ReadLump: %i < 0",lump);
   l = lumpinfo+lump;
   lseek (l->handle, l->position, SEEK_SET);
   SafeRead(l->handle,dest,l->size);
   }
Example #16
0
//===========================================================================
//
// Parameter:           -
// Returns:             -
// Changes Globals:     -
//===========================================================================
int LoadQuakeFile(quakefile_t *qf, void **bufferptr)
{
	FILE *fp;
	void *buffer;
	int length;
	unzFile zf;

	if(qf->zipfile)
	{
		//open the zip file
		zf = unzOpen(qf->pakfile);
		//set the file pointer
		qf->zipinfo.file = ((unz_s *) zf)->file;
		//open the Quake file in the zip file
		unzOpenCurrentFile(&qf->zipinfo);
		//allocate memory for the buffer
		length = qf->length;
		buffer = GetMemory(length + 1);
		//read the Quake file from the zip file
		length = unzReadCurrentFile(&qf->zipinfo, buffer, length);
		//close the Quake file in the zip file
		unzCloseCurrentFile(&qf->zipinfo);
		//close the zip file
		unzClose(zf);

		*bufferptr = buffer;
		return length;
	} //end if
	else
	{
		fp = SafeOpenRead(qf->filename);

		if(qf->offset)
		{
			fseek(fp, qf->offset, SEEK_SET);
		}

		length = qf->length;

		if(!length)
		{
			length = Q_filelength(fp);
		}

		buffer = GetMemory(length + 1);
		((char *)buffer)[length] = 0;
		SafeRead(fp, buffer, length);
		fclose(fp);

		*bufferptr = buffer;
		return length;
	} //end else
} //end of the function LoadQuakeFile
static void StackWalk(FILE *core, struct NaClExceptionContext *context) {
  uintptr_t next;
  uintptr_t i;
  int first = 1;
  uintptr_t prog_ctr = context->prog_ctr;
  uintptr_t frame_ptr = context->frame_ptr;
  uintptr_t args_start;

  fprintf(core, "\"frames\": [\n");
  for (;;) {
    next = SafeRead(FrameLocNext(frame_ptr));
    if (next <= frame_ptr || next == 0) {
      break;
    }
    if (first) {
      first = 0;
    } else {
      fprintf(core, ",");
    }
    fprintf(core, "{\n");
    fprintf(core, "\"frame_ptr\": %"NACL_PRIuPTR",\n", frame_ptr);
    fprintf(core, "\"prog_ctr\": %"NACL_PRIuPTR",\n", prog_ctr);
    fprintf(core, "\"data\": [\n");
    args_start = FrameLocArgs(frame_ptr);
    for (i = args_start; i < next; i += 4) {
      if (i != args_start) {
        fprintf(core, ",");
      }
      fprintf(core, "%"NACL_PRIuPTR"\n", SafeRead(i));
    }
    fprintf(core, "]\n");
    fprintf(core, "}\n");

    prog_ctr = SafeRead(FrameLocPC(frame_ptr));
    frame_ptr = next;
  }

  fprintf(core, "]\n");
}
Example #18
0
void SB_LoadFile (swappedbuffer_t *sbuf, char *filename)
{
	FILE	*f;

	f = SafeOpenRead (filename);
	sbuf->maxsize = Q_filelength (f);
	sbuf->start = qmalloc (sbuf->maxsize + 1);
	sbuf->start[sbuf->maxsize] = 0;
	SafeRead (f, sbuf->start, sbuf->maxsize);
	fclose (f);

	sbuf->index = sbuf->start;
	sbuf->initialized = true;
}
Example #19
0
File: wad.cpp Project: Blzut3/zdbsp
FWadReader::FWadReader (const char *filename)
	: Lumps (NULL), File (NULL)
{
	File = fopen (filename, "rb");
	if (File == NULL)
	{
		throw std::runtime_error("Could not open input file");
	}

	SafeRead (&Header, sizeof(Header));
	if (Header.Magic[0] != 'P' && Header.Magic[0] != 'I' &&
		Header.Magic[1] != 'W' &&
		Header.Magic[2] != 'A' &&
		Header.Magic[3] != 'D')
	{
		fclose (File);
		File = NULL;
		throw std::runtime_error("Input file is not a wad");
	}

	Header.NumLumps = LittleLong(Header.NumLumps);
	Header.Directory = LittleLong(Header.Directory);

	if (fseek (File, Header.Directory, SEEK_SET))
	{
		throw std::runtime_error("Could not read wad directory");
	}

	Lumps = new WadLump[Header.NumLumps];
	SafeRead (Lumps, Header.NumLumps * sizeof(*Lumps));

	for (int i = 0; i < Header.NumLumps; ++i)
	{
		Lumps[i].FilePos = LittleLong(Lumps[i].FilePos);
		Lumps[i].Size = LittleLong(Lumps[i].Size);
	}
}
Example #20
0
/*
====================
W_OpenWad
====================
*/
void W_OpenWad (char *filename)
{
	lumpinfo_t		*lump_p;
	unsigned		i;
	int				length;
	
//
// open the file and add to directory
//	
	wadhandle = SafeOpenRead (filename);
	SafeRead (wadhandle, &header, sizeof(header));

	if (!STRING_MATCHES_ID(header.identification,WAD_ID))
		Error ("Wad file %s doesn't have %s identifier\n",filename, WAD_IDNAME);
		
	header.numlumps = LittleLong(header.numlumps);
	header.infotableofs = LittleLong(header.infotableofs);

	numlumps = header.numlumps;

	length = numlumps*sizeof(lumpinfo_t);
	lumpinfo = malloc (length);
	lump_p = lumpinfo;
	
	fseek (wadhandle, header.infotableofs, SEEK_SET);
	SafeRead (wadhandle, lumpinfo, length);

//
// Fill in lumpinfo
//
	
	for (i=0 ; i<numlumps ; i++,lump_p++)
	{
		lump_p->filepos = LittleLong(lump_p->filepos);
		lump_p->size = LittleLong(lump_p->size);
	}
}
Example #21
0
/*
 *takes a binary file, set the columns which you want to 
 *sort in ESortParams.col_start and ESortParams.col_end
 */
void ExSortKmerEdges(const char *bin_kedge, const char *run_file, 
	unsigned char KEY_SIZE, ExKeyCompare key_compare){
	unsigned char *run_buffer = NULL;
	size_t key_buf_len = (KEY_SIZE*KEYS_IN_RUN);
	size_t buf_len = key_buf_len + sizeof(unsigned long);
	size_t ret_len;
	int bin_kedge_fd = open(bin_kedge, O_RDONLY);
	int run_file_fd = fileno(fopen(run_file, "w")); 
	unsigned long *runlen;
	unsigned long rcount = 0;
	unsigned char *key_buf;
	Clock *clk = CreateClock();

	if(!(bin_kedge_fd > 0 && run_file_fd > 0)){
		perror("FAILED TO OPEN FILES:");
		assert(0);
	}
	run_buffer = malloc(sizeof(unsigned char)*buf_len);
	assert(run_buffer);
	runlen = (unsigned long *)run_buffer;
	key_buf = run_buffer + sizeof(unsigned long);

	StartClock(clk);
	while((ret_len = SafeRead(bin_kedge_fd, key_buf, key_buf_len)) >= KEY_SIZE){
		*runlen = (unsigned long) ret_len;
		IntegerSort_SB(key_buf, key_buf + (*runlen) - KEY_SIZE, KEY_SIZE, 1, 
			ESortParams.col_start, ESortParams.col_end, 0, CharMap, 
			ESortParams.endian);

		ret_len = SafeWrite(run_file_fd, run_buffer, 
			((*runlen + sizeof(unsigned long))< buf_len)?(*runlen + 
				sizeof(unsigned long)):buf_len);
		rcount++;
		assert(ret_len == ((*runlen + sizeof(unsigned long) 
			< buf_len)?(*runlen + sizeof(unsigned long)):buf_len));
	}
	StopClock(clk);
	FreeISortBuckets();
	/*create the final run*/
	close(run_file_fd); close(bin_kedge_fd);
	free(run_buffer);
	
	printf("\n[EX-SORT CREATED %lu RUNS] took %ld ticks\n", rcount, GetClockTicks(clk));
	StartClock(clk);
	/*call the external rway merge*/
	ExternalRWayMerge(run_file, RUNS_PER_MERGE, KEY_SIZE, key_compare, rcount);
	StopClock(clk);
	printf("[R-WAY MERGE] took %ld ticks\n", GetClockTicks(clk));
}
Example #22
0
/*
 * ==============
 * LoadFile
 * ==============
 */
int             LoadFile(const char* const filename, char** bufferptr)
{
    FILE*           f;
    int             length;
    char*           buffer;

    f = SafeOpenRead(filename);
    length = q_filelength(f);
    buffer = (char*)Alloc(length + 1);
    SafeRead(f, buffer, length);
    fclose(f);

    *bufferptr = buffer;
    return length;
}
Example #23
0
long LoadFile (char *filename, void **bufferptr)
{
	int	handle;
	long	length;
	void	*buffer;

	handle = SafeOpenRead (filename);
	length = filelength (handle);
	buffer = SafeMalloc (length);
	SafeRead (handle, buffer, length);
	close (handle);

	*bufferptr = buffer;
	return length;
}
int main2(int argc, char** argv)
{
    options* opt = initProgram(argc, argv);
    data d;
    printf("Row size = %lu\n", sizeof(data));
    int fd = OpenFile(opt->data_file, O_RDONLY);
    
    for(int i=0; i<30; i++)
    {
        printf("Bytes read = %d\n", SafeRead(fd, (char*)&d, sizeof(data)));
        printf("Data[%d]:   %s\n", i, dataToString(&d));
    }

    CHECK_ERROR( CloseFile(fd) );
    exit(0);
}
Example #25
0
/*
==============
LoadFile

==============
*/
int LoadFile (const char *filename, void **bufferptr)
{
	FILE	*f;
	size_t	length;
	void	*buffer;

	f = SafeOpenRead (filename);
	length = (size_t) Q_filelength (f);
	buffer = malloc (length + 1);
	if (!buffer)
		COM_Error ("%s failed for %lu bytes.", __thisfunc__, (unsigned long)length);
	((char *)buffer)[length] = 0;
	SafeRead(f, buffer, length);
	fclose (f);

	*bufferptr = buffer;
	return length;
}
Example #26
0
/*
==================
LoadLump
==================
*/
int LoadLump (char *name, byte *dest)
{
  int		i, j;
  char	cname[16];
	
  CleanupName (name, cname);
	
  for (j = 0; j < NUM_WADS; j++) {
    for (i=0 ; i < wadinfo[j].numlumps ; i++) {
      if (!strcmp(cname, lumpinfo[j][i].name)) {
	fseek (texfile[j], lumpinfo[j][i].filepos, SEEK_SET);
	SafeRead (texfile[j], dest, lumpinfo[j][i].disksize);
	return lumpinfo[j][i].disksize;
      }
    }
  }
  printf ("WARNING: texture %s not found\n", name);
  return 0;
}
Example #27
0
//===========================================================================
//
// Parameter:			-
// Returns:				-
// Changes Globals:		-
//===========================================================================
int LoadQuakeFile(quakefile_t *qf, void **bufferptr)
{
	FILE *fp;
	void *buffer;
	int length;
	unzFile zf;

	if (qf->zipfile)
	{
		//open the zip file
		zf = unzOpen(qf->pakfile);
		//set the file position in the zip file (also sets the current file info)
		unzSetOffset(zf, qf->pos);
		//open the Quake file in the zip file
		unzOpenCurrentFile(zf);
		//allocate memory for the buffer
		length = qf->length;
		buffer = GetMemory(length+1);
		//read the Quake file from the zip file
		length = unzReadCurrentFile(zf, buffer, length);
		//close the Quake file in the zip file
		unzCloseCurrentFile(zf);
		//close the zip file
		unzClose(zf);

		*bufferptr = buffer;
		return length;
	} //end if
	else
	{
		fp = SafeOpenRead(qf->filename);
		length = qf->length;
		if (!length) length = Q_filelength(fp);
		buffer = GetMemory(length+1);
		((char *)buffer)[length] = 0;
		SafeRead(fp, buffer, length);
		fclose(fp);

		*bufferptr = buffer;
		return length;
	} //end else
} //end of the function LoadQuakeFile
Example #28
0
/*
==============
LoadFile
==============
*/
int    LoadFile (char *filename, void **bufferptr)
{
	int    length = 0;
	void    *buffer;

	FileHandle_t f = SafeOpenRead (filename);
	if ( FILESYSTEM_INVALID_HANDLE != f )
	{
		length = Q_filelength (f);
		buffer = malloc (length+1);
		((char *)buffer)[length] = 0;
		SafeRead (f, buffer, length);
		g_pFileSystem->Close (f);
		*bufferptr = buffer;
	}
	else
	{
		*bufferptr = NULL;
	}
	return length;
}
Example #29
0
void        autowad_UpdateUsedWads()
{
    // see which wadfiles are needed
    // code for this wad loop somewhat copied from below
    wadinfo_t       thiswad;
    lumpinfo_t*     thislump = NULL;
    wadpath_t*      currentwad;
    char*           pszWadFile;
    FILE*           texfile;
    const char*     pszWadroot = getenv("WADROOT");
    int             i, j;
    int             nTexLumps = 0;

#ifdef _DEBUG
    Log("[dbg] Starting wad dependency check\n");
#endif

    // open each wadpath and sort though its contents
    for (i = 0; i < g_iNumWadPaths; i++)
    {
        currentwad = g_pWadPaths[i];
        pszWadFile = currentwad->path;
        currentwad->usedbymap = false;  // guilty until proven innocent

#ifdef _DEBUG
        Log(" Parsing wad: '%s'\n", pszWadFile);
#endif

        texfile = fopen(pszWadFile, "rb");

#ifdef SYSTEM_WIN32
        if (!texfile)
        {
            // cant find it, maybe this wad file has a hard code drive
            if (pszWadFile[1] == ':')
            {
                pszWadFile += 2;                           // skip past the drive
                texfile = fopen(pszWadFile, "rb");
            }
        }
#endif

        char            szTmp[_MAX_PATH];
        if (!texfile && pszWadroot)
        {
            char            szFile[_MAX_PATH];
            char            szSubdir[_MAX_PATH];

            ExtractFile(pszWadFile, szFile);

            ExtractFilePath(pszWadFile, szTmp);
            ExtractFile(szTmp, szSubdir);

            // szSubdir will have a trailing separator
            safe_snprintf(szTmp, _MAX_PATH, "%s" SYSTEM_SLASH_STR "%s%s", pszWadroot, szSubdir, szFile);
            texfile = fopen(szTmp, "rb");

#ifdef SYSTEM_POSIX
            if (!texfile)
            {
                // cant find it, Convert to lower case and try again
                strlwr(szTmp);
                texfile = fopen(szTmp, "rb");
            }
#endif
        }

#ifdef HLCSG_SEARCHWADPATH_VL
#ifdef SYSTEM_WIN32
        if (!texfile && pszWadFile[0] == '\\')
        {
            char tmp[_MAX_PATH];
            int l;
            for (l = 'C'; l <= 'Z'; ++l)
            {
                safe_snprintf (tmp, _MAX_PATH, "%c:%s", l, pszWadFile);
                texfile = fopen (tmp, "rb");
                if (texfile)
                {
                    break;
                }
            }
        }
#endif
#endif

        if (!texfile) // still cant find it, skip this one
        {
#ifdef HLCSG_SEARCHWADPATH_VL
            pszWadFile = currentwad->path; // correct it back
#endif
            if(pszWadroot)
            {
                Warning("Wad file '%s' not found, also tried '%s'",pszWadFile,szTmp);
            }
            else
            {
                Warning("Wad file '%s' not found",pszWadFile);
            }
            continue;
        }

        // look and see if we're supposed to include the textures from this WAD in the bsp.
        {
            WadInclude_i    it;
            bool            including = false;
            for (it = g_WadInclude.begin(); it != g_WadInclude.end(); it++)
            {
                if (stristr(pszWadFile, it->c_str()))
                {
#ifdef _DEBUG
                    Log("  - including wad\n");
#endif
                    including = true;
                    currentwad->usedbymap = true;
                    break;
                }
            }
            if (including)
            {
                //fclose(texfile);
                //continue;
            }
        }

        // read in this wadfiles information
        SafeRead(texfile, &thiswad, sizeof(thiswad));

        // make sure its a valid format
        if (strncmp(thiswad.identification, "WAD2", 4) && strncmp(thiswad.identification, "WAD3", 4))
        {
            fclose(texfile);
            continue;
        }

        thiswad.numlumps        = LittleLong(thiswad.numlumps);
        thiswad.infotableofs    = LittleLong(thiswad.infotableofs);

        // read in lump
        if (fseek(texfile, thiswad.infotableofs, SEEK_SET))
        {
            fclose(texfile);
            continue;   // had trouble reading, skip this wad
        }

        // memalloc for this lump
        thislump = (lumpinfo_t*)realloc(thislump, (nTexLumps + thiswad.numlumps) * sizeof(lumpinfo_t));
        // BUGBUG: is this destructed?

        // for each texlump
        for (j = 0; j < thiswad.numlumps; j++, nTexLumps++)
        {
            SafeRead(texfile, &thislump[nTexLumps], (sizeof(lumpinfo_t) - sizeof(int)) );  // iTexFile is NOT read from file

            CleanupName(thislump[nTexLumps].name, thislump[nTexLumps].name);

            if (autowad_IsUsedTexture(thislump[nTexLumps].name))
            {
                currentwad->usedbymap = true;
                currentwad->usedtextures++;
#ifdef _DEBUG
                Log("    - Used wadfile: [%s]\n", thislump[nTexLumps].name);
#endif
                autowad_PurgeName(thislump[nTexLumps].name);
            }
        }

        fclose(texfile);
    }

#ifdef _DEBUG
    Log("[dbg] End wad dependency check\n\n");
#endif
    return;
}
Example #30
0
			uint In::SafeRead8()
			{
				byte data;
				SafeRead( &data, 1 );
				return *static_cast<std::istream*>(stream) ? data : ~0U;
			}