From e9c7b3e739719a2f84c2d583b6a6faf3661dbad0 Mon Sep 17 00:00:00 2001 From: havoc Date: Tue, 20 Aug 2002 03:13:02 +0000 Subject: [PATCH] cleaned up and merged a lot of sys_*.c code into sys_shared.c git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2261 d7cf8633-e32d-0410-b094-e92efae38249 --- sys.h | 14 +++--- sys_linux.c | 104 ++-------------------------------------- sys_shared.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++- sys_win.c | 133 +-------------------------------------------------- 4 files changed, 141 insertions(+), 241 deletions(-) diff --git a/sys.h b/sys.h index e8de6bb1..0c91f4d0 100644 --- a/sys.h +++ b/sys.h @@ -29,25 +29,23 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // returns the file size // return -1 if file is not present // the file should be in BINARY mode for stupid OSs that care -int Sys_FileOpenRead (char *path, int *hndl); +int Sys_FileOpenRead (const char *path, int *hndl); -int Sys_FileOpenWrite (char *path); +int Sys_FileOpenWrite (const char *path); void Sys_FileClose (int handle); void Sys_FileSeek (int handle, int position); int Sys_FileRead (int handle, void *dest, int count); int Sys_FileWrite (int handle, void *data, int count); -int Sys_FileTime (char *path); -void Sys_mkdir (char *path); +int Sys_FileTime (const char *path); +void Sys_mkdir (const char *path); // // system IO // -void Sys_DebugLog(char *file, char *fmt, ...); - -void Sys_Error (char *error, ...); +void Sys_Error (const char *error, ...); // an error will cause the entire program to exit -void Sys_Printf (char *fmt, ...); +void Sys_Printf (const char *fmt, ...); // send text to the console void Sys_Quit (void); diff --git a/sys_linux.c b/sys_linux.c index eb2993cb..0dc1ce9e 100644 --- a/sys_linux.c +++ b/sys_linux.c @@ -30,10 +30,6 @@ char *cachedir = "/tmp"; // General routines // ======================================================================= -void Sys_DebugNumber(int y, int val) -{ -} - void Sys_Quit (void) { Host_Shutdown(); @@ -42,7 +38,7 @@ void Sys_Quit (void) exit(0); } -void Sys_Error (char *error, ...) +void Sys_Error (const char *error, ...) { va_list argptr; char string[1024]; @@ -60,7 +56,7 @@ void Sys_Error (char *error, ...) } -void Sys_Warn (char *warning, ...) +void Sys_Warn (const char *warning, ...) { va_list argptr; char string[1024]; @@ -71,96 +67,6 @@ void Sys_Warn (char *warning, ...) fprintf(stderr, "Warning: %s", string); } -/* -============ -Sys_FileTime - -returns -1 if not present -============ -*/ -int Sys_FileTime (char *path) -{ - struct stat buf; - - if (stat (path,&buf) == -1) - return -1; - - return buf.st_mtime; -} - - -void Sys_mkdir (char *path) -{ - mkdir (path, 0777); -} - -int Sys_FileOpenRead (char *path, int *handle) -{ - int h; - struct stat fileinfo; - - h = open (path, O_RDONLY, 0666); - *handle = h; - if (h == -1) - return -1; - - if (fstat (h,&fileinfo) == -1) - Sys_Error ("Error fstating %s", path); - - return fileinfo.st_size; -} - -int Sys_FileOpenWrite (char *path) -{ - int handle; - - umask (0); - - handle = open(path,O_RDWR | O_CREAT | O_TRUNC, 0666); - - if (handle == -1) - { - Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno)); - return 0; - } - - return handle; -} - -int Sys_FileWrite (int handle, void *src, int count) -{ - return write (handle, src, count); -} - -void Sys_FileClose (int handle) -{ - close (handle); -} - -void Sys_FileSeek (int handle, int position) -{ - lseek (handle, position, SEEK_SET); -} - -int Sys_FileRead (int handle, void *dest, int count) -{ - return read (handle, dest, count); -} - -void Sys_DebugLog(char *file, char *fmt, ...) -{ - va_list argptr; - static char data[1024]; - int fd; - - va_start(argptr, fmt); - vsprintf(data, fmt, argptr); - va_end(argptr); - fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666); - write(fd, data, strlen(data)); - close(fd); -} - double Sys_DoubleTime (void) { static int first = true; @@ -246,10 +152,8 @@ int main (int c, char **v) memset(&host_parms, 0, sizeof(host_parms)); - COM_InitArgv(c, v); - host_parms.argc = com_argc; - host_parms.argv = com_argv; - + host_parms.argc = c; + host_parms.argv = v; host_parms.basedir = basedir; fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); diff --git a/sys_shared.c b/sys_shared.c index 370752bf..11f16040 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -2,9 +2,11 @@ #include "quakedef.h" #include #ifndef WIN32 +#include #include #include #endif +#include extern cvar_t timestamps; extern cvar_t timeformat; @@ -53,7 +55,7 @@ extern HANDLE hinput, houtput; #endif #define MAX_PRINT_MSG 16384 -void Sys_Printf (char *fmt, ...) +void Sys_Printf (const char *fmt, ...) { va_list argptr; char start[MAX_PRINT_MSG]; // String we started with @@ -102,6 +104,133 @@ void Sys_Printf (char *fmt, ...) #endif } +// LordHavoc: 256 pak files (was 10) +#define MAX_HANDLES 256 +QFile *sys_handles[MAX_HANDLES]; + +int findhandle (void) +{ + int i; + + for (i = 1;i < MAX_HANDLES;i++) + if (!sys_handles[i]) + return i; + Sys_Error ("out of handles"); + return -1; +} + +/* +================ +Sys_FileLength +================ +*/ +int Sys_FileLength (QFile *f) +{ + int pos, end; + + pos = Qtell (f); + Qseek (f, 0, SEEK_END); + end = Qtell (f); + Qseek (f, pos, SEEK_SET); + + return end; +} + +int Sys_FileOpenRead (const char *path, int *handle) +{ + QFile *f; + int i, retval; + + i = findhandle (); + + f = Qopen(path, "rbz"); + + if (!f) + { + *handle = -1; + retval = -1; + } + else + { + sys_handles[i] = f; + *handle = i; + retval = Sys_FileLength(f); + } + + return retval; +} + +int Sys_FileOpenWrite (const char *path) +{ + QFile *f; + int i; + + i = findhandle (); + + f = Qopen(path, "wb"); + if (!f) + { + Con_Printf("Sys_FileOpenWrite: Error opening %s: %s", path, strerror(errno)); + return 0; + } + sys_handles[i] = f; + + return i; +} + +void Sys_FileClose (int handle) +{ + Qclose (sys_handles[handle]); + sys_handles[handle] = NULL; +} + +void Sys_FileSeek (int handle, int position) +{ + Qseek (sys_handles[handle], position, SEEK_SET); +} + +int Sys_FileRead (int handle, void *dest, int count) +{ + return Qread (sys_handles[handle], dest, count); +} + +int Sys_FileWrite (int handle, void *data, int count) +{ + return Qwrite (sys_handles[handle], data, count); +} + +int Sys_FileTime (const char *path) +{ +#if WIN32 + QFile *f; + + f = Qopen(path, "rb"); + if (f) + { + Qclose(f); + return 1; + } + + return -1; +#else + struct stat buf; + + if (stat (path,&buf) == -1) + return -1; + + return buf.st_mtime; +#endif +} + +void Sys_mkdir (const char *path) +{ +#if WIN32 + _mkdir (path); +#else + mkdir (path, 0777); +#endif +} + char engineversion[128]; void Sys_Shared_EarlyInit(void) diff --git a/sys_win.c b/sys_win.c index 15156556..5bd55f90 100644 --- a/sys_win.c +++ b/sys_win.c @@ -45,130 +45,6 @@ static HANDLE hFile; static HANDLE heventParent; static HANDLE heventChild; -/* -=============================================================================== - -QFile IO - -=============================================================================== -*/ - -// LordHavoc: 256 pak files (was 10) -#define MAX_HANDLES 256 -QFile *sys_handles[MAX_HANDLES]; - -int findhandle (void) -{ - int i; - - for (i=1 ; i