Current News Archived News Search News Discussion Forum Old Forum Install Programs More Downloads... Troubleshooting Source Code Format Specs. Misc. Information Non-SF Stuff Links Small banner for links to this site: |
1 // License information for this code is in license.txt
3 #include <windows.h>
4 #include <stdlib.h>
5 #include "SFTypes.h"
7 void WINAPI SFMemZero(LPVOID lpvDestination, DWORD dwLength)
8 {
9 DWORD dwPrevLen = dwLength;
10 LPDWORD lpdwDestination = (LPDWORD)lpvDestination;
11 LPBYTE lpbyDestination;
13 dwLength >>= 2;
15 while (dwLength--)
16 *lpdwDestination++ = 0;
18 lpbyDestination = (LPBYTE)lpdwDestination;
20 dwLength = dwPrevLen;
21 dwLength &= 3;
23 while (dwLength--)
24 *lpbyDestination++ = 0;
25 }
27 LPVOID WINAPI SFAlloc(DWORD dwSize)
28 {
29 LPVOID lpMemory = malloc(dwSize);
30 if (lpMemory) SFMemZero(lpMemory,dwSize);
31 return lpMemory;
32 }
34 void WINAPI SFFree(LPVOID lpvMemory)
35 {
36 if (lpvMemory) free(lpvMemory);
37 }
39 Int64 SFGetFileSize(HANDLE hFile)
40 {
41 IntConv FileSize;
43 FileSize.ui64 = 0;
45 FileSize.ui32[0] = ::GetFileSize(hFile, &FileSize.ui32[1]);
47 if (FileSize.ui32[0] == INVALID_FILE_SIZE) {
48 if (::GetLastError() != NO_ERROR)
49 return -1;
50 }
52 return FileSize.i64;
53 }
55 Int64 SFSetFilePointer(HANDLE hFile, Int64 nDistance, UInt32 dwMoveMethod)
56 {
57 IntConv FilePos;
59 FilePos.i64 = nDistance;
61 FilePos.i32[0] = ::SetFilePointer(hFile, FilePos.i32[0], &FilePos.i32[1], dwMoveMethod);
63 #ifdef INVALID_SET_FILE_POINTER
64 if (FilePos.ui32[0] == INVALID_SET_FILE_POINTER) {
65 #else
66 if (FilePos.ui32[0] == INVALID_FILE_SIZE) {
67 #endif
68 if (::GetLastError() != NO_ERROR)
69 return -1;
70 }
72 return FilePos.i64;
73 }
75 size_t strlnlen(const char *strline)
76 {
77 if (strline==0) return 0;
78 const char *strcr = strchr(strline,'\r');
79 const char *strlf = strchr(strline,'\n');
80 if (strcr==0 && strlf==0) return strlen(strline);
81 if (strcr!=0 && (strcr<strlf || strlf==0)) return strcr-strline;
82 if (strlf!=0 && (strlf<strcr || strcr==0)) return strlf-strline;
83 return strlen(strline);
84 }
86 char *nextline(const char *strline)
87 {
88 if (strline==0) return 0;
89 const char *strcr = strchr(strline,'\r');
90 const char *strlf = strchr(strline,'\n');
91 if (strcr==0 && strlf==0) return 0;
92 const char *streol = strlf;
93 if (strcr!=0 && (strcr<strlf || strlf==0)) streol = strcr;
94 if (strlf!=0 && (strlf<strcr || strcr==0)) streol = strlf;
95 do {
96 streol++;
97 } while (streol[0]=='\r' || streol[0]=='\n');
98 if (streol[0]==0) return 0;
99 return (char *)streol;
100 }
|