Logo

HRESULT GetTextFile const TCHAR *  pszFileName,
WCHAR **  ppwszCoMem,
ULONG *  pcch
 

retrieve the unicode bytes of the specified text file

Load the content of a text file into a buffer. The buffer is converted into Unicode using the default engine codepage if it isn't already in Unicode

This code was borrowed from SAPI 5.1 sample CRecogDlgClass::GetTextFile

Parameters:
pszFileName - [in] source file name
ppwszCoMem - [out] the loaded text buffer
pcch - [out] size of text buffer
Returns:
HRESULT hr

Definition at line 76 of file sapi_util.cpp.

00080 {
00081 
00082     HRESULT hr = S_OK;
00083     HANDLE hf = INVALID_HANDLE_VALUE;
00084     DWORD cBytes;
00085     BOOL fUnicodeFile = FALSE;
00086     USHORT uTemp;
00087     WCHAR * pwszCoMem = 0;
00088     ULONG cch = 0;
00089     DWORD dwRead;
00090 
00091        
00092 
00093     if (SUCCEEDED(hr))
00094     {
00095         hf = CreateFile(pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
00096 
00097         hr = (hf != INVALID_HANDLE_VALUE) ? S_OK : HRESULT_FROM_WIN32(CommDlgExtendedError());
00098     }
00099 
00100     if (SUCCEEDED(hr))
00101     {
00102         cBytes = GetFileSize(hf, NULL); // 64K limit
00103 
00104         hr = (cBytes != -1) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
00105     }
00106 
00107     if (SUCCEEDED(hr))
00108     {
00109         hr = ReadFile(hf, &uTemp, 2, &dwRead, NULL) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
00110     }
00111 
00112     if (SUCCEEDED(hr))
00113     {
00114         fUnicodeFile = uTemp == 0xfeff;
00115 
00116         if (fUnicodeFile)
00117         {
00118             cBytes -= 2;
00119 
00120             pwszCoMem = (WCHAR *)CoTaskMemAlloc(cBytes);
00121 
00122             if (pwszCoMem)
00123             {
00124                 hr = ReadFile(hf, pwszCoMem, cBytes, &dwRead, NULL) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
00125 
00126                 cch = cBytes / sizeof(WCHAR);
00127             }
00128             else
00129             {
00130                 hr = E_OUTOFMEMORY;
00131             }
00132         }
00133         else
00134         {
00135             SPRECOGNIZERSTATUS stat;
00136             ZeroMemory(&stat, sizeof(stat));
00137             hr = S_OK;
00138             if (SUCCEEDED(hr))
00139             {
00140                 UINT uiCodePage = SpCodePageFromLcid(MAKELCID(LANG_ENGLISH, SORT_DEFAULT));
00141 
00142                 char * pszBuffer = (char *)malloc(cBytes);
00143 
00144                 hr = pszBuffer ? S_OK : E_OUTOFMEMORY;
00145 
00146                 if (SUCCEEDED(hr))
00147                 {
00148                     SetFilePointer(hf, 0, NULL, FILE_BEGIN); // rewind
00149         
00150                     hr = ReadFile(hf, pszBuffer, cBytes, &dwRead, NULL) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
00151                 }
00152 
00153                 if (SUCCEEDED(hr))
00154                 {
00155                     cch = MultiByteToWideChar(uiCodePage, 0, pszBuffer, cBytes, NULL, NULL);
00156 
00157                     if (cch)
00158                     {
00159                         pwszCoMem = (WCHAR *)CoTaskMemAlloc(sizeof(WCHAR) * cch);
00160                     }
00161                     else
00162                     {
00163                         hr = E_FAIL;
00164                     }
00165                 }
00166 
00167                 if (SUCCEEDED(hr))
00168                 {
00169                     MultiByteToWideChar(uiCodePage, 0, pszBuffer, cBytes, pwszCoMem, cch);
00170                 }
00171 
00172                 if (pszBuffer)
00173                 {
00174                     free(pszBuffer);
00175                 }
00176             }
00177         }
00178     }
00179 
00180     if (INVALID_HANDLE_VALUE != hf)
00181     {
00182         CloseHandle(hf);
00183     }
00184 
00185     *ppwszCoMem = pwszCoMem;
00186     *pcch = cch;
00187     
00188     return hr;
00189 }


Copyright (C) 2002-2005 Annosoft LLC. All Rights Reserved.
Visit us at www.annosoft.com