Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
4.9 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997
  5. //
  6. // File: cmime.cxx
  7. //
  8. // Contents: MimeType object
  9. //
  10. // History: 04-1-97 krishnag Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "iis.hxx"
  14. #pragma hdrstop
  15. // Class CMimeType
  16. DEFINE_Simple_IDispatch_Implementation(CMimeType)
  17. CMimeType::CMimeType():
  18. _pDispMgr(NULL),
  19. _lpMimeType(NULL),
  20. _lpExtension(NULL)
  21. {
  22. ENLIST_TRACKING(CMimeType);
  23. }
  24. HRESULT
  25. CMimeType::CreateMimeType(
  26. REFIID riid,
  27. void **ppvObj
  28. )
  29. {
  30. CMimeType FAR * pMimeType = NULL;
  31. HRESULT hr = S_OK;
  32. hr = AllocateMimeTypeObject(&pMimeType);
  33. BAIL_ON_FAILURE(hr);
  34. hr = pMimeType->QueryInterface(riid, ppvObj);
  35. BAIL_ON_FAILURE(hr);
  36. pMimeType->Release();
  37. RRETURN(hr);
  38. error:
  39. delete pMimeType;
  40. RRETURN(hr);
  41. }
  42. CMimeType::~CMimeType( )
  43. {
  44. if (_lpMimeType) {
  45. FreeADsStr(_lpMimeType);
  46. }
  47. if (_lpExtension) {
  48. FreeADsStr(_lpExtension);
  49. }
  50. delete _pDispMgr;
  51. }
  52. STDMETHODIMP
  53. CMimeType::QueryInterface(
  54. REFIID iid,
  55. LPVOID FAR* ppv
  56. )
  57. {
  58. if (IsEqualIID(iid, IID_IUnknown))
  59. {
  60. *ppv = (IISMimeType FAR *) this;
  61. }
  62. else if (IsEqualIID(iid, IID_IISMimeType))
  63. {
  64. *ppv = (IISMimeType FAR *) this;
  65. }
  66. else if (IsEqualIID(iid, IID_IDispatch))
  67. {
  68. *ppv = (IISMimeType FAR *) this;
  69. }
  70. else
  71. {
  72. *ppv = NULL;
  73. return E_NOINTERFACE;
  74. }
  75. AddRef();
  76. return NOERROR;
  77. }
  78. HRESULT
  79. CMimeType::InitFromIISString(
  80. LPWSTR pszStr
  81. )
  82. {
  83. LPWSTR pszToken = NULL;
  84. DWORD dwLen = 0;
  85. if (!pszStr) {
  86. return S_FALSE;
  87. }
  88. //
  89. // get length of pszStr; do not count ',' ; + 1 for null pointer and -1
  90. // for ',' so dwLen is = wcslen
  91. //
  92. dwLen = wcslen(pszStr);
  93. //
  94. // first token is extension
  95. //
  96. pszToken = wcstok(pszStr, L",");
  97. if (pszToken) {
  98. _lpExtension = AllocADsStr(pszToken);
  99. //
  100. // second token is mimetype
  101. //
  102. if (wcslen(pszStr) + 1 < dwLen) {
  103. pszToken = pszStr + wcslen(pszStr) + 1;
  104. _lpMimeType = AllocADsStr(pszToken);
  105. }
  106. }
  107. return S_OK;
  108. }
  109. HRESULT
  110. CMimeType::CopyMimeType(
  111. LPWSTR *ppszMimeType
  112. )
  113. {
  114. HRESULT hr = S_OK;
  115. LPWSTR pszMimeType = NULL;
  116. DWORD dwLen = 0;
  117. if (!ppszMimeType) {
  118. return S_FALSE;
  119. }
  120. if (_lpExtension) {
  121. dwLen = wcslen(_lpExtension);
  122. }
  123. if (_lpMimeType) {
  124. dwLen += wcslen(_lpMimeType);
  125. }
  126. //
  127. // dwLen +2 to include comma and null terminator
  128. //
  129. pszMimeType = (LPWSTR)AllocADsMem((dwLen+2) * sizeof(WCHAR));
  130. if (!pszMimeType) {
  131. hr = E_OUTOFMEMORY;
  132. BAIL_ON_FAILURE(hr);
  133. }
  134. //
  135. // empty contents
  136. //
  137. wcscpy(pszMimeType, L"");
  138. if (_lpExtension) {
  139. wcscpy(pszMimeType, _lpExtension);
  140. }
  141. wcscat(pszMimeType, L",");
  142. if (_lpMimeType) {
  143. wcscat(pszMimeType, _lpMimeType);
  144. }
  145. pszMimeType[wcslen(pszMimeType)] = L'\0';
  146. *ppszMimeType = pszMimeType;
  147. error:
  148. RRETURN(hr);
  149. }
  150. HRESULT
  151. CMimeType::AllocateMimeTypeObject(
  152. CMimeType ** ppMimeType
  153. )
  154. {
  155. CMimeType FAR * pMimeType = NULL;
  156. CAggregatorDispMgr FAR * pDispMgr = NULL;
  157. HRESULT hr = S_OK;
  158. pMimeType = new CMimeType();
  159. if (pMimeType == NULL) {
  160. hr = E_OUTOFMEMORY;
  161. }
  162. BAIL_ON_FAILURE(hr);
  163. pDispMgr = new CAggregatorDispMgr;
  164. if (pDispMgr == NULL) {
  165. hr = E_OUTOFMEMORY;
  166. }
  167. BAIL_ON_FAILURE(hr);
  168. hr = pDispMgr->LoadTypeInfoEntry(
  169. LIBID_IISOle,
  170. IID_IISMimeType,
  171. (IISMimeType *)pMimeType,
  172. DISPID_REGULAR
  173. );
  174. BAIL_ON_FAILURE(hr);
  175. pMimeType->_pDispMgr = pDispMgr;
  176. *ppMimeType = pMimeType;
  177. RRETURN(hr);
  178. error:
  179. delete pDispMgr;
  180. RRETURN(hr);
  181. }
  182. STDMETHODIMP
  183. CMimeType::get_MimeType(THIS_ BSTR FAR * retval)
  184. {
  185. HRESULT hr = S_OK;
  186. hr = ADsAllocString(_lpMimeType, retval);
  187. RRETURN(hr);
  188. }
  189. STDMETHODIMP
  190. CMimeType::put_MimeType(THIS_ BSTR bstrMimeType)
  191. {
  192. if (!bstrMimeType) {
  193. RRETURN(E_FAIL);
  194. }
  195. if (_lpMimeType) {
  196. FreeADsStr(_lpMimeType);
  197. }
  198. _lpMimeType = AllocADsStr(bstrMimeType);
  199. if (!_lpMimeType) {
  200. RRETURN(E_OUTOFMEMORY);
  201. }
  202. RRETURN(S_OK);
  203. }
  204. STDMETHODIMP
  205. CMimeType::get_Extension(THIS_ BSTR FAR * retval)
  206. {
  207. HRESULT hr = S_OK;
  208. hr = ADsAllocString(_lpExtension, retval);
  209. RRETURN(hr);
  210. }
  211. STDMETHODIMP
  212. CMimeType::put_Extension(THIS_ BSTR bstrExtension)
  213. {
  214. if (!bstrExtension) {
  215. RRETURN(E_FAIL);
  216. }
  217. if (_lpExtension) {
  218. FreeADsStr(_lpExtension);
  219. }
  220. _lpExtension = AllocADsStr(bstrExtension);
  221. if (!_lpExtension) {
  222. RRETURN(E_OUTOFMEMORY);
  223. }
  224. RRETURN(S_OK);
  225. }