emu48-mirror/sources/Cardcopy/CARDCOPY.C
2024-03-19 22:36:03 +01:00

139 lines
3.7 KiB
C

/*
* cardcopy, (c) 2000 Christoph Giesselink (cgiess@swol.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#define VERSION "1.0"
#define FT_ERR 0 // illegal format
#define FT_NEW 1 // empty file
#define FT_SXGX 2 // Emu48 HP48SX/GX state file
#define FT_PORT 3 // 128KB port file
#define PORT1SIZE (128*1024*2) // file size of 128KB file
#define HP48SIG "Emu48 Document\xFE" // HP48 state file signature
UINT CheckType(char *lpszFileName)
{
BYTE pbyFileSignature[16];
HANDLE hFile;
DWORD FileSizeHigh,FileSizeLow;
UINT nType = FT_ERR;
hFile = CreateFile(lpszFileName,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (hFile == INVALID_HANDLE_VALUE)
return FT_NEW;
// check filesize
FileSizeLow = GetFileSize(hFile,&FileSizeHigh);
if (FileSizeHigh == 0 && FileSizeLow == PORT1SIZE)
nType = FT_PORT;
// Read and Compare signature
ReadFile(hFile,pbyFileSignature,sizeof(pbyFileSignature),&FileSizeLow,NULL);
if (FileSizeLow == sizeof(pbyFileSignature) && strcmp(pbyFileSignature,HP48SIG) == 0)
nType = FT_SXGX;
CloseHandle(hFile);
return nType;
}
BOOL CopyData(HANDLE hFileSource,HANDLE hFileDest)
{
BYTE byBuffer[16];
INT i;
DWORD lBytes;
assert(PORT1SIZE % sizeof(byBuffer) == 0);
for (i = PORT1SIZE / sizeof(byBuffer); i > 0; --i)
{
ReadFile(hFileSource,byBuffer,sizeof(byBuffer),&lBytes,NULL);
if (lBytes != sizeof(byBuffer)) return TRUE;
WriteFile(hFileDest,byBuffer,sizeof(byBuffer),&lBytes,NULL);
if (lBytes != sizeof(byBuffer)) return TRUE;
}
return FALSE;
}
UINT main(int argc, char *argv[])
{
HANDLE hFileSource,hFileDest;
UINT nSourceType,nDestType;
printf("HP48 Port1 Import/Export Tool for Emu48 V" VERSION "\n");
if (argc != 3)
{
printf("\nUsage:\n\t%s <SourceFile> <DestinationFile>\n\n", argv[0]);
return 1;
}
// check source file type
nSourceType = CheckType(argv[1]);
if (nSourceType == FT_ERR)
{
printf("Error: Illegal source file type\n");
return 2;
}
// check destination file type
nDestType = CheckType(argv[2]);
if (nDestType == FT_ERR)
{
printf("Error: Illegal destination file type\n");
return 3;
}
// open source file
hFileSource = CreateFile(argv[1],GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if (hFileSource != INVALID_HANDLE_VALUE)
{
hFileDest = CreateFile(argv[2],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,0,NULL);
if (hFileDest != INVALID_HANDLE_VALUE)
{
if (nSourceType == FT_SXGX) SetFilePointer(hFileSource,-PORT1SIZE,NULL,FILE_END);
if (nDestType == FT_SXGX) SetFilePointer(hFileDest ,-PORT1SIZE,NULL,FILE_END);
CopyData(hFileSource,hFileDest);
puts("Copy successful.");
CloseHandle(hFileDest);
}
else
{
printf("Error: Can't open destination file %s\n",argv[2]);
return 4;
}
CloseHandle(hFileSource);
}
else
{
printf("Error: Can't open source file %s\n",argv[1]);
return 5;
}
return 0;
}