Recognizes if input is ogg or not.
[xiph.git] / oggds / PersistPropertyBag.cpp
blobfe7d8b23fa2bdfe368fe428f9da432cb853f3b4b
1 /*******************************************************************************
2 * *
3 * This file is part of the Ogg Vorbis DirectShow filter collection *
4 * *
5 * Copyright (c) 2001, Tobias Waldvogel *
6 * All rights reserved. *
7 * *
8 * Redistribution and use in source and binary forms, with or without *
9 * modification, are permitted provided that the following conditions are met: *
10 * *
11 * - Redistributions of source code must retain the above copyright notice, *
12 * this list of conditions and the following disclaimer. *
13 * *
14 * - Redistributions in binary form must reproduce the above copyright notice, *
15 * this list of conditions and the following disclaimer in the documentation *
16 * and/or other materials provided with the distribution. *
17 * *
18 * - The names of the contributors may not be used to endorse or promote *
19 * products derived from this software without specific prior written *
20 * permission. *
21 * *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE *
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN *
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) *
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
32 * POSSIBILITY OF SUCH DAMAGE. *
33 * *
34 *******************************************************************************/
36 #include "PersistPropertyBag.h"
37 #include "common.h"
38 #include <stdio.h>
40 CPersistPropertyBag::CPersistPropertyBag(const char** arProps, int iProps, const GUID* pClsID)
42 m_iProps = iProps;
43 m_arProps = arProps;
44 m_pClsID = pClsID;
47 HRESULT STDMETHODCALLTYPE CPersistPropertyBag::GetClassID(CLSID* pClsID)
49 *pClsID = * m_pClsID;
50 return NOERROR;
53 HRESULT STDMETHODCALLTYPE CPersistPropertyBag::InitNew(void)
55 return NOERROR;
58 // Walks through all properties in m_arProps and calls read
59 HRESULT STDMETHODCALLTYPE CPersistPropertyBag::Load(IPropertyBag *pPropBag, IErrorLog *pErrorLog)
61 VARIANT V;
62 wchar_t wzName[128];
64 VariantInit(&V);
66 for (int i=0; i<m_iProps; i++)
68 mbstowcs(wzName, m_arProps[i], 128);
69 if (SUCCEEDED(pPropBag->Read(wzName, &V, NULL)))
70 Write(wzName, &V);
73 VariantClear(&V);
74 return NOERROR;
77 // Walks through all properties in m_arProps and calls write
78 HRESULT STDMETHODCALLTYPE CPersistPropertyBag::Save(IPropertyBag *pPropBag, BOOL fClearDirty,
79 BOOL fSaveAllProperties)
81 VARIANT V;
82 wchar_t wzName[128];
84 VariantInit(&V);
86 for (int i=0; i<m_iProps; i++)
88 mbstowcs(wzName, m_arProps[i], 128);
89 if (SUCCEEDED(Read(wzName, &V, NULL)))
90 pPropBag->Write(wzName, &V);
93 VariantClear(&V);
94 return NOERROR;
97 CRegistryStuff::CRegistryStuff(const GUID* pClsID)
99 m_pClsID = pClsID;
102 // Returns a handle to the registry key HKEY_CLASSES_ROOT, CLSID\<idCLSID>\defaults
103 HKEY CRegistryStuff::OpenRegistry()
105 OLECHAR szCLSID[CHARS_IN_GUID];
106 CHAR chCLSIDEntry[260];
107 LPDWORD lpdwDisposition = 0;
108 HKEY hKey;
109 LONG result;
111 StringFromGUID2(*m_pClsID, szCLSID, CHARS_IN_GUID);
112 wsprintf(chCLSIDEntry, "CLSID\\%ls\\Defaults", szCLSID);
113 result = RegCreateKeyEx(HKEY_CLASSES_ROOT, chCLSIDEntry, 0, "",
114 REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL,
115 &hKey, lpdwDisposition);
117 return result == ERROR_SUCCESS ? hKey : NULL;
120 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, bool bVal)
122 if (HKEY hKey = OpenRegistry())
124 DWORD dwData = bVal ? 1 : 0;
125 RegSetValueEx(hKey, szName, 0, REG_DWORD, (CONST BYTE*)&dwData, sizeof(dwData));
126 RegCloseKey(hKey);
127 return NOERROR;
129 return E_FAIL;
132 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, __int32 iVal)
134 return SaveToRegistry(szName, (unsigned __int32)iVal);
137 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, unsigned __int32 iVal)
139 if (HKEY hKey = OpenRegistry())
141 RegSetValueEx(hKey, szName, 0, REG_DWORD, (CONST BYTE*)&iVal, sizeof(iVal));
142 RegCloseKey(hKey);
143 return NOERROR;
145 return E_FAIL;
148 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, float fVal)
150 if (HKEY hKey = OpenRegistry())
152 char szVal[32];
153 sprintf(szVal, "%f", fVal);
154 RegSetValueEx(hKey, szName, 0, REG_SZ, (CONST BYTE*)szVal, strlen(szVal));
155 RegCloseKey(hKey);
156 return NOERROR;
158 return E_FAIL;
162 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, char* szVal)
164 if (HKEY hKey = OpenRegistry())
166 RegSetValueEx(hKey, szName, 0, REG_SZ, (CONST BYTE*)szVal, strlen(szVal)+1);
167 RegCloseKey(hKey);
168 return NOERROR;
170 return E_FAIL;
173 HRESULT CRegistryStuff::SaveToRegistry(const char* szName, wchar_t* wzVal)
175 if (HKEY hKey = OpenRegistry())
177 char szValue[128];
179 wcstombs(szValue, wzVal, 128);
180 RegSetValueEx(hKey, szName, 0, REG_SZ, (CONST BYTE*)szValue, strlen(szValue)+1);
181 RegCloseKey(hKey);
182 return NOERROR;
184 return E_FAIL;
188 HRESULT CRegistryStuff::LoadFromRegistry(const char* szName,
189 bool* pbVal, bool bDefault)
191 if (HKEY hKey = OpenRegistry())
193 DWORD dwData;
194 DWORD dwSize = sizeof(dwData);
196 if (RegQueryValueEx(hKey, szName, NULL, NULL, (BYTE*)&dwData, &dwSize) == ERROR_SUCCESS)
197 *pbVal = dwData != 0;
198 else
199 *pbVal = bDefault;
201 RegCloseKey(hKey);
202 return NOERROR;
204 return E_FAIL;
207 HRESULT CRegistryStuff::LoadFromRegistry(const char* szName,
208 __int32* piVal, __int32 iDefault)
210 return LoadFromRegistry(szName, (unsigned __int32*) piVal,
211 (unsigned __int32) iDefault);
214 HRESULT CRegistryStuff::LoadFromRegistry(const char* szName,
215 unsigned __int32* piVal, unsigned __int32 iDefault)
217 if (HKEY hKey = OpenRegistry())
219 DWORD dwSize = sizeof(*piVal);
221 if (RegQueryValueEx(hKey, szName, NULL, NULL, (BYTE*)piVal, &dwSize) != ERROR_SUCCESS)
222 *piVal = iDefault;
224 RegCloseKey(hKey);
225 return NOERROR;
227 return E_FAIL;
230 HRESULT CRegistryStuff::LoadFromRegistry(const char* szName,
231 float* pfVal, float fDefault)
233 if (HKEY hKey = OpenRegistry())
235 char szVal[32];
236 DWORD dwSize = sizeof(szVal);
238 if (RegQueryValueEx(hKey, szName, NULL, NULL, (BYTE*)szVal, &dwSize) == ERROR_SUCCESS)
239 *pfVal = atof(szVal);
240 else
241 *pfVal = fDefault;
243 RegCloseKey(hKey);
244 return NOERROR;
246 return E_FAIL;