hxapihelp.cpp
来自「symbian 下的helix player源代码」· C++ 代码 · 共 498 行
CPP
498 行
/************************************************************************
* hxsym_hxapihelp.cpp
* -------------
*
* Synopsis:
* helpers to simplify working with helix api interfaces
*
*
* (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
*
************************************************************************/
#include "hxtypes.h"
#include "hxcom.h"
#include "hxresult.h"
#include "hxcomm.h" //IHXBuffer
#include "ihxpckts.h" //IHXValues
#include "hxmon.h" //IHXRegistry
#include "hxprefs.h" //IHXPreferences
#include "chxmapstringtostring.h"
#include "comptr_traits.h"
#include "comptr.h"
#include "chxavmisc.h"
#include "hxsym_debug.h"
#include "hxapihelp.h"
//////////////////////////////////
// read property name into a string object
bool reg::GetPropName(IHXRegistry* pReg, UINT32 idKey, CHXString& strName)
{
HX_ASSERT(pReg);
comptr<IHXBuffer> buffer;
HX_RESULT hr = pReg->GetPropName(idKey, buffer.AsRef());
if( SUCCEEDED(hr) )
{
strName = reinterpret_cast<const char*>(buffer->GetBuffer());
}
return SUCCEEDED(hr);
}
//////////////////////////////////
// read registry base.key into a string object
bool reg::GetString(IHXRegistry* pReg, const char* pszBase, const char* pszKey, CHXString& strValue, bool bIsBuffer)
{
HX_ASSERT(pReg);
HX_ASSERT(pszBase);
HX_ASSERT(pszKey);
comptr<IHXBuffer> buffer;
CHXString strFullKey(pszBase);
strFullKey += '.';
strFullKey += pszKey;
HX_RESULT hr;
if( bIsBuffer )
{
hr = pReg->GetBufByName(static_cast<const char*>(strFullKey), buffer.AsRef());
}
else
{
hr = pReg->GetStrByName(static_cast<const char*>(strFullKey), buffer.AsRef());
}
if( SUCCEEDED(hr) )
{
strValue = reinterpret_cast<const char*>(buffer->GetBuffer());
}
return SUCCEEDED(hr);
}
//
// Add string as string property; make readable as both string and buffer property
//
HX_RESULT reg::AddString(IHXRegistry* pReg,
IHXCommonClassFactory* pFactory,
const char* pszBaseKey,
const char* pszKey,
const char* pszVal)
{
HX_ASSERT_VALID_PTR(pReg);
HX_ASSERT_VALID_PTR(pFactory);
HX_ASSERT_VALID_PTR(pszKey);
HX_ASSERT_VALID_PTR(pszBaseKey);
HX_ASSERT_VALID_PTR(pszVal);
CHXString strKey;
strKey.Format("%s.%s", pszBaseKey, pszKey);
comptr<IHXBuffer> buffer;
HX_RESULT hr = buf::Create(pFactory, pszVal, buffer.AsRef());
if( FAILED(hr) )
{
return HR_CHECK(hr);
}
comptr<IHXRegistryAltStringHandling> setter;
if(setter.From(pReg))
{
UINT32 id = pReg->AddStr(strKey, buffer);
if( id )
{
setter->SetStringAccessAsBufferById( id );
hr = HXR_OK;
}
}
else
{
hr = pReg->AddBuf(strKey, buffer); // older server
}
return HR_CHECK(hr);
}
////////////////////////////
//
// scan through psz, verifying that second string comprises a valid
// key prefix of the first (case insensitive) if it does, return
// new position after prefix; else return NULL
//
// example:
//
// ScanPastKeyPrefix("foo.bar.woo", "foo")
//
// returns pointer to "bar.woo"
//
// ScanPastKeyPrefix("foo.bar.woo", "baz")
//
// returns NULL pointer
//
const char* reg::ScanPastKeyPrefix( const char* psz, const char* pszPrefix)
{
HX_ASSERT_VALID_PTR(pszPrefix);
HX_ASSERT_VALID_PTR(psz);
UINT32 idx = 0;
for( ; pszPrefix[idx]; ++idx )
{
//
// scan through part of key that should match the key
// for our reg entry; do lower case comparison.
//
char ch = psz[idx];
if( 'A' <= ch && ch <= 'Z' )
{
ch -= 'A' - 'a';
}
char chPrefix = pszPrefix[idx];
if( 'A' <= chPrefix && chPrefix <= 'Z' )
{
chPrefix -= 'A' - 'a';
}
if( ch != chPrefix )
{
// bad key
// PN_TRACE("string '%s' doesn't begin with prefix '%s'\n", psz, pszPrefix);
return NULL;
}
}
if( psz[idx++] != '.' )
{
// should have ended on a dot
// PN_TRACE("string '%s': string '%s' is not a valid key prefix\n", psz, pszPrefix);
return NULL;
}
return psz + idx;
}
////////////////////////////
//
// Scan through registry key nodes; append strExtracted with key fragement we passed
//
// return new position ( null terminator if last key)
//
// example:
//
// ScanKeyNodes("foo.bar.woo", 2, str)
//
// yields "foo.bar" in str, returns pointer to "woo"
//
const char* reg::ScanKeyNodes( const char* psz, UINT32 nNodeCount, CHXString& strExtracted )
{
HX_ASSERT_VALID_PTR(psz);
UINT32 idx = 0;
while( nNodeCount-- )
{
// collect to dot or null
for( ; psz[idx] && psz[idx] != '.'; ++idx )
{
strExtracted += psz[idx];
}
if( psz[idx] )
{
if( nNodeCount)
{
// add the dot since we're not finished
strExtracted += psz[idx];
}
++idx;
}
else
{
break;
}
}
return psz + idx;
}
HX_RESULT buf::Create(IHXCommonClassFactory* pFactory, const char* psz, IHXBuffer*& pbuffNew)
{
HX_ASSERT_VALID_PTR(pFactory);
HX_ASSERT_VALID_PTR(psz);
HX_RESULT hr = pFactory->CreateInstance(IID_IHXBuffer, reinterpret_cast<void**>(&pbuffNew));
if(SUCCEEDED(hr))
{
hr = pbuffNew->Set( reinterpret_cast<const BYTE*>(psz), strlen(psz) + 1);
if(FAILED(hr))
{
// can't use buffer
HX_RELEASE(pbuffNew);
}
}
return hr;
}
//////////////////////////////////////////
//
// Get a bool from the IHXValues object, stored as a UINT32. Bool var
// passed in is unmodified if key does not exist (or is of wrong type),
// so you can pass in a default.
//
bool val::GetBool(IHXValues* pval, const char* pszKey, bool& b)
{
HX_ASSERT_VALID_PTR(pval);
UINT32 n;
HX_RESULT hr = pval->GetPropertyULONG32(pszKey, n);
if( SUCCEEDED(hr) )
{
b = (n != 0);
}
return SUCCEEDED(hr);
}
//////////////////////////////////////////
//
// Get a CHXString from the IHXValues object. String is unmodified if
// key does not exist (or is of wrong type), so you can pass in a default.
//
bool val::GetString(IHXValues* pval, const char* pszKey, CHXString& strVal, ValType type)
{
HX_ASSERT_VALID_PTR(pval);
comptr<IHXBuffer> buffer;
HX_RESULT (STDMETHODCALLTYPE IHXValues::*pfnGetProp)(const char*, REF(IHXBuffer*)) =
(type == string) ? &IHXValues::GetPropertyCString : &IHXValues::GetPropertyBuffer;
HX_RESULT hr = (pval->*pfnGetProp)(pszKey, buffer.AsRef());
if( SUCCEEDED(hr) )
{
strVal = reinterpret_cast<const char*>(buffer->GetBuffer());
strVal.TrimLeft();
strVal.TrimRight();
}
return SUCCEEDED(hr);
}
//////////////////////////////////////////
//
bool val::GetUINT32(IHXValues* pval, const char* pszKey, UINT32& out)
{
HX_ASSERT_VALID_PTR(pval);
ULONG32 n;
HX_RESULT hr = pval->GetPropertyULONG32(pszKey, n);
if( SUCCEEDED(hr) )
{
out = n;
return true;
}
return false;
}
//////////////////////////////////////////
//
// gets UINT16, checking that value is in range
//
bool val::GetUINT16(IHXValues* pval, const char* pszKey, UINT16& out)
{
UINT32 n;
HX_RESULT hr = GetUINT32(pval, pszKey, n);
if( SUCCEEDED(hr) )
{
HX_ASSERT(n <= 0xffff);
if( n <= 0xffff )
{
// good - in range
out = UINT16(n);
}
else
{
hr = HXR_FAIL;
}
}
return SUCCEEDED(hr);
}
void val::GetStringProps(IHXValues* pval, ValType type, CHXMapStringToString& props)
{
typedef HX_RESULT (STDMETHODCALLTYPE IHXValues::*PFN_GetFirstProp)(const char*&, IHXBuffer*&);
typedef HX_RESULT (STDMETHODCALLTYPE IHXValues::*PFN_GetNextProp)(const char*&, IHXBuffer*&);
PFN_GetFirstProp pfnGetFirst;
PFN_GetNextProp pfnGetNext;
switch( type )
{
case buffer:
pfnGetFirst = &IHXValues::GetFirstPropertyBuffer;
pfnGetNext = &IHXValues::GetNextPropertyBuffer;
break;
case string:
pfnGetFirst = &IHXValues::GetFirstPropertyCString;
pfnGetNext = &IHXValues::GetNextPropertyCString;
break;
default:
HX_ASSERT(false);
}
const char* pszPropName = NULL;
comptr<IHXBuffer> buffer;
HX_RESULT hr = (pval->*pfnGetFirst)(pszPropName, buffer.AsRef());
while( SUCCEEDED(hr) )
{
props[ CHXString(pszPropName) ] = CHXString(reinterpret_cast<const char*>(buffer->GetBuffer()));
buffer.Reset();
hr = (pval->*pfnGetNext)(pszPropName, buffer.AsRef());
}
}
void prefs::Write(IHXCommonClassFactory* pFactory, IHXPreferences* pPrefs, const char* pKey, const char* pszVal)
{
HX_ASSERT(pFactory);
HX_ASSERT(pPrefs);
comptr<IHXBuffer> buffer;
buf::Create(pFactory, pszVal, buffer.AsRef());
pPrefs->WritePref(pKey, buffer);
}
void prefs::Write(IHXCommonClassFactory* pFactory, IHXPreferences* pPrefs, const char* pKey, UINT32 val)
{
HX_ASSERT(pFactory);
HX_ASSERT(pPrefs);
comptr<IHXBuffer> buffer;
CHXString strVal;
strVal.Format("%lu", val);
buf::Create(pFactory, strVal, buffer.AsRef());
pPrefs->WritePref(pKey, buffer);
}
//////////////////////////////////////////////
// these all return 'true' if pref exists (in which
// case out param is set to the retrieved value)
//
bool prefs::LookupString(IHXPreferences* pPrefs, const char* pKey, CHXString& strOut)
{
comptr<IHXBuffer> buffer;
HX_RESULT hr = pPrefs->ReadPref(pKey, buffer.AsRef());
if( SUCCEEDED(hr) )
{
strOut = reinterpret_cast<const char*>(buffer->GetBuffer());
return true;
}
return false;
}
bool prefs::LookupBool(IHXPreferences* pPrefs, const char* pKey, bool& bValOut)
{
CHXString str;
if( LookupString(pPrefs, pKey, str) )
{
if (!str.IsEmpty())
{
bValOut = (atoi(str) != 0);
}
else
{
// define "key=" (no val) as "key=0"
bValOut = false;
}
return true;
}
return false;
}
bool prefs::LookupUINT32(IHXPreferences* pPrefs, const char* pKey, UINT32& valOut)
{
CHXString str;
if(LookupString(pPrefs, pKey, str))
{
if (!str.IsEmpty())
{
valOut = strtoul(str, 0, 10);
}
else
{
// define "key=" (no val) as "key=0"
valOut = 0;
}
return true;
}
return false;
}
bool prefs::LookupINT32(IHXPreferences* pPrefs, const char* pKey, INT32& valOut)
{
CHXString str;
if(LookupString(pPrefs, pKey, str))
{
if (!str.IsEmpty())
{
valOut = atoi(str);
}
else
{
// define "key=" (no val) as "key=0"
valOut = 0;
}
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////
// these all return value from prefs, or default value if not found
//
CHXString prefs::GetString(IHXPreferences* pPrefs, const char* pKey, const char* pszDefault)
{
CHXString str;
if(!LookupString(pPrefs, pKey, str))
{
str = pszDefault;
}
return str;
}
bool prefs::GetBool(IHXPreferences* pPrefs, const char* pKey, bool bDefaultValue)
{
bool bVal;
if(!LookupBool(pPrefs, pKey, bVal))
{
bVal = bDefaultValue;
}
return bVal;
}
UINT32 prefs::GetUINT32(IHXPreferences* pPrefs, const char* pKey, UINT32 defaultValue)
{
UINT32 val;
if(!LookupUINT32(pPrefs, pKey, val))
{
val = defaultValue;
}
return val;
}
INT32 prefs::GetINT32(IHXPreferences* pPrefs, const char* pKey, INT32 defaultValue)
{
INT32 val;
if(!LookupINT32(pPrefs, pKey, val))
{
val = defaultValue;
}
return val;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?