-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSFUtil.cpp
100 lines (79 loc) · 2.17 KB
/
SFUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// License information for this code is in license.txt
#include <windows.h>
#include <stdlib.h>
#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);
#ifdef INVALID_SET_FILE_POINTER
if (FilePos.ui32[0] == INVALID_SET_FILE_POINTER) {
#else
if (FilePos.ui32[0] == INVALID_FILE_SIZE) {
#endif
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<strlf || strlf==0)) return strcr-strline;
if (strlf!=0 && (strlf<strcr || strcr==0)) return strlf-strline;
return strlen(strline);
}
char *nextline(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 0;
const char *streol = strlf;
if (strcr!=0 && (strcr<strlf || strlf==0)) streol = strcr;
if (strlf!=0 && (strlf<strcr || strcr==0)) streol = strlf;
do {
streol++;
} while (streol[0]=='\r' || streol[0]=='\n');
if (streol[0]==0) return 0;
return (char *)streol;
}