mirror of
https://git.code.sf.net/p/quake/quakeforge-old
synced 2025-02-16 16:51:16 +00:00
forgot to add these in the last checkin. They implement the QFile stuff.
This commit is contained in:
parent
edbb589045
commit
83fc2c0373
2 changed files with 119 additions and 0 deletions
93
common/quakeio.c
Normal file
93
common/quakeio.c
Normal file
|
@ -0,0 +1,93 @@
|
|||
#include <malloc.h>
|
||||
#include <stdarg.h>
|
||||
#include "quakeio.h"
|
||||
|
||||
QFile *Qopen(const char *path, const char *mode)
|
||||
{
|
||||
QFile *file;
|
||||
file=calloc(sizeof(*file),1);
|
||||
file->file=fopen(path,mode);
|
||||
return file;
|
||||
}
|
||||
|
||||
void Qclose(QFile *file)
|
||||
{
|
||||
if (file->file)
|
||||
fclose(file->file);
|
||||
else
|
||||
gzclose(file->gzfile);
|
||||
free(file);
|
||||
}
|
||||
|
||||
int Qread(QFile *file, void *buf, int count)
|
||||
{
|
||||
if (file->file)
|
||||
return fread(buf, 1, count, file->file);
|
||||
else
|
||||
return gzread(file->gzfile,buf,count);
|
||||
}
|
||||
|
||||
int Qwrite(QFile *file, void *buf, int count)
|
||||
{
|
||||
if (file->file)
|
||||
return fwrite(buf, 1, count, file->file);
|
||||
else
|
||||
return gzwrite(file->gzfile,buf,count);
|
||||
}
|
||||
|
||||
int Qprintf(QFile *file, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args,fmt);
|
||||
if (file->file)
|
||||
ret=vfprintf(file->file, fmt, args);
|
||||
else {
|
||||
char buf[4096];
|
||||
va_start(args, fmt);
|
||||
#ifdef HAVE_VSNPRINTF
|
||||
(void)vsnprintf(buf, sizeof(buf), fmt, args);
|
||||
#else
|
||||
(void)vsprintf(buf, fmt, args);
|
||||
#endif
|
||||
va_end(va);
|
||||
ret = strlen(buf); /* some *sprintf don't return the nb of bytes written */
|
||||
if (ret>0)
|
||||
ret=gzwrite(file, buf, (unsigned)ret);
|
||||
}
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *Qgets(QFile *file, char *buf, int count)
|
||||
{
|
||||
if (file->file)
|
||||
return fgets(buf, count, file->file);
|
||||
else
|
||||
return gzgets(file->gzfile,buf,count);
|
||||
}
|
||||
|
||||
int Qseek(QFile *file, long offset, int whence)
|
||||
{
|
||||
if (file->file)
|
||||
return fseek(file->file, offset, whence);
|
||||
else
|
||||
return gzseek(file->gzfile,offset,whence);
|
||||
}
|
||||
|
||||
long Qtell(QFile *file)
|
||||
{
|
||||
if (file->file)
|
||||
return ftell(file->file);
|
||||
else
|
||||
return gztell(file->gzfile);
|
||||
}
|
||||
|
||||
int Qflush(QFile *file)
|
||||
{
|
||||
if (file->file)
|
||||
return fflush(file->file);
|
||||
else
|
||||
return gzflush(file->gzfile,Z_SYNC_FLUSH);
|
||||
}
|
26
common/quakeio.h
Normal file
26
common/quakeio.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef __quake_io_h
|
||||
#define __quake_io_h
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "config.h"
|
||||
#ifdef HAS_ZLIB
|
||||
#include <zlib.h>
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
FILE *file;
|
||||
gzFile *gzfile;
|
||||
} QFile;
|
||||
|
||||
QFile *Qopen(const char *path, const char *mode);
|
||||
void Qclose(QFile *file);
|
||||
int Qread(QFile *file, void *buf, int count);
|
||||
int Qwrite(QFile *file, void *buf, int count);
|
||||
int Qprintf(QFile *file, const char *fmt, ...);
|
||||
char *Qgets(QFile *file, char *buf, int count);
|
||||
int Qseek(QFile *file, long offset, int whence);
|
||||
long Qtell(QFile *file);
|
||||
int Qflush(QFile *file);
|
||||
|
||||
#endif /*__quake_io_h*/
|
Loading…
Reference in a new issue