|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "TestCommon.h" |
|
7 #include "nsXPCOM.h" |
|
8 #include "nsStringAPI.h" |
|
9 #include "nsIPersistentProperties2.h" |
|
10 #include "nsIServiceManager.h" |
|
11 #include "nsIComponentRegistrar.h" |
|
12 #include "nsIURL.h" |
|
13 #include "nsIIOService.h" |
|
14 #include "nsNetCID.h" |
|
15 #include "nsIChannel.h" |
|
16 #include "nsIComponentManager.h" |
|
17 #include <stdio.h> |
|
18 #include "nsComponentManagerUtils.h" |
|
19 #include "nsServiceManagerUtils.h" |
|
20 #include "nsISimpleEnumerator.h" |
|
21 |
|
22 #define TEST_URL "resource:/res/test.properties" |
|
23 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID); |
|
24 |
|
25 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); |
|
26 |
|
27 /***************************************************************************/ |
|
28 |
|
29 int |
|
30 main(int argc, char* argv[]) |
|
31 { |
|
32 if (test_common_init(&argc, &argv) != 0) |
|
33 return -1; |
|
34 |
|
35 nsresult ret; |
|
36 |
|
37 nsCOMPtr<nsIServiceManager> servMan; |
|
38 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
|
39 |
|
40 nsIInputStream* in = nullptr; |
|
41 |
|
42 nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret)); |
|
43 if (NS_FAILED(ret)) return 1; |
|
44 |
|
45 nsIChannel *channel = nullptr; |
|
46 ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nullptr, nullptr, &channel); |
|
47 if (NS_FAILED(ret)) return 1; |
|
48 |
|
49 ret = channel->Open(&in); |
|
50 if (NS_FAILED(ret)) return 1; |
|
51 |
|
52 nsIPersistentProperties* props; |
|
53 ret = CallCreateInstance(kPersistentPropertiesCID, &props); |
|
54 if (NS_FAILED(ret) || (!props)) { |
|
55 printf("create nsIPersistentProperties failed\n"); |
|
56 return 1; |
|
57 } |
|
58 ret = props->Load(in); |
|
59 if (NS_FAILED(ret)) { |
|
60 printf("cannot load properties\n"); |
|
61 return 1; |
|
62 } |
|
63 int i = 1; |
|
64 while (1) { |
|
65 char name[16]; |
|
66 name[0] = 0; |
|
67 sprintf(name, "%d", i); |
|
68 nsAutoString v; |
|
69 ret = props->GetStringProperty(nsDependentCString(name), v); |
|
70 if (NS_FAILED(ret) || (!v.Length())) { |
|
71 break; |
|
72 } |
|
73 printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get()); |
|
74 i++; |
|
75 } |
|
76 |
|
77 nsCOMPtr<nsISimpleEnumerator> propEnum; |
|
78 ret = props->Enumerate(getter_AddRefs(propEnum)); |
|
79 |
|
80 if (NS_FAILED(ret)) { |
|
81 printf("cannot enumerate properties\n"); |
|
82 return 1; |
|
83 } |
|
84 |
|
85 |
|
86 printf("\nKey\tValue\n"); |
|
87 printf( "---\t-----\n"); |
|
88 |
|
89 bool hasMore; |
|
90 while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) && |
|
91 hasMore) { |
|
92 nsCOMPtr<nsISupports> sup; |
|
93 ret = propEnum->GetNext(getter_AddRefs(sup)); |
|
94 |
|
95 nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret); |
|
96 if (NS_FAILED(ret)) { |
|
97 printf("failed to get current item\n"); |
|
98 return 1; |
|
99 } |
|
100 |
|
101 nsAutoCString key; |
|
102 nsAutoString value; |
|
103 |
|
104 ret = propElem->GetKey(key); |
|
105 if (NS_FAILED(ret)) { |
|
106 printf("failed to get current element's key\n"); |
|
107 return 1; |
|
108 } |
|
109 ret = propElem->GetValue(value); |
|
110 if (NS_FAILED(ret)) { |
|
111 printf("failed to get current element's value\n"); |
|
112 return 1; |
|
113 } |
|
114 |
|
115 printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get()); |
|
116 } |
|
117 return 0; |
|
118 } |