Make WvStreams compile with gcc 4.4.
[wvstreams.git] / win32 / unipstoregen.cc
blob2b58b0b20403a7abe1f2cc1c3b8cae38dad26924
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2003 Net Integration Technologies, Inc.
4 *
5 * A generator that exposes Windows protected storage.
6 */
7 #include "unipstoregen.h"
8 #include "wvmoniker.h"
9 #include "wvlinkerhack.h"
10 #include <string>
12 WV_LINK(UniPStoreGen);
15 static const int MAX = 1024;
17 using namespace PSTORECLib;
19 typedef HRESULT (WINAPI *PStoreCreateInstancePtr)(IPStore **, DWORD, DWORD, DWORD);
21 HRESULT UniPStoreGen::create_types(WvString type_name, WvString subtype_name)
23 HRESULT hRes;
25 _PST_TYPEINFO myTypeInfo;
26 myTypeInfo.cbSize = strlen(type_name.cstr()) + 1;
27 myTypeInfo.szDisplayName = new wchar_t[myTypeInfo.cbSize];
28 mbstowcs(myTypeInfo.szDisplayName, type_name.cstr(), myTypeInfo.cbSize);
30 _PST_TYPEINFO mySubTypeInfo;
31 mySubTypeInfo.cbSize = strlen(subtype_name.cstr()) + 1;
32 mySubTypeInfo.szDisplayName = new wchar_t[mySubTypeInfo.cbSize];
33 mbstowcs(mySubTypeInfo.szDisplayName, subtype_name.cstr(), mySubTypeInfo.cbSize);
35 _PST_ACCESSRULESET myRuleSet;
36 myRuleSet.cbSize = sizeof(myRuleSet);
37 myRuleSet.cRules = 0;
38 myRuleSet.rgRules = 0;
40 hRes = m_spPStore->CreateType( m_key, &m_type, &myTypeInfo, 0);
42 if ((hRes != PST_E_OK) && (hRes != PST_E_TYPE_EXISTS))
44 m_log("CreateSubtype() returned: %s\n", hRes);
45 goto done;
48 hRes = m_spPStore->CreateSubtype( m_key, &m_type, &m_subtype, &mySubTypeInfo, &myRuleSet, 0);
49 if ((hRes != PST_E_OK) && (hRes != PST_E_TYPE_EXISTS))
51 m_log("CreateSubtype() returned: %s\n", hRes);
52 goto done;
55 done:
56 delete[] myTypeInfo.szDisplayName;
57 delete[] mySubTypeInfo.szDisplayName;
58 return hRes;
61 // moniker is
62 // PST_KEY_CURRENT_USER:TYPENAME:TYPEGUID:SUBTYPE:SUBTYPEGUID
63 UniPStoreGen::UniPStoreGen(WvString _moniker) :
64 m_log(_moniker), m_key(-1)
66 // load the library and get an entry point function pointer
67 m_hPstoreDLL = LoadLibrary("pstorec.dll");
68 assert(m_hPstoreDLL);
70 PStoreCreateInstancePtr pPStoreCreateInstance =
71 (PStoreCreateInstancePtr) GetProcAddress(m_hPstoreDLL, "PStoreCreateInstance");
72 assert(pPStoreCreateInstance);
74 HRESULT hr = pPStoreCreateInstance(&m_spPStore, 0, 0, 0);
75 assert(SUCCEEDED(hr));
77 // parse the moniker
78 char *moniker = _moniker.edit();
79 const char *seps = ":";
80 WvString _key = strtok(moniker, seps);
81 WvString type_name = strtok(NULL, seps);
82 WvString _type_guid = strtok(NULL, seps);
83 WvString subtype_name = strtok(NULL, seps);
84 WvString _subtype_guid = strtok(NULL, seps);
86 if (!!_key && strcmp(_key, "PST_KEY_CURRENT_USER") == 0)
88 m_key = PST_KEY_CURRENT_USER;
90 else if (!!_key && strcmp(_key, "PST_KEY_LOCAL_MACHINE") == 0)
92 m_key = PST_KEY_LOCAL_MACHINE;
95 if ((m_key >= 0) && !!type_name && !!_type_guid && !!subtype_name && !!_subtype_guid)
97 HRESULT hr;
98 hr = UuidFromString((unsigned char*)_type_guid.edit(), &m_type);
99 hr = UuidFromString((unsigned char*)_subtype_guid.edit(), &m_subtype);
100 int result = create_types(type_name, subtype_name);
101 assert(SUCCEEDED( result ) || (result == PST_E_TYPE_EXISTS));
105 UniPStoreGen::~UniPStoreGen()
107 m_spPStore = 0;
108 if (m_hPstoreDLL)
110 FreeLibrary(m_hPstoreDLL);
111 m_hPstoreDLL = 0;
115 bool UniPStoreGen::isok()
117 return m_key >= 0;
121 WvString UniPStoreGen::get(const UniConfKey &key)
123 HRESULT hRes;
124 WvString value = WvString::null;
126 unsigned char *data;
127 unsigned long cbdata;
129 WvString _name = key.last().printable();
130 WCHAR name[MAX];
131 mbstowcs(name, _name.cstr(), MAX);
133 hRes = m_spPStore->ReadItem(
134 m_key,
135 &m_type,
136 &m_subtype,
137 name,
138 &cbdata,
139 &data,
140 NULL,
144 if (hRes == PST_E_OK)
146 value.setsize(MAX);
147 wcstombs(value.edit(), (wchar_t*)data, MAX);
148 CoTaskMemFree(data);
151 return value;
154 void UniPStoreGen::set(const UniConfKey &key, WvStringParm value)
156 WCHAR name[MAX], data[MAX];
157 mbstowcs(name, key.last().printable().cstr(), MAX);
158 mbstowcs(data, value.cstr(), MAX);
160 DWORD cbdata = DWORD((wcslen(data) + 1) * sizeof(WCHAR));
162 HRESULT hRes = m_spPStore->WriteItem(
163 m_key,
164 &m_type,
165 &m_subtype,
166 name,
167 cbdata,
168 (unsigned char *)data,
169 NULL,
170 PST_CF_NONE,
174 if (hRes == PST_E_OK)
176 delta(key, value);
181 void UniPStoreGen::setv(const UniConfPairList &pairs)
183 setv_naive(pairs);
187 bool UniPStoreGen::exists(const UniConfKey &key)
189 return false;
192 bool UniPStoreGen::haschildren(const UniConfKey &key)
194 return false;
197 UniConfGen::Iter *UniPStoreGen::iterator(const UniConfKey &key)
199 return new NullIter();
202 static IUniConfGen *creator(WvStringParm s, IObject*)
204 return new UniPStoreGen(s);
207 #pragma warning(disable : 4073)
208 #pragma init_seg(lib)
209 WvMoniker<IUniConfGen> UniPStoreGenMoniker("pstore", creator);