xref: /minix3/external/mit/expat/dist/xmlwf/win32filemap.c (revision 1230fdc108a70388f87f1b3abdb6731e789a6d94)
1*1230fdc1SLionel Sambuc /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2*1230fdc1SLionel Sambuc    See the file COPYING for copying permission.
3*1230fdc1SLionel Sambuc */
4*1230fdc1SLionel Sambuc 
5*1230fdc1SLionel Sambuc #define STRICT 1
6*1230fdc1SLionel Sambuc #define WIN32_LEAN_AND_MEAN 1
7*1230fdc1SLionel Sambuc 
8*1230fdc1SLionel Sambuc #ifdef XML_UNICODE_WCHAR_T
9*1230fdc1SLionel Sambuc #ifndef XML_UNICODE
10*1230fdc1SLionel Sambuc #define XML_UNICODE
11*1230fdc1SLionel Sambuc #endif
12*1230fdc1SLionel Sambuc #endif
13*1230fdc1SLionel Sambuc 
14*1230fdc1SLionel Sambuc #ifdef XML_UNICODE
15*1230fdc1SLionel Sambuc #define UNICODE
16*1230fdc1SLionel Sambuc #define _UNICODE
17*1230fdc1SLionel Sambuc #endif /* XML_UNICODE */
18*1230fdc1SLionel Sambuc #include <windows.h>
19*1230fdc1SLionel Sambuc #include <stdio.h>
20*1230fdc1SLionel Sambuc #include <tchar.h>
21*1230fdc1SLionel Sambuc #include "filemap.h"
22*1230fdc1SLionel Sambuc 
23*1230fdc1SLionel Sambuc static void win32perror(const TCHAR *);
24*1230fdc1SLionel Sambuc 
25*1230fdc1SLionel Sambuc int
filemap(const TCHAR * name,void (* processor)(const void *,size_t,const TCHAR *,void * arg),void * arg)26*1230fdc1SLionel Sambuc filemap(const TCHAR *name,
27*1230fdc1SLionel Sambuc         void (*processor)(const void *, size_t, const TCHAR *, void *arg),
28*1230fdc1SLionel Sambuc         void *arg)
29*1230fdc1SLionel Sambuc {
30*1230fdc1SLionel Sambuc   HANDLE f;
31*1230fdc1SLionel Sambuc   HANDLE m;
32*1230fdc1SLionel Sambuc   DWORD size;
33*1230fdc1SLionel Sambuc   DWORD sizeHi;
34*1230fdc1SLionel Sambuc   void *p;
35*1230fdc1SLionel Sambuc 
36*1230fdc1SLionel Sambuc   f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
37*1230fdc1SLionel Sambuc                           FILE_FLAG_SEQUENTIAL_SCAN, NULL);
38*1230fdc1SLionel Sambuc   if (f == INVALID_HANDLE_VALUE) {
39*1230fdc1SLionel Sambuc     win32perror(name);
40*1230fdc1SLionel Sambuc     return 0;
41*1230fdc1SLionel Sambuc   }
42*1230fdc1SLionel Sambuc   size = GetFileSize(f, &sizeHi);
43*1230fdc1SLionel Sambuc   if (size == (DWORD)-1) {
44*1230fdc1SLionel Sambuc     win32perror(name);
45*1230fdc1SLionel Sambuc     return 0;
46*1230fdc1SLionel Sambuc   }
47*1230fdc1SLionel Sambuc   if (sizeHi) {
48*1230fdc1SLionel Sambuc     _ftprintf(stderr, _T("%s: bigger than 2Gb\n"), name);
49*1230fdc1SLionel Sambuc     return 0;
50*1230fdc1SLionel Sambuc   }
51*1230fdc1SLionel Sambuc   /* CreateFileMapping barfs on zero length files */
52*1230fdc1SLionel Sambuc   if (size == 0) {
53*1230fdc1SLionel Sambuc     static const char c = '\0';
54*1230fdc1SLionel Sambuc     processor(&c, 0, name, arg);
55*1230fdc1SLionel Sambuc     CloseHandle(f);
56*1230fdc1SLionel Sambuc     return 1;
57*1230fdc1SLionel Sambuc   }
58*1230fdc1SLionel Sambuc   m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL);
59*1230fdc1SLionel Sambuc   if (m == NULL) {
60*1230fdc1SLionel Sambuc     win32perror(name);
61*1230fdc1SLionel Sambuc     CloseHandle(f);
62*1230fdc1SLionel Sambuc     return 0;
63*1230fdc1SLionel Sambuc   }
64*1230fdc1SLionel Sambuc   p = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
65*1230fdc1SLionel Sambuc   if (p == NULL) {
66*1230fdc1SLionel Sambuc     win32perror(name);
67*1230fdc1SLionel Sambuc     CloseHandle(m);
68*1230fdc1SLionel Sambuc     CloseHandle(f);
69*1230fdc1SLionel Sambuc     return 0;
70*1230fdc1SLionel Sambuc   }
71*1230fdc1SLionel Sambuc   processor(p, size, name, arg);
72*1230fdc1SLionel Sambuc   UnmapViewOfFile(p);
73*1230fdc1SLionel Sambuc   CloseHandle(m);
74*1230fdc1SLionel Sambuc   CloseHandle(f);
75*1230fdc1SLionel Sambuc   return 1;
76*1230fdc1SLionel Sambuc }
77*1230fdc1SLionel Sambuc 
78*1230fdc1SLionel Sambuc static void
win32perror(const TCHAR * s)79*1230fdc1SLionel Sambuc win32perror(const TCHAR *s)
80*1230fdc1SLionel Sambuc {
81*1230fdc1SLionel Sambuc   LPVOID buf;
82*1230fdc1SLionel Sambuc   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
83*1230fdc1SLionel Sambuc                     | FORMAT_MESSAGE_FROM_SYSTEM,
84*1230fdc1SLionel Sambuc                     NULL,
85*1230fdc1SLionel Sambuc                     GetLastError(),
86*1230fdc1SLionel Sambuc                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
87*1230fdc1SLionel Sambuc                     (LPTSTR) &buf,
88*1230fdc1SLionel Sambuc                     0,
89*1230fdc1SLionel Sambuc                     NULL)) {
90*1230fdc1SLionel Sambuc     _ftprintf(stderr, _T("%s: %s"), s, buf);
91*1230fdc1SLionel Sambuc     fflush(stderr);
92*1230fdc1SLionel Sambuc     LocalFree(buf);
93*1230fdc1SLionel Sambuc   }
94*1230fdc1SLionel Sambuc   else
95*1230fdc1SLionel Sambuc     _ftprintf(stderr, _T("%s: unknown Windows error\n"), s);
96*1230fdc1SLionel Sambuc }
97