📄 stormportmac.cpp.svn-base
字号:
* MoveFile
* lpFromFileName: old file path
* lpToFileName: new file path
********************************************************************/
BOOL MoveFile(const char * lpFromFileName, const char * lpToFileName)
{
OSErr theErr;
FSRef fromFileRef;
FSRef toFileRef;
FSRef parentFolderRef;
// Get the path to the old file
theErr = FSLocationFromFullPath(lpFromFileName, &fromFileRef);
if (theErr != noErr)
{
SetLastError(theErr);
return false;
}
// Get the path to the new folder for the file
char folderName[strlen(lpToFileName)];
CFStringRef folderPathCFString = CFStringCreateWithCString(NULL, lpToFileName, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, folderPathCFString, kCFURLPOSIXPathStyle, FALSE);
CFURLRef folderURL = CFURLCreateCopyDeletingLastPathComponent(NULL, fileURL);
CFURLGetFileSystemRepresentation(folderURL, TRUE, (UInt8 *)folderName, strlen(lpToFileName));
theErr = FSLocationFromFullPath(folderName, &parentFolderRef);
CFRelease(fileURL);
CFRelease(folderURL);
CFRelease(folderPathCFString);
// Move the old file
theErr = FSMoveObject(&fromFileRef, &parentFolderRef, &toFileRef);
if (theErr != noErr)
{
SetLastError(theErr);
return false;
}
// Get a CFString for the new file name
CFStringRef newFileNameCFString = CFStringCreateWithCString(NULL, lpToFileName, kCFStringEncodingUTF8);
fileURL = CFURLCreateWithFileSystemPath(NULL, newFileNameCFString, kCFURLPOSIXPathStyle, FALSE);
CFRelease(newFileNameCFString);
newFileNameCFString = CFURLCopyLastPathComponent(fileURL);
CFRelease(fileURL);
// Convert CFString to Unicode and rename the file
UniChar unicodeFileName[256];
CFStringGetCharacters(newFileNameCFString, CFRangeMake(0, CFStringGetLength(newFileNameCFString)),
unicodeFileName);
theErr = FSRenameUnicode(&toFileRef, CFStringGetLength(newFileNameCFString), unicodeFileName,
kTextEncodingUnknown, NULL);
if (theErr != noErr)
{
SetLastError(theErr);
CFRelease(newFileNameCFString);
return false;
}
CFRelease(newFileNameCFString);
SetLastError(theErr);
return true;
}
/********************************************************************
* CreateFile
* ulMode: GENERIC_READ | GENERIC_WRITE
* ulSharing: FILE_SHARE_READ
* pSecAttrib: NULL
* ulCreation: OPEN_EXISTING, OPEN_ALWAYS, CREATE_NEW
* ulFlags: 0
* hFile: NULL
********************************************************************/
HANDLE CreateFile( const char *sFileName, /* file name */
DWORD ulMode, /* access mode */
DWORD ulSharing, /* share mode */
void *pSecAttrib, /* SD */
DWORD ulCreation, /* how to create */
DWORD ulFlags, /* file attributes */
HANDLE hFile ) /* handle to template file */
{
#pragma unused (ulSharing, pSecAttrib, ulFlags, hFile)
OSErr theErr;
FSRef theFileRef;
FSRef theParentRef;
short fileRef;
char permission;
static OSType gCreator;
static OSType gType;
theErr = FSLocationFromFullPath(sFileName, &theFileRef);
if (theErr == fnfErr)
{ // Create the FSRef for the parent directory.
memset(&theFileRef, 0, sizeof(FSRef));
UInt8 folderName[MAX_PATH];
CFStringRef folderPathCFString = CFStringCreateWithCString(NULL, sFileName, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, folderPathCFString, kCFURLPOSIXPathStyle, FALSE);
CFURLRef folderURL = CFURLCreateCopyDeletingLastPathComponent(NULL, fileURL);
CFURLGetFileSystemRepresentation(folderURL, TRUE, folderName, MAX_PATH);
theErr = FSLocationFromFullPath(folderName, &theParentRef);
CFRelease(fileURL);
CFRelease(folderURL);
CFRelease(folderPathCFString);
}
if (theErr != noErr)
{
SetLastError(theErr);
if (ulCreation == OPEN_EXISTING || theErr != fnfErr)
return INVALID_HANDLE_VALUE;
}
if (ulCreation != OPEN_EXISTING)
{ /* We create the file */
UniChar unicodeFileName[256];
CFStringRef filePathCFString = CFStringCreateWithCString(NULL, sFileName, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, filePathCFString, kCFURLPOSIXPathStyle, FALSE);
CFStringRef fileNameCFString = CFURLCopyLastPathComponent(fileURL);
CFStringGetCharacters(fileNameCFString, CFRangeMake(0, CFStringGetLength(fileNameCFString)),
unicodeFileName);
theErr = FSCreateCompat(&theParentRef, gCreator, gType, unicodeFileName,
CFStringGetLength(fileNameCFString), &theFileRef);
CFRelease(fileNameCFString);
CFRelease(filePathCFString);
CFRelease(fileURL);
if (theErr != noErr)
{
SetLastError(theErr);
return INVALID_HANDLE_VALUE;
}
}
if (ulMode == GENERIC_READ)
permission = fsRdPerm;
else
{
if (ulMode == GENERIC_WRITE)
permission = fsWrPerm;
else
permission = fsRdWrPerm;
}
theErr = FSOpenDFCompat(&theFileRef, permission, &fileRef);
SetLastError(theErr);
if (theErr == noErr)
return (HANDLE)(int)fileRef;
else
return INVALID_HANDLE_VALUE;
}
/********************************************************************
* CloseHandle
********************************************************************/
BOOL CloseHandle( HANDLE hFile ) /* handle to object */
{
OSErr theErr;
if ((hFile == NULL) || (hFile == INVALID_HANDLE_VALUE))
return FALSE;
theErr = FSCloseFork((short)(int)hFile);
SetLastError(theErr);
return theErr != noErr;
}
/********************************************************************
* GetFileSize
********************************************************************/
DWORD GetFileSize( HANDLE hFile, /* handle to file */
DWORD *ulOffSetHigh ) /* high-order word of file size */
{
SInt64 fileLength;
OSErr theErr;
if ((hFile == NULL) || (hFile == INVALID_HANDLE_VALUE))
{
SetLastError(theErr);
return -1u;
}
theErr = FSGetForkSize((short)(int)hFile, &fileLength);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
if (ulOffSetHigh != NULL)
*ulOffSetHigh = fileLength >> 32;
SetLastError(theErr);
return fileLength;
}
/********************************************************************
* SetFilePointer
* pOffSetHigh: NULL
* ulMethod: FILE_BEGIN, FILE_CURRENT
********************************************************************/
DWORD SetFilePointer( HANDLE hFile, /* handle to file */
LONG lOffSetLow, /* bytes to move pointer */
LONG *pOffSetHigh, /* bytes to move pointer */
DWORD ulMethod ) /* starting point */
{
OSErr theErr;
if (ulMethod == FILE_CURRENT)
{
SInt64 bytesToMove;
if (pOffSetHigh != NULL)
bytesToMove = ((SInt64)*pOffSetHigh << 32) + lOffSetLow;
else
bytesToMove = lOffSetLow;
SInt64 newPos;
theErr = FSSetForkPosition((short)(int)hFile, fsFromMark, bytesToMove);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
theErr = FSGetForkPosition((short)(int)hFile, &newPos);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
if (pOffSetHigh != NULL)
*pOffSetHigh = newPos >> 32;
SetLastError(theErr);
return newPos;
}
else if (ulMethod == FILE_BEGIN)
{
SInt64 bytesToMove;
if (pOffSetHigh != NULL)
bytesToMove = ((SInt64)*pOffSetHigh << 32) + lOffSetLow;
else
bytesToMove = lOffSetLow;
theErr = FSSetForkPosition((short)(int)hFile, fsFromStart, bytesToMove);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
SetLastError(theErr);
return lOffSetLow;
}
else
{
SInt64 bytesToMove;
if (pOffSetHigh != NULL)
bytesToMove = ((SInt64)*pOffSetHigh << 32) + lOffSetLow;
else
bytesToMove = lOffSetLow;
SInt64 newPos;
theErr = FSSetForkPosition((short)(int)hFile, fsFromLEOF, bytesToMove);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
theErr = FSGetForkPosition((short)(int)hFile, &newPos);
if (theErr != noErr)
{
SetLastError(theErr);
return -1u;
}
if (pOffSetHigh != NULL)
*pOffSetHigh = newPos >> 32;
SetLastError(theErr);
return newPos;
}
}
/********************************************************************
* SetEndOfFile
********************************************************************/
BOOL SetEndOfFile( HANDLE hFile ) /* handle to file */
{
OSErr theErr;
theErr = FSSetForkSize((short)(int)hFile, fsAtMark, 0);
SetLastError(theErr);
return theErr == noErr;
}
/********************************************************************
* ReadFile
* pOverLapped: NULL
********************************************************************/
BOOL ReadFile( HANDLE hFile, /* handle to file */
void *pBuffer, /* data buffer */
DWORD ulLen, /* number of bytes to read */
DWORD *ulRead, /* number of bytes read */
void *pOverLapped ) /* overlapped buffer */
{
#pragma unused (pOverLapped)
ByteCount nbCharsRead;
OSErr theErr;
nbCharsRead = ulLen;
theErr = FSReadFork((short)(int)hFile, fsAtMark, 0, nbCharsRead, pBuffer, &nbCharsRead);
*ulRead = nbCharsRead;
SetLastError(theErr);
return theErr == noErr;
}
/********************************************************************
* WriteFile
* pOverLapped: NULL
********************************************************************/
BOOL WriteFile( HANDLE hFile, /* handle to file */
const void *pBuffer, /* data buffer */
DWORD ulLen, /* number of bytes to write */
DWORD *ulWritten, /* number of bytes written */
void *pOverLapped ) /* overlapped buffer */
{
#pragma unused (pOverLapped)
ByteCount nbCharsToWrite;
OSErr theErr;
nbCharsToWrite = ulLen;
theErr = FSWriteFork((short)(int)hFile, fsAtMark, 0, nbCharsToWrite, pBuffer, &nbCharsToWrite);
*ulWritten = nbCharsToWrite;
SetLastError(theErr);
return theErr == noErr;
}
// Check if a memory block is accessible for reading. It's probably too
// hard to check on Mac, so sorry, we'll just have to crash
BOOL IsBadReadPtr(const void * ptr, int size)
{
#pragma unused (ptr, size)
return FALSE;
}
// Returns attributes of a file. Actually, it doesn't, it just checks if
// the file exists, since that's all StormLib uses it for
DWORD GetFileAttributes(const char * szFileName)
{
FSRef theRef;
OSErr theErr;
theErr = FSLocationFromFullPath(szFileName, &theRef);
if (theErr != noErr)
return -1u;
else
return 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -