Back to index

lightning-sunbird  0.9+nobinonly
nsIFileEnumerator.cpp
Go to the documentation of this file.
00001 #include "nsILocalFile.h"
00002 #include "nsDependentString.h"
00003 #include "nsString.h"
00004 
00005 #include <stdio.h>
00006 #include "nsIComponentRegistrar.h"
00007 #include "nsIComponentManager.h"
00008 #include "nsIServiceManager.h"
00009 #include "nsMemory.h"
00010 #include "nsXPIDLString.h"
00011 #include "nsISimpleEnumerator.h"
00012 
00013 
00014 PRBool LoopInDir(nsILocalFile* file)
00015 {
00016     nsresult rv;
00017     nsCOMPtr<nsISimpleEnumerator> entries;
00018     rv = file->GetDirectoryEntries(getter_AddRefs(entries));
00019     if(NS_FAILED(rv) || !entries)
00020         return PR_FALSE;
00021     
00022     PRBool hasMore;
00023     while(NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore)
00024     {
00025         nsCOMPtr<nsISupports> sup;
00026         entries->GetNext(getter_AddRefs(sup));
00027         if(!sup)
00028             return PR_FALSE;
00029         
00030         nsCOMPtr<nsILocalFile> file = do_QueryInterface(sup);
00031         if(!file)
00032             return PR_FALSE;
00033     
00034         nsCAutoString name;
00035         if(NS_FAILED(file->GetNativeLeafName(name)))
00036             return PR_FALSE;
00037         
00038         PRBool isDir;
00039         printf("%s\n", name.get());
00040         rv = file->IsDirectory(&isDir);
00041         if (NS_FAILED(rv))
00042               {
00043                      printf("IsDirectory Failed!!!\n");
00044                             return PR_FALSE;
00045               }
00046 
00047               if (isDir == PR_TRUE)
00048         {
00049            nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
00050            LoopInDir(lfile);   
00051         }        
00052     }
00053     return PR_TRUE;
00054 }
00055 
00056 
00057 int
00058 main(int argc, char* argv[])
00059 {
00060     nsresult rv;
00061     {
00062         nsCOMPtr<nsILocalFile> topDir;
00063 
00064         nsCOMPtr<nsIServiceManager> servMan;
00065         rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00066         if (NS_FAILED(rv)) return -1;
00067         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00068         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00069         if (registrar)
00070             registrar->AutoRegister(nsnull);
00071 
00072         if (argc > 1 && argv[1] != nsnull)
00073         {
00074             char* pathStr = argv[1];
00075             NS_NewNativeLocalFile(nsDependentCString(pathStr), PR_FALSE, getter_AddRefs(topDir));
00076         }
00077     
00078         if (!topDir)
00079         {
00080            printf("No Top Dir\n");
00081            return -1;
00082         }
00083         PRInt32 startTime = PR_IntervalNow();
00084     
00085         LoopInDir(topDir);
00086     
00087         PRInt32 endTime = PR_IntervalNow();
00088     
00089         printf("\nTime: %d\n", PR_IntervalToMilliseconds(endTime - startTime));
00090     } // this scopes the nsCOMPtrs
00091     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00092     rv = NS_ShutdownXPCOM(nsnull);
00093     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00094     return 0;
00095 }