michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestCommon.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsIPersistentProperties2.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsIURL.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsIComponentManager.h" michael@0: #include michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: michael@0: #define TEST_URL "resource:/res/test.properties" michael@0: static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID); michael@0: michael@0: static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); michael@0: michael@0: /***************************************************************************/ michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult ret; michael@0: michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: michael@0: nsIInputStream* in = nullptr; michael@0: michael@0: nsCOMPtr service(do_GetService(kIOServiceCID, &ret)); michael@0: if (NS_FAILED(ret)) return 1; michael@0: michael@0: nsIChannel *channel = nullptr; michael@0: ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nullptr, nullptr, &channel); michael@0: if (NS_FAILED(ret)) return 1; michael@0: michael@0: ret = channel->Open(&in); michael@0: if (NS_FAILED(ret)) return 1; michael@0: michael@0: nsIPersistentProperties* props; michael@0: ret = CallCreateInstance(kPersistentPropertiesCID, &props); michael@0: if (NS_FAILED(ret) || (!props)) { michael@0: printf("create nsIPersistentProperties failed\n"); michael@0: return 1; michael@0: } michael@0: ret = props->Load(in); michael@0: if (NS_FAILED(ret)) { michael@0: printf("cannot load properties\n"); michael@0: return 1; michael@0: } michael@0: int i = 1; michael@0: while (1) { michael@0: char name[16]; michael@0: name[0] = 0; michael@0: sprintf(name, "%d", i); michael@0: nsAutoString v; michael@0: ret = props->GetStringProperty(nsDependentCString(name), v); michael@0: if (NS_FAILED(ret) || (!v.Length())) { michael@0: break; michael@0: } michael@0: printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get()); michael@0: i++; michael@0: } michael@0: michael@0: nsCOMPtr propEnum; michael@0: ret = props->Enumerate(getter_AddRefs(propEnum)); michael@0: michael@0: if (NS_FAILED(ret)) { michael@0: printf("cannot enumerate properties\n"); michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: printf("\nKey\tValue\n"); michael@0: printf( "---\t-----\n"); michael@0: michael@0: bool hasMore; michael@0: while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) && michael@0: hasMore) { michael@0: nsCOMPtr sup; michael@0: ret = propEnum->GetNext(getter_AddRefs(sup)); michael@0: michael@0: nsCOMPtr propElem = do_QueryInterface(sup, &ret); michael@0: if (NS_FAILED(ret)) { michael@0: printf("failed to get current item\n"); michael@0: return 1; michael@0: } michael@0: michael@0: nsAutoCString key; michael@0: nsAutoString value; michael@0: michael@0: ret = propElem->GetKey(key); michael@0: if (NS_FAILED(ret)) { michael@0: printf("failed to get current element's key\n"); michael@0: return 1; michael@0: } michael@0: ret = propElem->GetValue(value); michael@0: if (NS_FAILED(ret)) { michael@0: printf("failed to get current element's value\n"); michael@0: return 1; michael@0: } michael@0: michael@0: printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get()); michael@0: } michael@0: return 0; michael@0: }