Separate file mapping code into separate file

This commit is contained in:
James Clark 1997-11-15 00:37:29 +00:00
parent 098f26ac79
commit cd5fb875b4

View File

@ -1,53 +1,17 @@
#define STRICT 1
#include <windows.h>
#include <stddef.h>
#include <stdio.h>
#include "wfcheck.h"
#include "filemap.h"
static
int doFile(const char *name)
void processFile(const void *data, size_t size, const char *filename, void *arg)
{
HANDLE f;
HANDLE m;
DWORD size;
DWORD sizeHi;
const char *p;
const char *badPtr = 0;
unsigned long badLine = 0;
unsigned long badCol = 0;
int ret;
int *ret = arg;
enum WfCheckResult result;
f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (f == INVALID_HANDLE_VALUE) {
fprintf(stderr, "%s: CreateFile failed\n", name);
return 0;
}
size = GetFileSize(f, &sizeHi);
if (sizeHi) {
fprintf(stderr, "%s: too big (limit 2Gb)\n", name);
return 0;
}
/* CreateFileMapping barfs on zero length files */
if (size == 0) {
fprintf(stderr, "%s: zero-length file\n", name);
return 0;
}
m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL);
if (m == NULL) {
fprintf(stderr, "%s: CreateFileMapping failed\n", name);
CloseHandle(f);
return 0;
}
p = (const char *)MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
if (p == NULL) {
CloseHandle(m);
CloseHandle(f);
fprintf(stderr, "%s: MapViewOfFile failed\n", name);
return 0;
}
result = wfCheck(p, size, &badPtr, &badLine, &badCol);
result = wfCheck(data, size, &badPtr, &badLine, &badCol);
if (result) {
static const char *message[] = {
0,
@ -61,21 +25,17 @@ int doFile(const char *name)
"duplicate attribute",
"junk after document element",
};
fprintf(stderr, "%s:", name);
fprintf(stderr, "%s:", filename);
if (badPtr != 0)
fprintf(stderr, "%lu:%lu:", badLine+1, badCol);
fprintf(stderr, "%c: %s", (result == wellFormedOutsideDtd ? 'W' : 'E'), message[result]);
putc('\n', stderr);
ret = 1;
if (!*ret)
*ret = 1;
}
else
ret = 0;
UnmapViewOfFile(p);
CloseHandle(m);
CloseHandle(f);
return ret;
}
int main(int argc, char **argv)
{
int i;
@ -85,9 +45,8 @@ int main(int argc, char **argv)
return 1;
}
for (i = 1; i < argc; i++) {
int n = doFile(argv[i]);
if (n > ret)
ret = n;
if (!filemap(argv[i], processFile, &ret))
ret = 2;
}
return ret;
}