123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- #include "main.h"
- #include "./loaderIni.h"
- #include "./service.h"
- #include "./ifc_wasabihelper.h"
- #include "./ifc_omservicehost.h"
- #include "./ifc_omstorage.h"
- #include "./ifc_omstoragehandler.h"
- #include "./ifc_omstoragehandlerenum.h"
- #include <shlwapi.h>
- #include <strsafe.h>
- #define OMS_GROUP "OnlineService"
- #define OMS_ID "id"
- #define OMS_NAME "name"
- #define OMS_URL "url"
- #define OMS_ICON "icon"
- #define OMS_FLAGS "flags"
- #define OMS_RATING "rating"
- #define OMS_VERSION "version"
- #define OMS_DESCRIPTION "description"
- #define OMS_AUTHORFIRST "authorFirst"
- #define OMS_AUTHORLAST "authorLast"
- #define OMS_PUBLISHED "publishedDate"
- #define OMS_UPDATED "updatedDate"
- #define OMS_THUMBNAIL "thumbnail"
- #define OMS_SCREENSHOT "screenshot"
- #define OMS_GENERATION "generation"
- typedef HRESULT (ifc_omservice::*SVCSTRGET)(LPWSTR, UINT);
- typedef HRESULT (ifc_omservice::*SVCUINTGET)(UINT*);
- typedef HRESULT (ifc_omservicedetails::*DETAILGET)(LPWSTR, UINT);
- #define WRITE_OBJECT_STR(__mofifiedFlag, __class, __getterType, __object, __getter, __key)\
- {if (0 != (ifc_omserviceeditor::##__mofifiedFlag & modified)) {\
- HRESULT r = WriteObjectStr<__class, __getterType>((__key), (__object), &##__class::##__getter);\
- if (FAILED(r)) {hr = E_FAIL;} else { saved |= ifc_omserviceeditor::##__mofifiedFlag; }}}
- #define WRITE_OBJECT_STR_FALLBACK(__mofifiedFlag, __class, __getterType, __object, __getter1, __getter2, __key)\
- {if (0 != (ifc_omserviceeditor::##__mofifiedFlag & modified)) {\
- HRESULT r = WriteObjectStr<__class, __getterType>((__key), (__object), &##__class::##__getter1);\
- if (S_FALSE == r) r = WriteObjectStr<__class, __getterType>((__key), (__object), &##__class::##__getter2);\
- if (FAILED(r)) {hr = E_FAIL;} else { saved |= ifc_omserviceeditor::##__mofifiedFlag; }}}
- #define WRITE_OBJECT_UINT(__mofifiedFlag, __class, __getterType, __object, __getter, __key, __writeFlags)\
- {if (0 != (ifc_omserviceeditor::##__mofifiedFlag & modified)) {\
- HRESULT r = WriteObjectUInt<__class, __getterType>((__key), (__object), &##__class::##__getter, __writeFlags);\
- if (FAILED(r)) {hr = E_FAIL;} else { saved |= ifc_omserviceeditor::##__mofifiedFlag; }}}
- #define WRITE_SERVICE_STR(__mofifiedFlag, __getter, __key) WRITE_OBJECT_STR(__mofifiedFlag, ifc_omservice, SVCSTRGET, service, __getter, __key)
- #define WRITE_SERVICE_STR_FALLBACK(__mofifiedFlag, __getter1, __getter2, __key) WRITE_OBJECT_STR_FALLBACK(__mofifiedFlag, ifc_omservice, SVCSTRGET, service, __getter1, __getter2, __key)
- #define WRITE_SERVICE_UINT(__mofifiedFlag, __getter, __key, __writeZero) WRITE_OBJECT_UINT(__mofifiedFlag, ifc_omservice, SVCUINTGET, service, __getter, __key, __writeZero)
- #define WRITE_DETAILS_STR(__mofifiedFlag, __getter, __key) WRITE_OBJECT_STR(__mofifiedFlag, ifc_omservicedetails, DETAILGET, details, __getter, __key)
- LoaderIni::LoaderIni()
- : bufferAnsi(NULL), bufferAnsiMax(0), buffer(NULL), bufferMax(0), handlerEnum(NULL)
- {
- memset(pathAnsi, 0, sizeof(pathAnsi));
- }
- LoaderIni::~LoaderIni()
- {
- //Plugin_FreeAnsiString(pathAnsi);
- Plugin_FreeAnsiString(bufferAnsi);
- Plugin_FreeString(buffer);
- if (NULL != handlerEnum)
- handlerEnum->Release();
- }
- HRESULT LoaderIni::Load(LPCWSTR pszAddress, ifc_omservicehost *host, ifc_omservice **serviceOut)
- {
- if (NULL == serviceOut)
- return E_POINTER;
- *serviceOut = NULL;
- HRESULT hr = MakeAnsiPath(pszAddress);
- if (FAILED(hr)) return hr;
- UINT id = ReadInt(OMS_ID, 0);
- if (0 == id) return E_UNEXPECTED;
- OmService *service;
- hr = OmService::CreateInstance(id, host, &service);
- if (FAILED(hr)) return hr;
- ifc_omserviceeditor *editor;
- if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
- editor->BeginUpdate();
- else
- editor = NULL;
- service->SetAddress(pszAddress);
- #define READ_SERVICE_STR(__key, __putter)\
- { LPCSTR value; HRESULT r = ReadAnsi((__key), NULL, &value);\
- if (SUCCEEDED(r)) { if ('\0' != *value) r = service->##__putter((LPCWSTR)value, TRUE); }\
- if (FAILED(r)) hr = E_FAIL;}
- READ_SERVICE_STR(OMS_NAME, SetName);
- READ_SERVICE_STR(OMS_URL, SetUrl);
- READ_SERVICE_STR(OMS_ICON, SetIcon);
- UINT val = ReadInt(OMS_FLAGS, 0);
- if (0 != val) service->SetFlags(val, val & ~ifc_omservice::RuntimeFlagsMask);
- val = ReadInt(OMS_RATING, 0);
- if (0 != val) service->SetRating(val);
- val = ReadInt(OMS_VERSION, 0);
- if (0 != val) service->SetVersion(val);
- val = ReadInt(OMS_GENERATION, 0);
- if (0 != val) service->SetGeneration(val);
- READ_SERVICE_STR(OMS_DESCRIPTION, SetDescription);
- READ_SERVICE_STR(OMS_AUTHORFIRST, SetAuthorFirst);
- READ_SERVICE_STR(OMS_AUTHORLAST, SetAuthorLast);
- READ_SERVICE_STR(OMS_PUBLISHED, SetPublished);
- READ_SERVICE_STR(OMS_UPDATED, SetUpdated);
- READ_SERVICE_STR(OMS_THUMBNAIL, SetThumbnail);
- READ_SERVICE_STR(OMS_SCREENSHOT, SetScreenshot);
- if (NULL != handlerEnum)
- {
- handlerEnum->Reset();
- ifc_omstoragehandler *storageHandler;
- while (S_OK == handlerEnum->Next(1, &storageHandler, NULL))
- {
- LPCWSTR pszKey;
- if (SUCCEEDED(storageHandler->GetKey(&pszKey)))
- {
- if (SUCCEEDED(WideCharToAnsiBuffer(CP_UTF8, 0, pszKey, -1, NULL, NULL)))
- {
- LPCSTR keyAnsi = bufferAnsi;
- LPCSTR valueAnsi;
- if (SUCCEEDED(ReadAnsi(keyAnsi, NULL, &valueAnsi)) &&
- NULL != valueAnsi && L'\0' != *valueAnsi)
- {
- if (SUCCEEDED(MultiByteToBuffer(CP_UTF8, 0, valueAnsi, -1)))
- storageHandler->Invoke(service, pszKey, buffer);
- }
- }
- }
- storageHandler->Release();
- }
- }
- if (NULL != editor)
- {
- editor->SetModified(0, ((UINT)-1));
- editor->EndUpdate();
- editor->Release();
- }
- *serviceOut = service;
- return hr;
- }
- HRESULT LoaderIni::Reload(ifc_omservice *service)
- {
- if (NULL == service)
- return E_INVALIDARG;
- HRESULT hr = RequestBuffer(&buffer, &bufferMax, MAX_PATH * 2);
- if (FAILED(hr)) return hr;
- hr = service->GetAddress(buffer, bufferMax);
- if (FAILED(hr)) return hr;
- if (L'\0' == *buffer) return E_UNEXPECTED;
- hr = MakeAnsiPath(buffer);
- if (FAILED(hr)) return hr;
- UINT id = ReadInt(OMS_ID, 0);
- if (id != service->GetId()) return E_UNEXPECTED;
- ifc_omserviceeditor *editor;
- hr = service->QueryInterface(IFC_OmServiceEditor, (void**)&editor);
- if (SUCCEEDED(hr))
- {
- #define READ_EDITOR_STR(__key, __putter)\
- { LPCSTR value; HRESULT r = ReadAnsi((__key), NULL, &value);\
- if (SUCCEEDED(r)) { r = editor->##__putter((LPCWSTR)value, TRUE); }\
- if (FAILED(r)) hr = E_FAIL;}
- editor->BeginUpdate();
- READ_EDITOR_STR(OMS_NAME, SetName);
- READ_EDITOR_STR(OMS_URL, SetUrl);
- READ_EDITOR_STR(OMS_ICON, SetIcon);
- UINT val;
- val = ReadInt(OMS_FLAGS, 0);
- editor->SetFlags(val, val & ~ifc_omservice::RuntimeFlagsMask);
- val = ReadInt(OMS_RATING, 0);
- editor->SetRating(val);
- val = ReadInt(OMS_VERSION, 0);
- editor->SetVersion(val);
- val = ReadInt(OMS_GENERATION, 0);
- editor->SetGeneration(val);
- READ_EDITOR_STR(OMS_DESCRIPTION, SetDescription);
- READ_EDITOR_STR(OMS_AUTHORFIRST, SetAuthorFirst);
- READ_EDITOR_STR(OMS_AUTHORLAST, SetAuthorLast);
- READ_EDITOR_STR(OMS_PUBLISHED, SetPublished);
- READ_EDITOR_STR(OMS_UPDATED, SetUpdated);
- READ_EDITOR_STR(OMS_THUMBNAIL, SetThumbnail);
- READ_EDITOR_STR(OMS_SCREENSHOT, SetScreenshot);
- }
- else
- {
- editor = NULL;
- }
- if (NULL != handlerEnum)
- {
- handlerEnum->Reset();
- ifc_omstoragehandler *storageHandler;
- while (S_OK == handlerEnum->Next(1, &storageHandler, NULL))
- {
- LPCWSTR pszKey;
- if (SUCCEEDED(storageHandler->GetKey(&pszKey)))
- {
- if (SUCCEEDED(WideCharToAnsiBuffer(CP_UTF8, 0, pszKey, -1, NULL, NULL)))
- {
- LPCSTR keyAnsi = bufferAnsi;
- LPCSTR valueAnsi;
- if (SUCCEEDED(ReadAnsi(keyAnsi, NULL, &valueAnsi)) &&
- NULL != valueAnsi && L'\0' != *valueAnsi)
- {
- if (SUCCEEDED(MultiByteToBuffer(CP_UTF8, 0, valueAnsi, -1)))
- storageHandler->Invoke(service, pszKey, buffer);
- }
- }
- }
- storageHandler->Release();
- }
- }
- if (NULL != editor)
- {
- editor->EndUpdate();
- editor->Release();
- }
- return hr;
- }
- HRESULT LoaderIni::Save(ifc_omservice *service, UINT flags)
- {
- if(NULL == service)
- return E_INVALIDARG;
- HRESULT hr;
- BOOL generatedName;
- hr = RequestBuffer(&buffer, &bufferMax, MAX_PATH * 2);
- if (FAILED(hr)) return hr;
- hr = GetServicePath(service, buffer, bufferMax, &generatedName);
- if (FAILED(hr)) return hr;
- if (FALSE != generatedName)
- service->SetAddress(buffer);
- hr = MakeAnsiPath(buffer);
- if (FAILED(hr)) return hr;
- if (0 == (ifc_omstorage::saveModifiedOnly & flags))
- WritePrivateProfileSectionA(OMS_GROUP, NULL, pathAnsi);
- UINT modified = ((UINT)-1);
- UINT saved = 0;
- if (0 != (ifc_omstorage::saveModifiedOnly & flags))
- {
- ifc_omserviceeditor *editor;
- if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
- {
- editor->GetModified(&modified);
- editor->Release();
- if (0 == modified)
- return S_FALSE;
- }
- }
- if (FAILED(WriteUint(OMS_ID, service->GetId(), flagWriteZero)))
- return E_FAIL;
- WRITE_SERVICE_STR(modifiedName, GetName, OMS_NAME);
- WRITE_SERVICE_STR_FALLBACK(modifiedUrl, GetUrlDirect, GetUrl, OMS_URL);
- WRITE_SERVICE_STR(modifiedIcon, GetIcon, OMS_ICON);
- if (0 != (ifc_omserviceeditor::modifiedFlags & modified))
- {
- UINT val;
- if (SUCCEEDED(service->GetFlags(&val)) && SUCCEEDED(WriteUint(OMS_FLAGS, val & ~ifc_omservice::RuntimeFlagsMask, flagHexMode)))
- saved |= ifc_omserviceeditor::modifiedFlags;
- }
- WRITE_SERVICE_UINT(modifiedRating, GetRating, OMS_RATING, flagWriteNormal);
- WRITE_SERVICE_UINT(modifiedVersion, GetVersion, OMS_VERSION, flagWriteNormal);
- WRITE_SERVICE_UINT(modifiedGeneration, GetGeneration, OMS_GENERATION, flagWriteNormal);
- ifc_omservicedetails *details;
- if (SUCCEEDED(service->QueryInterface(IFC_OmServiceDetails, (void**)&details)))
- {
- WRITE_DETAILS_STR(modifiedDescription, GetDescription, OMS_DESCRIPTION);
- WRITE_DETAILS_STR(modifiedAuthorFirst, GetAuthorFirst, OMS_AUTHORFIRST);
- WRITE_DETAILS_STR(modifiedAuthorLast, GetAuthorLast, OMS_AUTHORLAST);
- WRITE_DETAILS_STR(modifiedPublished, GetPublished, OMS_PUBLISHED);
- WRITE_DETAILS_STR(modifiedUpdated, GetUpdated, OMS_UPDATED);
- WRITE_DETAILS_STR(modifiedThumbnail, GetThumbnail, OMS_THUMBNAIL);
- WRITE_DETAILS_STR(modifiedScreenshot, GetScreenshot, OMS_SCREENSHOT);
- details->Release();
- }
- if (0 != (ifc_omstorage::saveClearModified & flags) && 0 != saved)
- {
- ifc_omserviceeditor *editor;
- if (SUCCEEDED(service->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
- {
- editor->SetModified(0, saved);
- editor->Release();
- }
- }
- return hr;
- }
- HRESULT LoaderIni::RegisterHandlers(ifc_omstoragehandlerenum *enumerator)
- {
- if (NULL != handlerEnum)
- handlerEnum->Release();
- handlerEnum = enumerator;
- if (NULL != handlerEnum)
- handlerEnum->AddRef();
- return S_OK;
- }
- HRESULT LoaderIni::RequestAnsiBuffer(LPSTR *ppBuffer, UINT *pBufferMax, UINT requestSize)
- {
- if (*pBufferMax >= requestSize)
- return S_OK;
- UINT size = (0 != *pBufferMax) ? (*pBufferMax * 2) : 128;
- while (size < requestSize) size = size*2;
- Plugin_FreeAnsiString(*ppBuffer);
- *ppBuffer = Plugin_MallocAnsiString(size);
- if (NULL == *ppBuffer)
- {
- *ppBuffer = 0;
- *pBufferMax = 0;
- return E_OUTOFMEMORY;
- }
- *pBufferMax = size;
- return S_OK;
- }
- HRESULT LoaderIni::RequestBuffer(LPWSTR *ppBuffer, UINT *pBufferMax, UINT requestSize)
- {
- if (*pBufferMax >= requestSize)
- return S_OK;
- UINT size = (0 != *pBufferMax) ? (*pBufferMax * 2) : 128;
- while (size < requestSize) size = size*2;
- Plugin_FreeString(*ppBuffer);
- *ppBuffer = Plugin_MallocString(size);
- if (NULL == *ppBuffer)
- {
- *ppBuffer = 0;
- *pBufferMax = 0;
- return E_OUTOFMEMORY;
- }
- *pBufferMax = size;
- return S_OK;
- }
- HRESULT LoaderIni::MakeAnsiPath(LPCWSTR pszAddress)
- {
- BOOL fUsedDefaultChar = FALSE;
- WCHAR szShort[MAX_PATH] = {0};
- UINT cch = WideCharToMultiByte(CP_ACP, 0, pszAddress, -1, NULL, 0, NULL, &fUsedDefaultChar);
- if (0 == cch || FALSE != fUsedDefaultChar)
- {
- cch = GetShortPathName(pszAddress, szShort, ARRAYSIZE(szShort));
- if (0 != cch)
- {
- pszAddress = szShort;
- cch = WideCharToMultiByte(CP_ACP, 0, pszAddress, -1, NULL, 0, NULL, &fUsedDefaultChar);
- }
- if (0 == cch || FALSE != fUsedDefaultChar)
- {
- if (NULL != pathAnsi) *pathAnsi = L'\0';
- return E_FAIL;
- }
- }
- cch = WideCharToMultiByte(CP_ACP, 0, pszAddress, cch, pathAnsi, MAX_PATH, NULL, NULL);
- if (0 == cch)
- {
- DWORD errorCode = GetLastError();
- *pathAnsi = L'\0';
- return HRESULT_FROM_WIN32(errorCode);
- }
- pathAnsi[cch] = '\0';
- return S_OK;
- }
- HRESULT LoaderIni::GetServicePath(ifc_omservice *service, LPWSTR pszBuffer, UINT cchBufferMax, BOOL *fGenerated)
- {
- if (NULL == pszBuffer) return E_POINTER;
- pszBuffer[0] = L'\0';
- if (NULL == service) return E_INVALIDARG;
- HRESULT hr;
- WCHAR szPath[MAX_PATH * 2] = {0};
- hr = service->GetAddress(szPath, ARRAYSIZE(szPath));
- if (FAILED(hr)) return hr;
- ifc_omservicehost *host = NULL;
- ifc_omservicehostext *hostExt;
- if (SUCCEEDED(service->QueryInterface(IFC_OmServiceHostExt, (void**)&hostExt)))
- {
- if (FAILED(hostExt->GetHost(&host)))
- host = NULL;
- hostExt->Release();
- }
- BOOL nameGenerated = FALSE;
- if (L'\0' == *szPath)
- {
- hr = (NULL != host) ?
- host->GetDefaultName(service, szPath, ARRAYSIZE(szPath)) :
- StringCchPrintf(szPath, ARRAYSIZE(szPath), L"omService_{%010u}", service->GetId());
- if (L'\0' == *szPath)
- hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
- nameGenerated = TRUE;
- }
- if (SUCCEEDED(hr))
- {
- hr = Plugin_ResolveRelativePath(szPath, host, pszBuffer, cchBufferMax);
- if (SUCCEEDED(hr) && FALSE != nameGenerated)
- {
- LPWSTR pExt = PathFindExtension(pszBuffer);
- if (pExt != pszBuffer && L'.' == *pExt && SUCCEEDED(StringCchCopy(szPath, ARRAYSIZE(szPath), pExt)))
- {
- UINT pExtMax = cchBufferMax - (UINT)(pExt - pszBuffer);
- UINT attempt = 0;
- while (FALSE != PathFileExists(pszBuffer))
- {
- if (FAILED(StringCchPrintf(pExt, pExtMax, L"(%u)%s", ++attempt, szPath)))
- {
- StringCchCopy(pExt, pExtMax, szPath);
- break;
- }
- }
- }
- }
- if (SUCCEEDED(hr) && SUCCEEDED(StringCchCopy(szPath, ARRAYSIZE(szPath), pszBuffer)))
- {
- PathRemoveFileSpec(szPath);
- Plugin_EnsurePathExist(szPath);
- }
- }
- if (FAILED(hr))
- pszBuffer[0] = L'\0';
- if (NULL != host)
- host->Release();
- if (NULL != fGenerated)
- *fGenerated = nameGenerated;
-
- return hr;
- }
- HRESULT LoaderIni::WideCharToAnsiBuffer(UINT codePage, DWORD flags, LPCWSTR pszWideChar, INT cchWideChar, LPCSTR pDefaultChar, BOOL *pUsedDefaultChar)
- {
- UINT cch = WideCharToMultiByte(codePage, flags, pszWideChar, cchWideChar, NULL, 0, pDefaultChar, pUsedDefaultChar);
- if (0 == cch)
- {
- DWORD errorCode = GetLastError();
- return HRESULT_FROM_WIN32(errorCode);
- }
- HRESULT hr = RequestAnsiBuffer(&bufferAnsi, &bufferAnsiMax, cch);
- if (FAILED(hr)) return hr;
- if (0 == WideCharToMultiByte(codePage, flags, pszWideChar, cchWideChar, bufferAnsi, bufferAnsiMax, pDefaultChar, pUsedDefaultChar))
- {
- DWORD errorCode = GetLastError();
- return HRESULT_FROM_WIN32(errorCode);
- }
- return S_OK;
- }
- HRESULT LoaderIni::MultiByteToBuffer(UINT codePage, DWORD flags, LPCSTR pszMultiByte, INT cbMultiByte)
- {
- UINT cch = MultiByteToWideChar(codePage, flags, pszMultiByte, cbMultiByte, NULL, 0);
- if (0 == cch)
- {
- DWORD errorCode = GetLastError();
- return HRESULT_FROM_WIN32(errorCode);
- }
- HRESULT hr = RequestBuffer(&buffer, &bufferMax, cch);
- if (FAILED(hr)) return hr;
- if (0 == MultiByteToWideChar(codePage, flags, pszMultiByte, cbMultiByte, buffer, bufferMax))
- {
- DWORD errorCode = GetLastError();
- return HRESULT_FROM_WIN32(errorCode);
- }
- return S_OK;
- }
- HRESULT LoaderIni::Write(LPCSTR pszKey, LPCWSTR pszValue)
- {
- LPCSTR valueAnsi;
- if (NULL == pszValue) valueAnsi = NULL;
- else if (L'\0' == *pszValue) valueAnsi = '\0';
- else
- {
- HRESULT hr = WideCharToAnsiBuffer(CP_UTF8, 0, pszValue, -1, NULL, NULL);
- if (FAILED(hr)) return hr;
- valueAnsi = bufferAnsi;
- }
- return WriteAnsi(pszKey, valueAnsi);
- }
- HRESULT LoaderIni::WriteAnsi(LPCSTR pszKey, LPCSTR pszValue)
- {
- if (FALSE == WritePrivateProfileStringA(OMS_GROUP, pszKey, pszValue, pathAnsi))
- {
- DWORD errorCode = GetLastError();
- return HRESULT_FROM_WIN32(errorCode);
- }
- return S_OK;
- }
- HRESULT LoaderIni::WriteUint(LPCSTR pszKey, UINT uValue, UINT flags)
- {
- HRESULT hr;
- CHAR szBuffer[16] = {0}, *pVal;
-
- if (0 == uValue && 0 == (flagWriteZero & flags))
- {
- hr = S_OK;
- pVal = NULL;
- }
- else
- {
- hr = StringCchPrintfA(szBuffer, ARRAYSIZE(szBuffer), ((0 == (flagHexMode & flags)) ? "%u" : "0x%08X"), uValue);
- pVal = szBuffer;
- }
-
- if (SUCCEEDED(hr))
- {
- hr = WriteAnsi(pszKey, pVal);
- }
- return hr;
- }
- HRESULT LoaderIni::ReadAnsi(LPCSTR pszKey, LPCSTR pszDefault, LPCSTR *ppszValue)
- {
- HRESULT hr;
- if (NULL == bufferAnsi)
- {
- hr = RequestAnsiBuffer(&bufferAnsi, &bufferAnsiMax, 2);
- if (FAILED(hr))
- {
- *ppszValue = pszDefault;
- return hr;
- }
- }
- for(;;)
- {
- UINT copied = GetPrivateProfileStringA(OMS_GROUP, pszKey, pszDefault, bufferAnsi, bufferAnsiMax, pathAnsi);
- if (copied != bufferAnsiMax - 1) break;
-
- hr = RequestAnsiBuffer(&bufferAnsi, &bufferAnsiMax, bufferAnsiMax * 2);
- if (FAILED(hr))
- {
- *ppszValue = pszDefault;
- return hr;
- }
- }
- *ppszValue = bufferAnsi;
- return S_OK;
- }
- UINT LoaderIni::ReadInt(LPCSTR pszKey, UINT nDefault)
- {
- return GetPrivateProfileIntA(OMS_GROUP, pszKey, nDefault, pathAnsi);
- }
- template <class Object, class Getter>
- HRESULT LoaderIni::WriteObjectStr(LPCSTR pszKey, Object *object, Getter getter)
- {
- HRESULT hr;
- if (NULL == buffer)
- {
- hr = RequestBuffer(&buffer, &bufferMax, 256);
- if (FAILED(hr)) return hr;
- }
- for (;;)
- {
- hr = (object->*getter)(buffer, bufferMax);
- if (SUCCEEDED(hr)) break;
- if (E_NOTIMPL == hr) return S_FALSE;
- if (OMSVC_E_INSUFFICIENT_BUFFER != hr) return hr;
- hr = RequestBuffer(&buffer, &bufferMax, bufferMax * 2);
- if (FAILED(hr)) return hr;
- }
- return Write(pszKey, buffer);
- }
- template <class Object, class Getter>
- HRESULT LoaderIni::WriteObjectUInt(LPCSTR pszKey, Object *object, Getter getter, UINT flags)
- {
- UINT val;
- HRESULT hr = (object->*getter)(&val);
- if (FAILED(hr))
- {
- return (E_NOTIMPL == hr) ? S_FALSE : hr;
- }
- return WriteUint(pszKey, val, flags);
- }
|