Started factoring nsIPersistentProperties from nsIProperties.

This commit is contained in:
warren%netscape.com 1999-04-22 07:32:51 +00:00
Родитель 154f8381b4
Коммит 1ea549d80d
11 изменённых файлов: 563 добавлений и 97 удалений

Просмотреть файл

@ -19,30 +19,89 @@
#ifndef nsIProperties_h___
#define nsIProperties_h___
#include "nsISupports.h"
#define NS_IPROPERTIES_IID \
{ /* f42bc870-dc17-11d2-9311-00e09805570f */ \
0xf42bc870, \
0xdc17, \
0x11d2, \
{0x93, 0x11, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
}
class nsIProperties : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IPROPERTIES_IID; return iid; }
/**
* Defines a new property.
* @return NS_ERROR_FAILURE if a property is already defined.
*/
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue) = 0;
/**
* Undefines a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD UndefineProperty(const char* prop) = 0;
/**
* Gets a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result) = 0;
/**
* Sets a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD SetProperty(const char* prop, nsISupports* value) = 0;
/**
* @return NS_OK if the property exists with the specified value
* @return NS_COMFALSE if the property does not exist, or doesn't have
* the specified value (values are compared with ==)
*/
NS_IMETHOD HasProperty(const char* prop, nsISupports* value) = 0;
};
// Returns a default implementation of an nsIProperties object.
extern nsresult
NS_NewIProperties(nsIProperties* *result);
////////////////////////////////////////////////////////////////////////////////
#include "nsID.h"
#include "nsIInputStream.h"
#include "nsIOutputStream.h"
#include "nsISupports.h"
#include "nsString.h"
// {1A180F60-93B2-11d2-9B8B-00805F8A16D9}
#define NS_IPROPERTIES_IID \
#define NS_IPERSISTENTPROPERTIES_IID \
{ 0x1a180f60, 0x93b2, 0x11d2, \
{ 0x9b, 0x8b, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9 } }
// {2245E573-9464-11d2-9B8B-00805F8A16D9}
NS_DECLARE_ID(kPropertiesCID,
NS_DECLARE_ID(kPersistentPropertiesCID,
0x2245e573, 0x9464, 0x11d2, 0x9b, 0x8b, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9);
class nsIProperties : public nsISupports
class nsIPersistentProperties : public nsIProperties
{
public:
static const nsIID& GetIID() { static nsIID iid = NS_IPERSISTENTPROPERTIES_IID; return iid; }
NS_IMETHOD Load(nsIInputStream* aIn) = 0;
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader) = 0;
NS_IMETHOD Subclass(nsIPersistentProperties* aSubclass) = 0;
// XXX these 2 methods will be subsumed by the ones from
// nsIProperties once we figure this all out
NS_IMETHOD GetProperty(const nsString& aKey, nsString& aValue) = 0;
NS_IMETHOD SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue) = 0;
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader) = 0;
NS_IMETHOD Subclass(nsIProperties* aSubclass) = 0;
};
#endif /* nsIProperties_h___ */
////////////////////////////////////////////////////////////////////////////////
#endif // nsIProperties_h___

Просмотреть файл

@ -53,7 +53,7 @@ NSRegisterSelf(nsISupports* aServMgr, const char* path)
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kPropertiesCID, NULL, NULL,
rv = compMgr->RegisterComponent(kPersistentPropertiesCID, NULL, NULL,
path, PR_TRUE, PR_TRUE);
rv = compMgr->RegisterComponent(kObserverServiceCID,
@ -84,7 +84,7 @@ NSUnregisterSelf(nsISupports* aServMgr, const char* path)
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterFactory(kPropertiesCID, path);
rv = compMgr->UnregisterFactory(kPersistentPropertiesCID, path);
rv = compMgr->UnregisterFactory(kObserverServiceCID, path);
@ -108,8 +108,8 @@ NSGetFactory(nsISupports* aServMgr,
return NS_ERROR_NULL_POINTER;
}
if (aClass.Equals(kPropertiesCID)) {
nsPropertiesFactory *propsFactory = new nsPropertiesFactory();
if (aClass.Equals(kPersistentPropertiesCID)) {
nsPersistentPropertiesFactory *propsFactory = new nsPersistentPropertiesFactory();
if (!propsFactory) {
return NS_ERROR_OUT_OF_MEMORY;
}

Просмотреть файл

@ -17,6 +17,130 @@
*/
#define NS_IMPL_IDS
#include "nsIProperties.h"
#include "nsHashtable.h"
////////////////////////////////////////////////////////////////////////////////
class nsProperties : public nsIProperties, public nsHashtable {
public:
NS_DECL_ISUPPORTS
// nsIProperties methods:
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue);
NS_IMETHOD UndefineProperty(const char* prop);
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result);
NS_IMETHOD SetProperty(const char* prop, nsISupports* value);
NS_IMETHOD HasProperty(const char* prop, nsISupports* value);
// nsProperties methods:
nsProperties();
virtual ~nsProperties();
static PRBool ReleaseValues(nsHashKey* key, void* data, void* closure);
};
NS_IMPL_ISUPPORTS(nsProperties, nsIProperties::GetIID());
nsProperties::nsProperties()
{
}
PRBool
nsProperties::ReleaseValues(nsHashKey* key, void* data, void* closure)
{
nsISupports* value = (nsISupports*)data;
NS_IF_RELEASE(value);
return PR_TRUE;
}
nsProperties::~nsProperties()
{
Enumerate(ReleaseValues);
}
NS_IMETHODIMP
nsProperties::DefineProperty(const char* prop, nsISupports* initialValue)
{
nsCStringKey key(prop);
if (Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Put(&key, initialValue);
NS_ASSERTION(prevValue == NULL, "hashtable error");
NS_IF_ADDREF(initialValue);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::UndefineProperty(const char* prop)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Remove(&key);
NS_IF_RELEASE(prevValue);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::GetProperty(const char* prop, nsISupports* *result)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* value = (nsISupports*)Get(&key);
NS_IF_ADDREF(value);
*result = value;
return NS_OK;
}
NS_IMETHODIMP
nsProperties::SetProperty(const char* prop, nsISupports* value)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Put(&key, value);
NS_IF_RELEASE(prevValue);
NS_IF_ADDREF(value);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::HasProperty(const char* prop, nsISupports* expectedValue)
{
nsISupports* value;
nsresult rv = GetProperty(prop, &value);
if (NS_FAILED(rv)) return NS_COMFALSE;
rv = (value == expectedValue) ? NS_OK : NS_COMFALSE;
NS_IF_RELEASE(value);
return rv;
}
////////////////////////////////////////////////////////////////////////////////
nsresult
NS_NewIProperties(nsIProperties* *result)
{
nsProperties* props = new nsProperties();
if (props == NULL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(props);
*result = props;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// Persistent Properties (should go in a separate file)
////////////////////////////////////////////////////////////////////////////////
#include "nsID.h"
#include "nsBaseDLL.h"
@ -28,36 +152,48 @@
#include "plhash.h"
#include "pratom.h"
class nsProperties : public nsIProperties
class nsPersistentProperties : public nsIPersistentProperties
{
public:
nsProperties();
virtual ~nsProperties();
nsPersistentProperties();
virtual ~nsPersistentProperties();
NS_DECL_ISUPPORTS
// nsIProperties methods:
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue);
NS_IMETHOD UndefineProperty(const char* prop);
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result);
NS_IMETHOD SetProperty(const char* prop, nsISupports* value);
NS_IMETHOD HasProperty(const char* prop, nsISupports* value);
// nsIPersistentProperties methods:
NS_IMETHOD Load(nsIInputStream* aIn);
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader);
NS_IMETHOD Subclass(nsIPersistentProperties* aSubclass);
// XXX these 2 methods will be subsumed by the ones from
// nsIProperties once we figure this all out
NS_IMETHOD GetProperty(const nsString& aKey, nsString& aValue);
NS_IMETHOD SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue);
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader);
NS_IMETHOD Subclass(nsIProperties* aSubclass);
// nsPersistentProperties methods:
PRInt32 Read();
PRInt32 SkipLine(PRInt32 c);
PRInt32 SkipWhiteSpace(PRInt32 c);
nsIUnicharInputStream* mIn;
nsIProperties* mSubclass;
nsIPersistentProperties* mSubclass;
struct PLHashTable* mTable;
};
nsProperties::nsProperties()
nsPersistentProperties::nsPersistentProperties()
{
NS_INIT_REFCNT();
mIn = nsnull;
mSubclass = NS_STATIC_CAST(nsIProperties*, this);
mSubclass = NS_STATIC_CAST(nsIPersistentProperties*, this);
mTable = nsnull;
}
@ -69,7 +205,7 @@ FreeHashEntries(PLHashEntry* he, PRIntn i, void* arg)
return HT_ENUMERATE_REMOVE;
}
nsProperties::~nsProperties()
nsPersistentProperties::~nsPersistentProperties()
{
if (mTable) {
// Free the PRUnicode* pointers contained in the hash table entries
@ -81,17 +217,19 @@ nsProperties::~nsProperties()
NS_DEFINE_IID(kIPropertiesIID, NS_IPROPERTIES_IID);
NS_IMPL_ISUPPORTS(nsProperties, kIPropertiesIID)
NS_IMPL_ISUPPORTS(nsPersistentProperties, kIPropertiesIID)
NS_IMETHODIMP
nsProperties::Load(nsIInputStream *aIn)
nsPersistentProperties::Load(nsIInputStream *aIn)
{
PRInt32 c;
nsresult ret;
ret = NS_NewConverterStream(&mIn, nsnull, aIn);
if (ret != NS_OK) {
#ifdef NS_DEBUG
cout << "NS_NewConverterStream failed" << endl;
#endif
return NS_ERROR_FAILURE;
}
c = Read();
@ -155,7 +293,7 @@ CompareKeys(const PRUnichar *aStr1, const PRUnichar *aStr2)
}
NS_IMETHODIMP
nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
nsPersistentProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue)
{
// XXX The ToNewCString() calls allocate memory using "new" so this code
@ -177,7 +315,7 @@ nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
PRUint32 hashValue = nsCRT::HashValue(key, &len);
PLHashEntry **hep = PL_HashTableRawLookup(mTable, hashValue, key);
PLHashEntry *he = *hep;
if (he && aOldValue.Length()) {
if (he && aOldValue) {
// XXX fix me
}
PL_HashTableRawAdd(mTable, hep, hashValue, aKey.ToNewUnicode(),
@ -187,13 +325,13 @@ nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
}
NS_IMETHODIMP
nsProperties::Save(nsIOutputStream* aOut, const nsString& aHeader)
nsPersistentProperties::Save(nsIOutputStream* aOut, const nsString& aHeader)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsProperties::Subclass(nsIProperties* aSubclass)
nsPersistentProperties::Subclass(nsIPersistentProperties* aSubclass)
{
if (aSubclass) {
mSubclass = aSubclass;
@ -203,9 +341,9 @@ nsProperties::Subclass(nsIProperties* aSubclass)
}
NS_IMETHODIMP
nsProperties::GetProperty(const nsString& aKey, nsString& aValue)
nsPersistentProperties::GetProperty(const nsString& aKey, nsString& aValue)
{
const PRUnichar *key = aKey.GetUnicode();
const PRUnichar *key = aKey;
PRUint32 len;
PRUint32 hashValue = nsCRT::HashValue(key, &len);
PLHashEntry **hep = PL_HashTableRawLookup(mTable, hashValue, key);
@ -219,7 +357,7 @@ nsProperties::GetProperty(const nsString& aKey, nsString& aValue)
}
PRInt32
nsProperties::Read()
nsPersistentProperties::Read()
{
PRUnichar c;
PRUint32 nRead;
@ -237,7 +375,7 @@ nsProperties::Read()
(((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
PRInt32
nsProperties::SkipWhiteSpace(PRInt32 c)
nsPersistentProperties::SkipWhiteSpace(PRInt32 c)
{
while ((c >= 0) && IS_WHITE_SPACE(c)) {
c = Read();
@ -247,7 +385,7 @@ nsProperties::SkipWhiteSpace(PRInt32 c)
}
PRInt32
nsProperties::SkipLine(PRInt32 c)
nsPersistentProperties::SkipLine(PRInt32 c)
{
while ((c >= 0) && (c != '\r') && (c != '\n')) {
c = Read();
@ -262,21 +400,55 @@ nsProperties::SkipLine(PRInt32 c)
return c;
}
nsPropertiesFactory::nsPropertiesFactory()
////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPersistentProperties::DefineProperty(const char* prop, nsISupports* initialValue)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::UndefineProperty(const char* prop)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::GetProperty(const char* prop, nsISupports* *result)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::SetProperty(const char* prop, nsISupports* value)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::HasProperty(const char* prop, nsISupports* value)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
////////////////////////////////////////////////////////////////////////////////
nsPersistentPropertiesFactory::nsPersistentPropertiesFactory()
{
NS_INIT_REFCNT();
}
nsPropertiesFactory::~nsPropertiesFactory()
nsPersistentPropertiesFactory::~nsPersistentPropertiesFactory()
{
}
NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
NS_IMPL_ISUPPORTS(nsPropertiesFactory, kIFactoryIID);
NS_IMPL_ISUPPORTS(nsPersistentPropertiesFactory, kIFactoryIID);
NS_IMETHODIMP
nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
nsPersistentPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
void** aResult)
{
if (aOuter) {
@ -286,7 +458,7 @@ nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
return NS_ERROR_NULL_POINTER;
}
*aResult = nsnull;
nsProperties* props = new nsProperties();
nsPersistentProperties* props = new nsPersistentProperties();
if (!props) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -299,7 +471,7 @@ nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
}
NS_IMETHODIMP
nsPropertiesFactory::LockFactory(PRBool aLock)
nsPersistentPropertiesFactory::LockFactory(PRBool aLock)
{
if (aLock) {
PR_AtomicIncrement(&gLockCount);
@ -310,3 +482,5 @@ nsPropertiesFactory::LockFactory(PRBool aLock)
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -21,11 +21,11 @@
#include "nsIFactory.h"
class nsPropertiesFactory : public nsIFactory
class nsPersistentPropertiesFactory : public nsIFactory
{
public:
nsPropertiesFactory();
virtual ~nsPropertiesFactory();
nsPersistentPropertiesFactory();
virtual ~nsPersistentPropertiesFactory();
NS_DECL_ISUPPORTS

Просмотреть файл

@ -29,7 +29,7 @@
#include "plevent.h"
#endif
#define TEST_URL "resource:/res/test.properties"
#define TEST_URL "resource:/res/test.PersistentProperties"
#ifdef XP_PC
#define NETLIB_DLL "netlib.dll"
@ -49,7 +49,7 @@
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID);
static NS_DEFINE_IID(kIPropertiesIID, NS_IPROPERTIES_IID);
static NS_DEFINE_IID(kIPersistentPropertiesIID, NS_IPERSISTENTPROPERTIES_IID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
@ -101,11 +101,11 @@ main(int argc, char *argv[])
printf("cannot open stream\n");
return 1;
}
nsIProperties *props = nsnull;
ret = nsComponentManager::CreateInstance(kPropertiesCID, NULL,
kIPropertiesIID, (void**) &props);
nsIPersistentProperties *props = nsnull;
ret = nsComponentManager::CreateInstance(kPersistentPropertiesCID, NULL,
kIPersistentPropertiesIID, (void**) &props);
if (NS_FAILED(ret)) {
printf("create nsIProperties failed\n");
printf("create nsIPersistentProperties failed\n");
return 1;
}
props->Load(in);

Просмотреть файл

@ -42,7 +42,7 @@ NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID);
NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID);
static NS_DEFINE_IID(kIPropertiesIID, NS_IPROPERTIES_IID);
static NS_DEFINE_IID(kIPersistentPropertiesIID, NS_IPERSISTENTPROPERTIES_IID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
class nsStringBundle : public nsIStringBundle
@ -56,7 +56,7 @@ public:
NS_IMETHOD GetStringFromID(PRInt32 aID, nsString& aResult);
NS_IMETHOD GetStringFromName(const nsString& aName, nsString& aResult);
nsIProperties* mProps;
nsIPersistentProperties* mProps;
};
nsStringBundle::nsStringBundle(nsIURL* aURL, nsILocale* aLocale,
@ -83,11 +83,11 @@ nsStringBundle::nsStringBundle(nsIURL* aURL, nsILocale* aLocale,
#endif
return;
}
*aResult = nsComponentManager::CreateInstance(kPropertiesCID, NULL,
kIPropertiesIID, (void**) &mProps);
*aResult = nsComponentManager::CreateInstance(kPersistentPropertiesCID, NULL,
kIPersistentPropertiesIID, (void**) &mProps);
if (NS_FAILED(*aResult)) {
#ifdef NS_DEBUG
printf("create nsIProperties failed\n");
printf("create nsIPersistentProperties failed\n");
#endif
return;
}

Просмотреть файл

@ -204,9 +204,9 @@ static NS_DEFINE_IID(kCXMLDocument, NS_XMLDOCUMENT_CID);
static NS_DEFINE_IID(kCImageDocument, NS_IMAGEDOCUMENT_CID);
static NS_DEFINE_IID(kCHTMLImageElement, NS_HTMLIMAGEELEMENT_CID);
static NS_DEFINE_CID(kNameSpaceManagerCID, NS_NAMESPACEMANAGER_CID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_IID(kObserverCID, NS_OBSERVER_CID);
static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID);
static NS_DEFINE_CID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
static NS_DEFINE_CID(kObserverCID, NS_OBSERVER_CID);
#if defined(NS_USING_PROFILES)
static NS_DEFINE_IID(kProfileCID, NS_PROFILE_CID);
@ -408,7 +408,7 @@ NS_SetupRegistry()
nsComponentManager::RegisterComponent(kUnicharUtilCID, NULL, NULL, UNICHARUTIL_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kPropertiesCID, NULL, NULL, BASE_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kPersistentPropertiesCID, NULL, NULL, BASE_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kCollationCID, NULL, NULL, NSLOCALE_DLL, PR_FALSE, PR_FALSE);
nsComponentManager::RegisterComponent(kCollationFactoryCID, NULL, NULL, NSLOCALE_DLL, PR_FALSE, PR_FALSE);

Просмотреть файл

@ -19,30 +19,89 @@
#ifndef nsIProperties_h___
#define nsIProperties_h___
#include "nsISupports.h"
#define NS_IPROPERTIES_IID \
{ /* f42bc870-dc17-11d2-9311-00e09805570f */ \
0xf42bc870, \
0xdc17, \
0x11d2, \
{0x93, 0x11, 0x00, 0xe0, 0x98, 0x05, 0x57, 0x0f} \
}
class nsIProperties : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IPROPERTIES_IID; return iid; }
/**
* Defines a new property.
* @return NS_ERROR_FAILURE if a property is already defined.
*/
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue) = 0;
/**
* Undefines a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD UndefineProperty(const char* prop) = 0;
/**
* Gets a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result) = 0;
/**
* Sets a property.
* @return NS_ERROR_FAILURE if a property is not already defined.
*/
NS_IMETHOD SetProperty(const char* prop, nsISupports* value) = 0;
/**
* @return NS_OK if the property exists with the specified value
* @return NS_COMFALSE if the property does not exist, or doesn't have
* the specified value (values are compared with ==)
*/
NS_IMETHOD HasProperty(const char* prop, nsISupports* value) = 0;
};
// Returns a default implementation of an nsIProperties object.
extern nsresult
NS_NewIProperties(nsIProperties* *result);
////////////////////////////////////////////////////////////////////////////////
#include "nsID.h"
#include "nsIInputStream.h"
#include "nsIOutputStream.h"
#include "nsISupports.h"
#include "nsString.h"
// {1A180F60-93B2-11d2-9B8B-00805F8A16D9}
#define NS_IPROPERTIES_IID \
#define NS_IPERSISTENTPROPERTIES_IID \
{ 0x1a180f60, 0x93b2, 0x11d2, \
{ 0x9b, 0x8b, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9 } }
// {2245E573-9464-11d2-9B8B-00805F8A16D9}
NS_DECLARE_ID(kPropertiesCID,
NS_DECLARE_ID(kPersistentPropertiesCID,
0x2245e573, 0x9464, 0x11d2, 0x9b, 0x8b, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9);
class nsIProperties : public nsISupports
class nsIPersistentProperties : public nsIProperties
{
public:
static const nsIID& GetIID() { static nsIID iid = NS_IPERSISTENTPROPERTIES_IID; return iid; }
NS_IMETHOD Load(nsIInputStream* aIn) = 0;
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader) = 0;
NS_IMETHOD Subclass(nsIPersistentProperties* aSubclass) = 0;
// XXX these 2 methods will be subsumed by the ones from
// nsIProperties once we figure this all out
NS_IMETHOD GetProperty(const nsString& aKey, nsString& aValue) = 0;
NS_IMETHOD SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue) = 0;
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader) = 0;
NS_IMETHOD Subclass(nsIProperties* aSubclass) = 0;
};
#endif /* nsIProperties_h___ */
////////////////////////////////////////////////////////////////////////////////
#endif // nsIProperties_h___

Просмотреть файл

@ -17,6 +17,130 @@
*/
#define NS_IMPL_IDS
#include "nsIProperties.h"
#include "nsHashtable.h"
////////////////////////////////////////////////////////////////////////////////
class nsProperties : public nsIProperties, public nsHashtable {
public:
NS_DECL_ISUPPORTS
// nsIProperties methods:
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue);
NS_IMETHOD UndefineProperty(const char* prop);
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result);
NS_IMETHOD SetProperty(const char* prop, nsISupports* value);
NS_IMETHOD HasProperty(const char* prop, nsISupports* value);
// nsProperties methods:
nsProperties();
virtual ~nsProperties();
static PRBool ReleaseValues(nsHashKey* key, void* data, void* closure);
};
NS_IMPL_ISUPPORTS(nsProperties, nsIProperties::GetIID());
nsProperties::nsProperties()
{
}
PRBool
nsProperties::ReleaseValues(nsHashKey* key, void* data, void* closure)
{
nsISupports* value = (nsISupports*)data;
NS_IF_RELEASE(value);
return PR_TRUE;
}
nsProperties::~nsProperties()
{
Enumerate(ReleaseValues);
}
NS_IMETHODIMP
nsProperties::DefineProperty(const char* prop, nsISupports* initialValue)
{
nsCStringKey key(prop);
if (Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Put(&key, initialValue);
NS_ASSERTION(prevValue == NULL, "hashtable error");
NS_IF_ADDREF(initialValue);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::UndefineProperty(const char* prop)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Remove(&key);
NS_IF_RELEASE(prevValue);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::GetProperty(const char* prop, nsISupports* *result)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* value = (nsISupports*)Get(&key);
NS_IF_ADDREF(value);
*result = value;
return NS_OK;
}
NS_IMETHODIMP
nsProperties::SetProperty(const char* prop, nsISupports* value)
{
nsCStringKey key(prop);
if (!Exists(&key))
return NS_ERROR_FAILURE;
nsISupports* prevValue = (nsISupports*)Put(&key, value);
NS_IF_RELEASE(prevValue);
NS_IF_ADDREF(value);
return NS_OK;
}
NS_IMETHODIMP
nsProperties::HasProperty(const char* prop, nsISupports* expectedValue)
{
nsISupports* value;
nsresult rv = GetProperty(prop, &value);
if (NS_FAILED(rv)) return NS_COMFALSE;
rv = (value == expectedValue) ? NS_OK : NS_COMFALSE;
NS_IF_RELEASE(value);
return rv;
}
////////////////////////////////////////////////////////////////////////////////
nsresult
NS_NewIProperties(nsIProperties* *result)
{
nsProperties* props = new nsProperties();
if (props == NULL)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(props);
*result = props;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// Persistent Properties (should go in a separate file)
////////////////////////////////////////////////////////////////////////////////
#include "nsID.h"
#include "nsBaseDLL.h"
@ -28,36 +152,48 @@
#include "plhash.h"
#include "pratom.h"
class nsProperties : public nsIProperties
class nsPersistentProperties : public nsIPersistentProperties
{
public:
nsProperties();
virtual ~nsProperties();
nsPersistentProperties();
virtual ~nsPersistentProperties();
NS_DECL_ISUPPORTS
// nsIProperties methods:
NS_IMETHOD DefineProperty(const char* prop, nsISupports* initialValue);
NS_IMETHOD UndefineProperty(const char* prop);
NS_IMETHOD GetProperty(const char* prop, nsISupports* *result);
NS_IMETHOD SetProperty(const char* prop, nsISupports* value);
NS_IMETHOD HasProperty(const char* prop, nsISupports* value);
// nsIPersistentProperties methods:
NS_IMETHOD Load(nsIInputStream* aIn);
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader);
NS_IMETHOD Subclass(nsIPersistentProperties* aSubclass);
// XXX these 2 methods will be subsumed by the ones from
// nsIProperties once we figure this all out
NS_IMETHOD GetProperty(const nsString& aKey, nsString& aValue);
NS_IMETHOD SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue);
NS_IMETHOD Save(nsIOutputStream* aOut, const nsString& aHeader);
NS_IMETHOD Subclass(nsIProperties* aSubclass);
// nsPersistentProperties methods:
PRInt32 Read();
PRInt32 SkipLine(PRInt32 c);
PRInt32 SkipWhiteSpace(PRInt32 c);
nsIUnicharInputStream* mIn;
nsIProperties* mSubclass;
nsIPersistentProperties* mSubclass;
struct PLHashTable* mTable;
};
nsProperties::nsProperties()
nsPersistentProperties::nsPersistentProperties()
{
NS_INIT_REFCNT();
mIn = nsnull;
mSubclass = NS_STATIC_CAST(nsIProperties*, this);
mSubclass = NS_STATIC_CAST(nsIPersistentProperties*, this);
mTable = nsnull;
}
@ -69,7 +205,7 @@ FreeHashEntries(PLHashEntry* he, PRIntn i, void* arg)
return HT_ENUMERATE_REMOVE;
}
nsProperties::~nsProperties()
nsPersistentProperties::~nsPersistentProperties()
{
if (mTable) {
// Free the PRUnicode* pointers contained in the hash table entries
@ -81,17 +217,19 @@ nsProperties::~nsProperties()
NS_DEFINE_IID(kIPropertiesIID, NS_IPROPERTIES_IID);
NS_IMPL_ISUPPORTS(nsProperties, kIPropertiesIID)
NS_IMPL_ISUPPORTS(nsPersistentProperties, kIPropertiesIID)
NS_IMETHODIMP
nsProperties::Load(nsIInputStream *aIn)
nsPersistentProperties::Load(nsIInputStream *aIn)
{
PRInt32 c;
nsresult ret;
ret = NS_NewConverterStream(&mIn, nsnull, aIn);
if (ret != NS_OK) {
#ifdef NS_DEBUG
cout << "NS_NewConverterStream failed" << endl;
#endif
return NS_ERROR_FAILURE;
}
c = Read();
@ -155,7 +293,7 @@ CompareKeys(const PRUnichar *aStr1, const PRUnichar *aStr2)
}
NS_IMETHODIMP
nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
nsPersistentProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
nsString& aOldValue)
{
// XXX The ToNewCString() calls allocate memory using "new" so this code
@ -177,7 +315,7 @@ nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
PRUint32 hashValue = nsCRT::HashValue(key, &len);
PLHashEntry **hep = PL_HashTableRawLookup(mTable, hashValue, key);
PLHashEntry *he = *hep;
if (he && aOldValue.Length()) {
if (he && aOldValue) {
// XXX fix me
}
PL_HashTableRawAdd(mTable, hep, hashValue, aKey.ToNewUnicode(),
@ -187,13 +325,13 @@ nsProperties::SetProperty(const nsString& aKey, nsString& aNewValue,
}
NS_IMETHODIMP
nsProperties::Save(nsIOutputStream* aOut, const nsString& aHeader)
nsPersistentProperties::Save(nsIOutputStream* aOut, const nsString& aHeader)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsProperties::Subclass(nsIProperties* aSubclass)
nsPersistentProperties::Subclass(nsIPersistentProperties* aSubclass)
{
if (aSubclass) {
mSubclass = aSubclass;
@ -203,9 +341,9 @@ nsProperties::Subclass(nsIProperties* aSubclass)
}
NS_IMETHODIMP
nsProperties::GetProperty(const nsString& aKey, nsString& aValue)
nsPersistentProperties::GetProperty(const nsString& aKey, nsString& aValue)
{
const PRUnichar *key = aKey.GetUnicode();
const PRUnichar *key = aKey;
PRUint32 len;
PRUint32 hashValue = nsCRT::HashValue(key, &len);
PLHashEntry **hep = PL_HashTableRawLookup(mTable, hashValue, key);
@ -219,7 +357,7 @@ nsProperties::GetProperty(const nsString& aKey, nsString& aValue)
}
PRInt32
nsProperties::Read()
nsPersistentProperties::Read()
{
PRUnichar c;
PRUint32 nRead;
@ -237,7 +375,7 @@ nsProperties::Read()
(((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n'))
PRInt32
nsProperties::SkipWhiteSpace(PRInt32 c)
nsPersistentProperties::SkipWhiteSpace(PRInt32 c)
{
while ((c >= 0) && IS_WHITE_SPACE(c)) {
c = Read();
@ -247,7 +385,7 @@ nsProperties::SkipWhiteSpace(PRInt32 c)
}
PRInt32
nsProperties::SkipLine(PRInt32 c)
nsPersistentProperties::SkipLine(PRInt32 c)
{
while ((c >= 0) && (c != '\r') && (c != '\n')) {
c = Read();
@ -262,21 +400,55 @@ nsProperties::SkipLine(PRInt32 c)
return c;
}
nsPropertiesFactory::nsPropertiesFactory()
////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsPersistentProperties::DefineProperty(const char* prop, nsISupports* initialValue)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::UndefineProperty(const char* prop)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::GetProperty(const char* prop, nsISupports* *result)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::SetProperty(const char* prop, nsISupports* value)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPersistentProperties::HasProperty(const char* prop, nsISupports* value)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
////////////////////////////////////////////////////////////////////////////////
nsPersistentPropertiesFactory::nsPersistentPropertiesFactory()
{
NS_INIT_REFCNT();
}
nsPropertiesFactory::~nsPropertiesFactory()
nsPersistentPropertiesFactory::~nsPersistentPropertiesFactory()
{
}
NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
NS_IMPL_ISUPPORTS(nsPropertiesFactory, kIFactoryIID);
NS_IMPL_ISUPPORTS(nsPersistentPropertiesFactory, kIFactoryIID);
NS_IMETHODIMP
nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
nsPersistentPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
void** aResult)
{
if (aOuter) {
@ -286,7 +458,7 @@ nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
return NS_ERROR_NULL_POINTER;
}
*aResult = nsnull;
nsProperties* props = new nsProperties();
nsPersistentProperties* props = new nsPersistentProperties();
if (!props) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -299,7 +471,7 @@ nsPropertiesFactory::CreateInstance(nsISupports* aOuter, REFNSIID aIID,
}
NS_IMETHODIMP
nsPropertiesFactory::LockFactory(PRBool aLock)
nsPersistentPropertiesFactory::LockFactory(PRBool aLock)
{
if (aLock) {
PR_AtomicIncrement(&gLockCount);
@ -310,3 +482,5 @@ nsPropertiesFactory::LockFactory(PRBool aLock)
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -21,11 +21,11 @@
#include "nsIFactory.h"
class nsPropertiesFactory : public nsIFactory
class nsPersistentPropertiesFactory : public nsIFactory
{
public:
nsPropertiesFactory();
virtual ~nsPropertiesFactory();
nsPersistentPropertiesFactory();
virtual ~nsPersistentPropertiesFactory();
NS_DECL_ISUPPORTS

Просмотреть файл

@ -29,7 +29,7 @@
#include "plevent.h"
#endif
#define TEST_URL "resource:/res/test.properties"
#define TEST_URL "resource:/res/test.PersistentProperties"
#ifdef XP_PC
#define NETLIB_DLL "netlib.dll"
@ -49,7 +49,7 @@
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID);
static NS_DEFINE_IID(kIPropertiesIID, NS_IPROPERTIES_IID);
static NS_DEFINE_IID(kIPersistentPropertiesIID, NS_IPERSISTENTPROPERTIES_IID);
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
@ -101,11 +101,11 @@ main(int argc, char *argv[])
printf("cannot open stream\n");
return 1;
}
nsIProperties *props = nsnull;
ret = nsComponentManager::CreateInstance(kPropertiesCID, NULL,
kIPropertiesIID, (void**) &props);
nsIPersistentProperties *props = nsnull;
ret = nsComponentManager::CreateInstance(kPersistentPropertiesCID, NULL,
kIPersistentPropertiesIID, (void**) &props);
if (NS_FAILED(ret)) {
printf("create nsIProperties failed\n");
printf("create nsIPersistentProperties failed\n");
return 1;
}
props->Load(in);