1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/nsIFileEnumerator.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +#include "nsIFile.h" 1.5 +#include "nsStringGlue.h" 1.6 + 1.7 +#include <stdio.h> 1.8 +#include "nsIComponentRegistrar.h" 1.9 +#include "nsIComponentManager.h" 1.10 +#include "nsIServiceManager.h" 1.11 +#include "nsMemory.h" 1.12 +#include "nsISimpleEnumerator.h" 1.13 +#include "nsCOMPtr.h" 1.14 + 1.15 +bool LoopInDir(nsIFile* file) 1.16 +{ 1.17 + nsresult rv; 1.18 + nsCOMPtr<nsISimpleEnumerator> entries; 1.19 + rv = file->GetDirectoryEntries(getter_AddRefs(entries)); 1.20 + if(NS_FAILED(rv) || !entries) 1.21 + return false; 1.22 + 1.23 + bool hasMore; 1.24 + while(NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore) 1.25 + { 1.26 + nsCOMPtr<nsISupports> sup; 1.27 + entries->GetNext(getter_AddRefs(sup)); 1.28 + if(!sup) 1.29 + return false; 1.30 + 1.31 + nsCOMPtr<nsIFile> file = do_QueryInterface(sup); 1.32 + if(!file) 1.33 + return false; 1.34 + 1.35 + nsAutoCString name; 1.36 + if(NS_FAILED(file->GetNativeLeafName(name))) 1.37 + return false; 1.38 + 1.39 + bool isDir; 1.40 + printf("%s\n", name.get()); 1.41 + rv = file->IsDirectory(&isDir); 1.42 + if (NS_FAILED(rv)) 1.43 + { 1.44 + printf("IsDirectory Failed!!!\n"); 1.45 + return false; 1.46 + } 1.47 + 1.48 + if (isDir) 1.49 + { 1.50 + LoopInDir(file); 1.51 + } 1.52 + } 1.53 + return true; 1.54 +} 1.55 + 1.56 + 1.57 +int 1.58 +main(int argc, char* argv[]) 1.59 +{ 1.60 + nsresult rv; 1.61 + { 1.62 + nsCOMPtr<nsIFile> topDir; 1.63 + 1.64 + nsCOMPtr<nsIServiceManager> servMan; 1.65 + rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.66 + if (NS_FAILED(rv)) return -1; 1.67 + 1.68 + if (argc > 1 && argv[1] != nullptr) 1.69 + { 1.70 + char* pathStr = argv[1]; 1.71 + NS_NewNativeLocalFile(nsDependentCString(pathStr), false, getter_AddRefs(topDir)); 1.72 + } 1.73 + 1.74 + if (!topDir) 1.75 + { 1.76 + printf("No Top Dir\n"); 1.77 + return -1; 1.78 + } 1.79 + int32_t startTime = PR_IntervalNow(); 1.80 + 1.81 + LoopInDir(topDir); 1.82 + 1.83 + int32_t endTime = PR_IntervalNow(); 1.84 + 1.85 + printf("\nTime: %d\n", PR_IntervalToMilliseconds(endTime - startTime)); 1.86 + } // this scopes the nsCOMPtrs 1.87 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.88 + rv = NS_ShutdownXPCOM(nullptr); 1.89 + NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); 1.90 + return 0; 1.91 +}