home *** CD-ROM | disk | FTP | other *** search
- // GetFile.cpp -- Global function GetOSFile
- //
- // This file is part of Microsoft SQL Server online documentation.
- // Copyright (C) 1992-1997 Microsoft Corporation. All rights reserved.
- //
- // This source code is an intended supplement to the Microsoft SQL
- // Server online references and related electronic documentation.
- #include "stdafx.h"
- #include "GetFile.h"
-
- //////////////////////////////////////////////////////////////////////////
- // GetOSFile -- Reads the specified data file into a chunk of memory.
- //
- // Returns TRUE if successful, FALSE otherwise.
- //
- // The caller is responsible for freeing the returned handle.
- BOOL GetOSFile
- (
- LPCTSTR szPathname,
- PBYTE* ppData
- )
- {
- HANDLE hFile;
- DWORD cbHigh;
- DWORD cbLow;
- DWORD cbRead;
- PBYTE pData;
- WCHAR wcFirst;
-
- // Set up for failure
- *ppData = NULL;
-
- hFile = CreateFile(szPathname, GENERIC_READ, 0, NULL, OPEN_EXISTING,
- FILE_FLAG_SEQUENTIAL_SCAN, NULL);
-
- if (hFile == INVALID_HANDLE_VALUE)
- {
- return (FALSE);
- }
-
- cbLow = GetFileSize(hFile, &cbHigh);
-
- if (cbLow == (DWORD) -1)
- {
- CloseHandle(hFile);
- return (FALSE);
- }
-
- pData = new BYTE[cbLow + 1];
- if (pData == NULL)
- {
- CloseHandle(hFile);
- return (FALSE);
- }
-
- if (ReadFile(hFile, pData, cbLow, &cbRead, NULL) == FALSE)
- {
- delete [] pData;
- return (FALSE);
- }
-
- CloseHandle(hFile);
- wcFirst = *((WCHAR*) pData);
- if (wcFirst == 0xFEFF || wcFirst == 0xFFFE)
- {
- // We don't handle UNICODE text files yet.
- delete [] pData;
- return (FALSE);
- }
-
- pData[cbLow] = (char) NULL;
- *ppData = pData;
-
- return (TRUE);
- }
-
-