Logo

sapi_util.cpp

Go to the documentation of this file.
00001 /* 
00002 sapi_util.cpp
00003 
00004 Copyright (C) 2005 Annosoft, LLC. Richardson, Texas. 
00005 All rights reserved.  
00006     
00007 Permission is hereby granted, free of charge, to use and distribute
00008 this software and its documentation without restriction, including   
00009 without limitation the rights to use, copy, modify, merge, publish,  
00010 distribute, sublicense, and/or sell copies of this work, and to      
00011 permit persons to whom this work is furnished to do so, subject to   
00012 the following conditions:                                            
00013 1. The code must retain the above copyright notice, this list of    
00014     conditions and the following disclaimer.                        
00015 2. Any modifications must be clearly marked as such.                
00016 3. Original authors' names are not deleted.                         
00017 4. The name "Annosoft" and the authors' names can be not used to endorse or 
00018    promote products derived from this software without specific prior written       
00019    permission.                                            
00020 
00021 ANNOSOFT AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES 
00022 WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 
00023 MERCHANTABILITY AND FITNESS, IN NO EVENT ANNOSOFT NOR THE CONTRIBUTORS 
00024 BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY 
00025 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   
00026 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 
00027 OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
00028 
00029 */
00030 
00036 #include "stdafx.h"
00037 #include <comdef.h> // for nice bstr_t thing!
00038 #include "sapi_util.h"
00039 
00041 std::wstring TCHAR_2_wstring(const TCHAR *str)
00042 {
00043 #if _UNICODE
00044     return std::wstring(str);
00045 #else
00046     bstr_t bst = str;
00047     std::wstring ret = bst;
00048     return (ret);
00049 #endif
00050 }
00051 
00053 std::string  wstring_2_string(const std::wstring& str)
00054 {
00055     bstr_t bst = str.c_str();
00056     std::string ret = bst;
00057     return (ret);
00058 }
00059 
00060 
00061 
00063 
00076 HRESULT GetTextFile(
00077     const TCHAR* pszFileName,
00078     WCHAR ** ppwszCoMem,
00079     ULONG * pcch)
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