#include "setup.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <limits.h>
#if MAC_XCD
#include <sys/time.h>
#include <unistd.h>
#endif
#if WIN_MVC
#include <windows.h>
#include <direct.h>
#include <io.h>
#include <fcntl.h>
#include <signal.h>
#endif
#if UNIX_7 || WIN_GCC
#include <sys/types.h>
#include <sys/timeb.h>
#include <signal.h>
#endif
#if UNIX_V || LINUX || DARWIN
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#endif
#include "envrnmnt.h"
#include "memalloc.h"
#include "sysdep.h"
#define SYSTEM_DEPENDENT_DATA 58
struct systemDependentData
{
#if WIN_MVC
int BinaryFileHandle;
unsigned char getcBuffer[7];
int getcLength;
int getcPosition;
#endif
#if (! WIN_MVC)
FILE *BinaryFP;
#endif
int (*BeforeOpenFunction)(Environment *);
int (*AfterOpenFunction)(Environment *);
jmp_buf *jmpBuffer;
};
#define SystemDependentData(theEnv) ((struct systemDependentData *) GetEnvironmentData(theEnv,SYSTEM_DEPENDENT_DATA))
void InitializeSystemDependentData(
Environment *theEnv)
{
AllocateEnvironmentData(theEnv,SYSTEM_DEPENDENT_DATA,sizeof(struct systemDependentData),NULL);
}
double gentime()
{
#if MAC_XCD || UNIX_V || DARWIN || LINUX || UNIX_7
struct timeval now;
gettimeofday(&now, 0);
return (now.tv_usec / 1000000.0) + now.tv_sec;
#elif WIN_MVC
FILETIME ft;
unsigned long long tt;
GetSystemTimeAsFileTime(&ft);
tt = ft.dwHighDateTime;
tt <<=32;
tt |= ft.dwLowDateTime;
tt /=10;
tt -= 11644473600000000ULL;
return (double) tt / 1000000.0;
#else
return((double) time(NULL));
#endif
}
int gensystem(
Environment *theEnv,
const char *commandBuffer)
{
return system(commandBuffer);
}
int gengetchar(
Environment *theEnv)
{
return(getc(stdin));
}
int genungetchar(
Environment *theEnv,
int theChar)
{
return(ungetc(theChar,stdin));
}
void genprintfile(
Environment *theEnv,
FILE *fptr,
const char *str)
{
fprintf(fptr,"%s",str);
}
void InitializeNonportableFeatures(
Environment *theEnv)
{
#if MAC_XCD
#pragma unused(theEnv)
#endif
}
void genexit(
Environment *theEnv,
int num)
{
if (SystemDependentData(theEnv)->jmpBuffer != NULL)
{ longjmp(*SystemDependentData(theEnv)->jmpBuffer,1); }
exit(num);
}
void SetJmpBuffer(
Environment *theEnv,
jmp_buf *theJmpBuffer)
{
SystemDependentData(theEnv)->jmpBuffer = theJmpBuffer;
}
char *genstrcpy(
char *dest,
const char *src)
{
return strcpy(dest,src);
}
char *genstrncpy(
char *dest,
const char *src,
size_t n)
{
return strncpy(dest,src,n);
}
char *genstrcat(
char *dest,
const char *src)
{
return strcat(dest,src);
}
char *genstrncat(
char *dest,
const char *src,
size_t n)
{
return strncat(dest,src,n);
}
int gensprintf(
char *buffer,
const char *restrictStr,
...)
{
va_list args;
int rv;
va_start(args,restrictStr);
rv = vsprintf(buffer,restrictStr,args);
va_end(args);
return rv;
}
int genrand()
{
return(rand());
}
void genseed(
unsigned int seed)
{
srand(seed);
}
char *gengetcwd(
char *buffer,
int buflength)
{
#if MAC_XCD
return(getcwd(buffer,buflength));
#else
if (buffer != NULL)
{ buffer[0] = 0; }
return(buffer);
#endif
}
int genchdir(
Environment *theEnv,
const char *directory)
{
int rv = -1;
if (directory == NULL)
{
#if MAC_XCD || DARWIN || LINUX || WIN_MVC
return 1;
#else
return 0;
#endif
}
#if MAC_XCD || DARWIN || LINUX
rv = chdir(directory) + 1;
#endif
#if WIN_MVC
wchar_t *wdirectory;
int wlength;
wlength = MultiByteToWideChar(CP_UTF8,0,directory,-1,NULL,0);
wdirectory = (wchar_t *) genalloc(theEnv,wlength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,directory,-1,wdirectory,wlength);
rv = _wchdir(wdirectory) + 1;
genfree(theEnv,wdirectory,wlength * sizeof(wchar_t));
#endif
return rv;
}
bool genremove(
Environment *theEnv,
const char *fileName)
{
#if WIN_MVC
wchar_t *wfileName;
int wfnlength;
#endif
#if WIN_MVC
wfnlength = MultiByteToWideChar(CP_UTF8,0,fileName,-1,NULL,0);
wfileName = (wchar_t *) genalloc(theEnv,wfnlength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,fileName,-1,wfileName,wfnlength);
if (_wremove(wfileName))
{
genfree(theEnv,wfileName,wfnlength * sizeof(wchar_t));
return false;
}
genfree(theEnv,wfileName,wfnlength * sizeof(wchar_t));
#else
if (remove(fileName)) return false;
#endif
return true;
}
bool genrename(
Environment *theEnv,
const char *oldFileName,
const char *newFileName)
{
#if WIN_MVC
wchar_t *woldFileName, *wnewFileName;
int wofnlength, wnfnlength;
#endif
#if WIN_MVC
wofnlength = MultiByteToWideChar(CP_UTF8,0,oldFileName,-1,NULL,0);
wnfnlength = MultiByteToWideChar(CP_UTF8,0,newFileName,-1,NULL,0);
woldFileName = (wchar_t *) genalloc(theEnv,wofnlength * sizeof(wchar_t));
wnewFileName = (wchar_t *) genalloc(theEnv,wnfnlength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,oldFileName,-1,woldFileName,wofnlength);
MultiByteToWideChar(CP_UTF8,0,newFileName,-1,wnewFileName,wnfnlength);
if (_wrename(woldFileName,wnewFileName))
{
genfree(theEnv,woldFileName,wofnlength * sizeof(wchar_t));
genfree(theEnv,wnewFileName,wnfnlength * sizeof(wchar_t));
return false;
}
genfree(theEnv,woldFileName,wofnlength * sizeof(wchar_t));
genfree(theEnv,wnewFileName,wnfnlength * sizeof(wchar_t));
#else
if (rename(oldFileName,newFileName)) return false;
#endif
return true;
}
int (*SetBeforeOpenFunction(Environment *theEnv,
int (*theFunction)(Environment *)))(Environment *)
{
int (*tempFunction)(Environment *);
tempFunction = SystemDependentData(theEnv)->BeforeOpenFunction;
SystemDependentData(theEnv)->BeforeOpenFunction = theFunction;
return(tempFunction);
}
int (*SetAfterOpenFunction(Environment *theEnv,
int (*theFunction)(Environment *)))(Environment *)
{
int (*tempFunction)(Environment *);
tempFunction = SystemDependentData(theEnv)->AfterOpenFunction;
SystemDependentData(theEnv)->AfterOpenFunction = theFunction;
return(tempFunction);
}
FILE *GenOpen(
Environment *theEnv,
const char *fileName,
const char *accessType)
{
FILE *theFile;
#if WIN_MVC
wchar_t *wfileName;
wchar_t *waccessType;
int wfnlength, watlength;
#endif
if (SystemDependentData(theEnv)->BeforeOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->BeforeOpenFunction)(theEnv); }
#if WIN_MVC
wfnlength = MultiByteToWideChar(CP_UTF8,0,fileName,-1,NULL,0);
watlength = MultiByteToWideChar(CP_UTF8,0,accessType,-1,NULL,0);
wfileName = (wchar_t *) genalloc(theEnv,wfnlength * sizeof(wchar_t));
waccessType = (wchar_t *) genalloc(theEnv,watlength * sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,fileName,-1,wfileName,wfnlength);
MultiByteToWideChar(CP_UTF8,0,accessType,-1,waccessType,watlength);
_wfopen_s(&theFile,wfileName,waccessType);
genfree(theEnv,wfileName,wfnlength * sizeof(wchar_t));
genfree(theEnv,waccessType,watlength * sizeof(wchar_t));
#else
theFile = fopen(fileName,accessType);
#endif
if ((theFile != NULL) & (strcmp(accessType,"r") == 0))
{
int theChar;
theChar = getc(theFile);
if (theChar == 0xEF)
{
theChar = getc(theFile);
if (theChar == 0xBB)
{
theChar = getc(theFile);
if (theChar != 0xBF)
{ ungetc(theChar,theFile);}
}
else
{ ungetc(theChar,theFile);}
}
else
{ ungetc(theChar,theFile); }
}
if (SystemDependentData(theEnv)->AfterOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->AfterOpenFunction)(theEnv); }
return theFile;
}
int GenClose(
Environment *theEnv,
FILE *theFile)
{
int rv;
rv = fclose(theFile);
return rv;
}
int GenFlush(
Environment *theEnv,
FILE *theFile)
{
int rv;
rv = fflush(theFile);
return rv;
}
void GenRewind(
Environment *theEnv,
FILE *theFile)
{
rewind(theFile);
}
long long GenTell(
Environment *theEnv,
FILE *theFile)
{
long long rv;
rv = ftell(theFile);
if (rv == -1)
{
if (errno > 0)
{ return LLONG_MIN; }
}
return rv;
}
int GenSeek(
Environment *theEnv,
FILE *theFile,
long offset,
int whereFrom)
{
return fseek(theFile,offset,whereFrom);
}
bool GenOpenReadBinary(
Environment *theEnv,
const char *funcName,
const char *fileName)
{
if (SystemDependentData(theEnv)->BeforeOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->BeforeOpenFunction)(theEnv); }
#if WIN_MVC
SystemDependentData(theEnv)->BinaryFileHandle = _open(fileName,O_RDONLY | O_BINARY);
if (SystemDependentData(theEnv)->BinaryFileHandle == -1)
{
if (SystemDependentData(theEnv)->AfterOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->AfterOpenFunction)(theEnv); }
return false;
}
#endif
#if (! WIN_MVC)
if ((SystemDependentData(theEnv)->BinaryFP = fopen(fileName,"rb")) == NULL)
{
if (SystemDependentData(theEnv)->AfterOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->AfterOpenFunction)(theEnv); }
return false;
}
#endif
if (SystemDependentData(theEnv)->AfterOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->AfterOpenFunction)(theEnv); }
return true;
}
void GenReadBinary(
Environment *theEnv,
void *dataPtr,
size_t size)
{
#if WIN_MVC
char *tempPtr;
tempPtr = (char *) dataPtr;
while (size > INT_MAX)
{
_read(SystemDependentData(theEnv)->BinaryFileHandle,tempPtr,INT_MAX);
size -= INT_MAX;
tempPtr = tempPtr + INT_MAX;
}
if (size > 0)
{ _read(SystemDependentData(theEnv)->BinaryFileHandle,tempPtr,(unsigned int) size); }
#endif
#if (! WIN_MVC)
fread(dataPtr,size,1,SystemDependentData(theEnv)->BinaryFP);
#endif
}
void GetSeekCurBinary(
Environment *theEnv,
long offset)
{
#if WIN_MVC
_lseek(SystemDependentData(theEnv)->BinaryFileHandle,offset,SEEK_CUR);
#endif
#if (! WIN_MVC)
fseek(SystemDependentData(theEnv)->BinaryFP,offset,SEEK_CUR);
#endif
}
void GetSeekSetBinary(
Environment *theEnv,
long offset)
{
#if WIN_MVC
_lseek(SystemDependentData(theEnv)->BinaryFileHandle,offset,SEEK_SET);
#endif
#if (! WIN_MVC)
fseek(SystemDependentData(theEnv)->BinaryFP,offset,SEEK_SET);
#endif
}
void GenTellBinary(
Environment *theEnv,
long *offset)
{
#if WIN_MVC
*offset = _lseek(SystemDependentData(theEnv)->BinaryFileHandle,0,SEEK_CUR);
#endif
#if (! WIN_MVC)
*offset = ftell(SystemDependentData(theEnv)->BinaryFP);
#endif
}
void GenCloseBinary(
Environment *theEnv)
{
if (SystemDependentData(theEnv)->BeforeOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->BeforeOpenFunction)(theEnv); }
#if WIN_MVC
_close(SystemDependentData(theEnv)->BinaryFileHandle);
#endif
#if (! WIN_MVC)
fclose(SystemDependentData(theEnv)->BinaryFP);
#endif
if (SystemDependentData(theEnv)->AfterOpenFunction != NULL)
{ (*SystemDependentData(theEnv)->AfterOpenFunction)(theEnv); }
}
void GenWrite(
void *dataPtr,
size_t size,
FILE *fp)
{
if (size == 0) return;
#if UNIX_7
fwrite(dataPtr,size,1,fp);
#else
fwrite(dataPtr,size,1,fp);
#endif
}