mirror of
https://git.code.sf.net/p/quake/quakeforge-old
synced 2024-11-11 07:01:46 +00:00
93 lines
1.7 KiB
C
93 lines
1.7 KiB
C
#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);
|
|
}
|