AppleWin/source/linux/wwrapper.cpp
Andrea Odetti 1fccde2892 Add libapple and simple ncurses frontend.
Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
2017-10-14 19:18:44 +01:00

268 lines
6.3 KiB
C++

/*
* Wrappers for some common Microsoft file and memory functions used in AppleWin
* by beom beotiger, Nov 2007AD
*/
#include "linux/wwrapper.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <boost/lexical_cast.hpp>
#include "../resource/resource.h"
#include "Log.h"
DWORD SetFilePointer(HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod) {
/* ummm,fseek in Russian */
fseek((FILE*)hFile, lDistanceToMove, dwMoveMethod);
return ftell((FILE*)hFile);
}
BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped) {
/* read something from file */
DWORD bytesread = fread(lpBuffer, 1, nNumberOfBytesToRead, (FILE*)hFile);
*lpNumberOfBytesRead = bytesread;
return (nNumberOfBytesToRead == bytesread);
}
BOOL WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) {
/* write something to file */
DWORD byteswritten = fwrite(lpBuffer, 1, nNumberOfBytesToWrite, (FILE*)hFile);
*lpNumberOfBytesWritten = byteswritten;
return (nNumberOfBytesToWrite == byteswritten);
}
/* close handle whatever it has been .... hmmmmm. I just love Microsoft! */
BOOL CloseHandle(HANDLE hObject) {
return(!fclose((FILE*) hObject));
}
BOOL DeleteFile(LPCTSTR lpFileName) {
if(remove(lpFileName) == 0) return TRUE;
else return FALSE;
}
DWORD GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
/* what is the size of the specified file??? Hmmm, really I donna. ^_^ */
long lcurset = ftell((FILE*)hFile); // remember current file position
fseek((FILE*)hFile, 0, FILE_END); // go to the end of file
DWORD lfilesize = ftell((FILE*)hFile); // that is the real size of file, isn't it??
fseek((FILE*)hFile, lcurset, FILE_BEGIN); // let the file position be the same as before
return lfilesize;
}
LPVOID VirtualAlloc(LPVOID lpAddress, size_t dwSize,
DWORD flAllocationType, DWORD flProtect) {
/* just malloc and alles? 0_0 */
void* mymemory;
mymemory = realloc(lpAddress, dwSize);
if(flAllocationType & MEM_COMMIT) ZeroMemory(mymemory, dwSize); // original VirtualAlloc does this (if..)
return mymemory;
}
BOOL VirtualFree(LPVOID lpAddress, size_t dwSize,
DWORD dwFreeType) {
free(lpAddress);
return TRUE;
}
// make all chars in buffer lowercase
DWORD CharLowerBuff(LPTSTR lpsz, DWORD cchLength)
{
// char *s;
if (lpsz)
for (lpsz; *lpsz; lpsz++)
*lpsz = tolower(*lpsz);
return 1;
}
void _tzset()
{
tzset();
}
errno_t ctime_s(char * buf, size_t size, const time_t *time)
{
const char * t = asctime(localtime(time));
strncpy(buf, t, size);
return 0;
}
void strcpy_s(char * dest, size_t size, const char * source)
{
strncpy(dest, source, size);
}
HANDLE CreateFile(LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
if (dwCreationDisposition == CREATE_NEW)
{
if (dwDesiredAccess & GENERIC_READ)
return fopen(lpFileName, "w+");
else
return fopen(lpFileName, "w");
}
if (dwCreationDisposition == OPEN_EXISTING)
{
if (dwDesiredAccess & GENERIC_WRITE)
return fopen(lpFileName, "r+");
else
return fopen(lpFileName, "r");
}
return NULL;
}
DWORD GetFileAttributes(const char * filename)
{
const int exists = access(filename, F_OK);
if (exists)
return INVALID_FILE_ATTRIBUTES;
const int read = access(filename, R_OK);
if (read)
return INVALID_FILE_ATTRIBUTES;
const int write = access(filename, W_OK);
if (write)
return FILE_ATTRIBUTE_READONLY;
return FILE_ATTRIBUTE_NORMAL;
}
DWORD GetFullPathName(const char* filename, DWORD length, char * buffer, char ** filePart)
{
realpath(filename, buffer);
return strlen(buffer);
}
std::string MAKEINTRESOURCE(int x)
{
switch (x)
{
case IDR_APPLE2_ROM: return "Apple2.rom";
case IDR_APPLE2_PLUS_ROM: return "Apple2_Plus.rom";
case IDR_APPLE2E_ROM: return "Apple2e.rom";
case IDR_APPLE2E_ENHANCED_ROM: return "Apple2e_Enhanced.rom";
case IDR_DISK2_FW: return "DISK2.rom";
case IDR_SSC_FW: return "SSC.rom";
case IDR_HDDRVR_FW: return "Hddrvr.bin";
case IDR_PRINTDRVR_FW: return "Parallel.rom";
case IDR_MOCKINGBOARD_D_FW: return "Mockingboard-D.rom";
case IDR_MOUSEINTERFACE_FW: return "MouseInterface.rom";
case IDR_THUNDERCLOCKPLUS_FW: return "ThunderClockPlus.rom";
case IDR_TKCLOCK_FW: return"TKClock.rom";
}
LogFileOutput("Unknown resource %d\n", x);
return std::string();
}
HRSRC FindResource(void *, const std::string & filename, const char *)
{
HRSRC result;
if (!filename.empty())
{
const std::string path = "resource/" + filename;
int fd = open(path.c_str(), O_RDONLY);
if (fd != -1)
{
struct stat stdbuf;
if ((fstat(fd, &stdbuf) == 0) && S_ISREG(stdbuf.st_mode))
{
const off_t size = stdbuf.st_size;
result.data.resize(size);
ssize_t rd = read(fd, result.data.data(), size);
}
close(fd);
}
}
if (result.data.empty())
{
LogFileOutput("FindResource: could not load resource %s\n", filename.c_str());
printf("pippo\n");
}
return result;
}
DWORD SizeofResource(void *, const HRSRC & res)
{
return res.data.size();
}
HGLOBAL LoadResource(void *, const HRSRC & res)
{
return res.data.data();
}
BYTE * LockResource(HGLOBAL data)
{
return (BYTE *)data;
}
DWORD timeGetTime()
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_usec / 1000;
}
DWORD GetCurrentDirectory(DWORD length, char * buffer)
{
getcwd(buffer, length);
return 0;
}
UINT64 _strtoui64(const char * str, void *, int)
{
return boost::lexical_cast<UINT64>(str);
}
void GetLocalTime(SYSTEMTIME *t)
{
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
time_t tim = ts.tv_sec;
t->wMilliseconds = ts.tv_nsec / 1000000;
tm * local = localtime(&tim);
t->wSecond = local->tm_sec;
t->wMinute = local->tm_min;
t->wHour = local->tm_hour;
t->wDayOfWeek = local->tm_wday;
t->wDay = local->tm_mday;
t->wMonth = local->tm_mon + 1;
t->wYear = local->tm_year;
}
BOOL GetOpenFileName(LPOPENFILENAME lpofn)
{
return FALSE;
}