home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / exml.lha / exml / expat / xmlwf / win32filemap.c < prev    next >
C/C++ Source or Header  |  1999-04-13  |  3KB  |  103 lines

  1. /*
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.0 (the "License"); you may not use this file except in
  4. compliance with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6.  
  7. Software distributed under the License is distributed on an "AS IS"
  8. basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. License for the specific language governing rights and limitations
  10. under the License.
  11.  
  12. The Original Code is expat.
  13.  
  14. The Initial Developer of the Original Code is James Clark.
  15. Portions created by James Clark are Copyright (C) 1998
  16. James Clark. All Rights Reserved.
  17.  
  18. Contributor(s):
  19. */
  20.  
  21. #define STRICT 1
  22. #ifdef XML_UNICODE
  23. #define UNICODE
  24. #define _UNICODE
  25. #endif /* XML_UNICODE */
  26. #include <windows.h>
  27. #include <stdio.h>
  28. #include <tchar.h>
  29. #include "filemap.h"
  30.  
  31. static void win32perror(const TCHAR *);
  32.  
  33. int filemap(const TCHAR *name,
  34.         void (*processor)(const void *, size_t, const TCHAR *, void *arg),
  35.         void *arg)
  36. {
  37.   HANDLE f;
  38.   HANDLE m;
  39.   DWORD size;
  40.   DWORD sizeHi;
  41.   void *p;
  42.  
  43.   f = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
  44.               FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  45.   if (f == INVALID_HANDLE_VALUE) {
  46.     win32perror(name);
  47.     return 0;
  48.   }
  49.   size = GetFileSize(f, &sizeHi);
  50.   if (size == (DWORD)-1) {
  51.     win32perror(name);
  52.     return 0;
  53.   }
  54.   if (sizeHi) {
  55.     _ftprintf(stderr, _T("%s: bigger than 2Gb\n"), name);
  56.     return 0;
  57.   }
  58.   /* CreateFileMapping barfs on zero length files */
  59.   if (size == 0) {
  60.     static const char c = '\0';
  61.     processor(&c, 0, name, arg);
  62.     CloseHandle(f);
  63.     return 1;
  64.   }
  65.   m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL);
  66.   if (m == NULL) {
  67.     win32perror(name);
  68.     CloseHandle(f);
  69.     return 0;
  70.   }
  71.   p = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
  72.   if (p == NULL) {
  73.     win32perror(name);
  74.     CloseHandle(m);
  75.     CloseHandle(f);
  76.     return 0;
  77.   }
  78.   processor(p, size, name, arg); 
  79.   UnmapViewOfFile(p);
  80.   CloseHandle(m);
  81.   CloseHandle(f);
  82.   return 1;
  83. }
  84.  
  85. static
  86. void win32perror(const TCHAR *s)
  87. {
  88.   LPVOID buf;
  89.   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  90.             NULL,
  91.             GetLastError(),
  92.             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  93.             (LPTSTR) &buf,
  94.             0,
  95.             NULL)) {
  96.     _ftprintf(stderr, _T("%s: %s"), s, buf);
  97.     fflush(stderr);
  98.     LocalFree(buf);
  99.   }
  100.   else
  101.     _ftprintf(stderr, _T("%s: unknown Windows error\n"), s);
  102. }
  103.