1 | #include "nsILocalFile.h"
|
---|
2 | #include "nsDependentString.h"
|
---|
3 | #include "nsString.h"
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include "nsIComponentRegistrar.h"
|
---|
7 | #include "nsIComponentManager.h"
|
---|
8 | #include "nsIServiceManager.h"
|
---|
9 | #include "nsMemory.h"
|
---|
10 | #include "nsXPIDLString.h"
|
---|
11 | #include "nsISimpleEnumerator.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | PRBool LoopInDir(nsILocalFile* file)
|
---|
15 | {
|
---|
16 | nsresult rv;
|
---|
17 | nsCOMPtr<nsISimpleEnumerator> entries;
|
---|
18 | rv = file->GetDirectoryEntries(getter_AddRefs(entries));
|
---|
19 | if(NS_FAILED(rv) || !entries)
|
---|
20 | return PR_FALSE;
|
---|
21 |
|
---|
22 | PRBool hasMore;
|
---|
23 | while(NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore)
|
---|
24 | {
|
---|
25 | nsCOMPtr<nsISupports> sup;
|
---|
26 | entries->GetNext(getter_AddRefs(sup));
|
---|
27 | if(!sup)
|
---|
28 | return PR_FALSE;
|
---|
29 |
|
---|
30 | nsCOMPtr<nsILocalFile> file = do_QueryInterface(sup);
|
---|
31 | if(!file)
|
---|
32 | return PR_FALSE;
|
---|
33 |
|
---|
34 | nsCAutoString name;
|
---|
35 | if(NS_FAILED(file->GetNativeLeafName(name)))
|
---|
36 | return PR_FALSE;
|
---|
37 |
|
---|
38 | PRBool isDir;
|
---|
39 | printf("%s\n", name.get());
|
---|
40 | rv = file->IsDirectory(&isDir);
|
---|
41 | if (NS_FAILED(rv))
|
---|
42 | {
|
---|
43 | printf("IsDirectory Failed!!!\n");
|
---|
44 | return PR_FALSE;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (isDir == PR_TRUE)
|
---|
48 | {
|
---|
49 | nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
|
---|
50 | LoopInDir(lfile);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return PR_TRUE;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | int
|
---|
58 | main(int argc, char* argv[])
|
---|
59 | {
|
---|
60 | nsresult rv;
|
---|
61 | {
|
---|
62 | nsCOMPtr<nsILocalFile> topDir;
|
---|
63 |
|
---|
64 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
65 | rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
66 | if (NS_FAILED(rv)) return -1;
|
---|
67 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
68 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
69 | if (registrar)
|
---|
70 | registrar->AutoRegister(nsnull);
|
---|
71 |
|
---|
72 | if (argc > 1 && argv[1] != nsnull)
|
---|
73 | {
|
---|
74 | char* pathStr = argv[1];
|
---|
75 | NS_NewNativeLocalFile(nsDependentCString(pathStr), PR_FALSE, getter_AddRefs(topDir));
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (!topDir)
|
---|
79 | {
|
---|
80 | printf("No Top Dir\n");
|
---|
81 | return -1;
|
---|
82 | }
|
---|
83 | PRInt32 startTime = PR_IntervalNow();
|
---|
84 |
|
---|
85 | LoopInDir(topDir);
|
---|
86 |
|
---|
87 | PRInt32 endTime = PR_IntervalNow();
|
---|
88 |
|
---|
89 | printf("\nTime: %d\n", PR_IntervalToMilliseconds(endTime - startTime));
|
---|
90 | } // this scopes the nsCOMPtrs
|
---|
91 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
---|
92 | rv = NS_ShutdownXPCOM(nsnull);
|
---|
93 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
---|
94 | return 0;
|
---|
95 | }
|
---|