X-Git-Url: https://sfsrealm.hopto.org/projects/gitweb.cgi?p=SFmpqapi.git;a=blobdiff_plain;f=SFUtil.cpp;fp=SFUtil.cpp;h=76d2228e351497b8943485382c340e84bdafc809;hp=0000000000000000000000000000000000000000;hb=7214af3eb69acdaef221c1846157ed07438bd0c1;hpb=1c63ef2a5d9f881e1006c4c674a81cebd9596c5e diff --git a/SFUtil.cpp b/SFUtil.cpp new file mode 100644 index 0000000..76d2228 --- /dev/null +++ b/SFUtil.cpp @@ -0,0 +1,96 @@ +// License information for this code is in license.txt + +#include +#include +#include "SFTypes.h" + +void WINAPI SFMemZero(LPVOID lpvDestination, DWORD dwLength) +{ + DWORD dwPrevLen = dwLength; + LPDWORD lpdwDestination = (LPDWORD)lpvDestination; + LPBYTE lpbyDestination; + + dwLength >>= 2; + + while (dwLength--) + *lpdwDestination++ = 0; + + lpbyDestination = (LPBYTE)lpdwDestination; + + dwLength = dwPrevLen; + dwLength &= 3; + + while (dwLength--) + *lpbyDestination++ = 0; +} + +LPVOID WINAPI SFAlloc(DWORD dwSize) +{ + LPVOID lpMemory = malloc(dwSize); + if (lpMemory) SFMemZero(lpMemory,dwSize); + return lpMemory; +} + +void WINAPI SFFree(LPVOID lpvMemory) +{ + if (lpvMemory) free(lpvMemory); +} + +Int64 SFGetFileSize(HANDLE hFile) +{ + IntConv FileSize; + + FileSize.ui64 = 0; + + FileSize.ui32[0] = ::GetFileSize(hFile, &FileSize.ui32[1]); + + if (FileSize.ui32[0] == INVALID_FILE_SIZE) { + if (::GetLastError() != NO_ERROR) + return -1; + } + + return FileSize.i64; +} + +Int64 SFSetFilePointer(HANDLE hFile, Int64 nDistance, UInt32 dwMoveMethod) +{ + IntConv FilePos; + + FilePos.i64 = nDistance; + + FilePos.i32[0] = ::SetFilePointer(hFile, FilePos.i32[0], &FilePos.i32[1], dwMoveMethod); + + if (FilePos.i32[0] == INVALID_SET_FILE_POINTER) { + if (::GetLastError() != NO_ERROR) + return -1; + } + + return FilePos.i64; +} + +size_t strlnlen(const char *strline) +{ + if (strline==0) return 0; + const char *strcr = strchr(strline,'\r'); + const char *strlf = strchr(strline,'\n'); + if (strcr==0 && strlf==0) return strlen(strline); + if (strcr!=0 && (strcr