home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / ODBC / loaddata / getfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  1.8 KB  |  77 lines

  1. // GetFile.cpp -- Global function GetOSFile
  2. //
  3. // This file is part of Microsoft SQL Server online documentation.
  4. // Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
  5. //
  6. // This source code is an intended supplement to the Microsoft SQL
  7. // Server online references and related electronic documentation.
  8. #include "stdafx.h"
  9. #include "GetFile.h"
  10.  
  11. //////////////////////////////////////////////////////////////////////////
  12. // GetOSFile -- Reads the specified data file into a chunk of memory.
  13. //
  14. // Returns TRUE if successful, FALSE otherwise.
  15. //
  16. // The caller is responsible for freeing the returned handle.
  17. BOOL GetOSFile
  18.     (
  19.     LPCTSTR szPathname,
  20.     PBYTE* ppData
  21.     )
  22.     {
  23.     HANDLE      hFile;
  24.     DWORD       cbHigh;
  25.     DWORD       cbLow;
  26.     DWORD       cbRead;
  27.     PBYTE       pData;
  28.     WCHAR       wcFirst;
  29.     
  30.     // Set up for failure
  31.     *ppData = NULL;
  32.  
  33.     hFile = CreateFile(szPathname, GENERIC_READ, 0, NULL, OPEN_EXISTING,
  34.         FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  35.  
  36.     if (hFile == INVALID_HANDLE_VALUE)
  37.         {
  38.         return (FALSE);
  39.         }
  40.  
  41.     cbLow = GetFileSize(hFile, &cbHigh);
  42.  
  43.     if (cbLow == (DWORD) -1)
  44.         {
  45.         CloseHandle(hFile);
  46.         return (FALSE);
  47.         }
  48.  
  49.     pData = new BYTE[cbLow + 1];
  50.     if (pData == NULL)
  51.         {
  52.         CloseHandle(hFile);
  53.         return (FALSE);
  54.         }
  55.  
  56.     if (ReadFile(hFile, pData, cbLow, &cbRead, NULL) == FALSE)
  57.         {
  58.         delete [] pData;
  59.         return (FALSE);
  60.         }
  61.  
  62.     CloseHandle(hFile);
  63.     wcFirst = *((WCHAR*) pData);
  64.     if (wcFirst == 0xFEFF || wcFirst == 0xFFFE)
  65.         {
  66.         // We don't handle UNICODE text files yet.
  67.         delete [] pData;
  68.         return (FALSE);
  69.         }
  70.  
  71.     pData[cbLow] = (char) NULL;
  72.     *ppData = pData;
  73.  
  74.     return (TRUE);
  75.     }
  76.  
  77.