2002-05-08 06:08:25 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-05-08 06:08:25 +00:00
|
|
|
|
2004-10-21 20:57:17 +00:00
|
|
|
#include "TestCommon.h"
|
2002-05-08 06:08:25 +00:00
|
|
|
#include "nsXPCOM.h"
|
2005-11-08 19:23:00 +00:00
|
|
|
#include "nsStringAPI.h"
|
2002-05-08 06:08:25 +00:00
|
|
|
#include "nsIPersistentProperties2.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIEnumerator.h"
|
2004-02-07 02:41:10 +00:00
|
|
|
#include <stdio.h>
|
2005-11-08 19:23:00 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2002-05-08 06:08:25 +00:00
|
|
|
|
|
|
|
#define TEST_URL "resource:/res/test.properties"
|
2002-08-30 17:13:34 +00:00
|
|
|
static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
|
2002-05-08 06:08:25 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2004-10-21 20:57:17 +00:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-05-08 06:08:25 +00:00
|
|
|
nsresult ret;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2002-05-08 06:08:25 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIInputStream* in = nullptr;
|
2002-05-08 06:08:25 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
|
|
|
|
if (NS_FAILED(ret)) return ret;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIChannel *channel = nullptr;
|
|
|
|
ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nullptr, nullptr, &channel);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret)) return ret;
|
|
|
|
|
|
|
|
ret = channel->Open(&in);
|
|
|
|
if (NS_FAILED(ret)) return ret;
|
|
|
|
|
2004-11-01 18:50:36 +00:00
|
|
|
nsIPersistentProperties* props;
|
|
|
|
ret = CallCreateInstance(kPersistentPropertiesCID, &props);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret) || (!props)) {
|
|
|
|
printf("create nsIPersistentProperties failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = props->Load(in);
|
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("cannot load properties\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int i = 1;
|
|
|
|
while (1) {
|
|
|
|
char name[16];
|
|
|
|
name[0] = 0;
|
|
|
|
sprintf(name, "%d", i);
|
|
|
|
nsAutoString v;
|
2002-08-30 17:13:34 +00:00
|
|
|
ret = props->GetStringProperty(nsDependentCString(name), v);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret) || (!v.Length())) {
|
|
|
|
break;
|
|
|
|
}
|
2005-11-08 19:23:00 +00:00
|
|
|
printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
|
2002-05-08 06:08:25 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2002-08-30 17:13:34 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> propEnum;
|
|
|
|
ret = props->Enumerate(getter_AddRefs(propEnum));
|
|
|
|
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret)) {
|
2002-08-30 17:13:34 +00:00
|
|
|
printf("cannot enumerate properties\n");
|
|
|
|
return 1;
|
2002-05-08 06:08:25 +00:00
|
|
|
}
|
2002-08-30 17:13:34 +00:00
|
|
|
|
2002-05-08 06:08:25 +00:00
|
|
|
|
2004-02-07 02:41:10 +00:00
|
|
|
printf("\nKey\tValue\n");
|
|
|
|
printf( "---\t-----\n");
|
2002-08-30 17:13:34 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasMore;
|
2002-08-30 17:13:34 +00:00
|
|
|
while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
|
|
|
|
hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> sup;
|
|
|
|
ret = propEnum->GetNext(getter_AddRefs(sup));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret)) {
|
2002-08-30 17:13:34 +00:00
|
|
|
printf("failed to get current item\n");
|
|
|
|
return 1;
|
2002-05-08 06:08:25 +00:00
|
|
|
}
|
|
|
|
|
2002-08-30 17:13:34 +00:00
|
|
|
nsCAutoString key;
|
|
|
|
nsAutoString value;
|
2002-05-08 06:08:25 +00:00
|
|
|
|
2002-08-30 17:13:34 +00:00
|
|
|
ret = propElem->GetKey(key);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("failed to get current element's key\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2002-08-30 17:13:34 +00:00
|
|
|
ret = propElem->GetValue(value);
|
2002-05-08 06:08:25 +00:00
|
|
|
if (NS_FAILED(ret)) {
|
|
|
|
printf("failed to get current element's value\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-10-27 01:05:09 +00:00
|
|
|
printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
|
2002-05-08 06:08:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|