gecko-dev/xpcom/tests/nsIFileEnumerator.cpp

89 lines
2.1 KiB
C++
Raw Normal View History

#include "nsIFile.h"
#include "nsStringGlue.h"
2000-07-11 00:10:34 +00:00
#include <stdio.h>
#include "nsIComponentRegistrar.h"
2000-07-11 00:10:34 +00:00
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsMemory.h"
#include "nsISimpleEnumerator.h"
#include "nsCOMPtr.h"
2000-07-11 00:10:34 +00:00
bool LoopInDir(nsIFile* file)
2000-07-11 00:10:34 +00:00
{
nsresult rv;
nsCOMPtr<nsISimpleEnumerator> entries;
rv = file->GetDirectoryEntries(getter_AddRefs(entries));
if(NS_FAILED(rv) || !entries)
return false;
2000-07-11 00:10:34 +00:00
bool hasMore;
2000-07-11 00:10:34 +00:00
while(NS_SUCCEEDED(entries->HasMoreElements(&hasMore)) && hasMore)
{
nsCOMPtr<nsISupports> sup;
entries->GetNext(getter_AddRefs(sup));
if(!sup)
return false;
2000-07-11 00:10:34 +00:00
nsCOMPtr<nsIFile> file = do_QueryInterface(sup);
2000-07-11 00:10:34 +00:00
if(!file)
return false;
2000-07-11 00:10:34 +00:00
nsAutoCString name;
if(NS_FAILED(file->GetNativeLeafName(name)))
return false;
2000-07-11 00:10:34 +00:00
bool isDir;
printf("%s\n", name.get());
2000-07-11 00:10:34 +00:00
rv = file->IsDirectory(&isDir);
if (NS_FAILED(rv))
{
printf("IsDirectory Failed!!!\n");
return false;
}
if (isDir)
2000-07-11 00:10:34 +00:00
{
LoopInDir(file);
2000-07-11 00:10:34 +00:00
}
}
return true;
2000-07-11 00:10:34 +00:00
}
int
main(int argc, char* argv[])
{
nsresult rv;
{
nsCOMPtr<nsIFile> topDir;
2000-07-11 00:10:34 +00:00
nsCOMPtr<nsIServiceManager> servMan;
rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
if (NS_FAILED(rv)) return -1;
2000-07-11 00:10:34 +00:00
if (argc > 1 && argv[1] != nullptr)
{
char* pathStr = argv[1];
NS_NewNativeLocalFile(nsDependentCString(pathStr), false, getter_AddRefs(topDir));
}
2000-07-11 00:10:34 +00:00
if (!topDir)
{
printf("No Top Dir\n");
return -1;
}
int32_t startTime = PR_IntervalNow();
2000-07-11 00:10:34 +00:00
LoopInDir(topDir);
2000-07-11 00:10:34 +00:00
int32_t endTime = PR_IntervalNow();
2000-07-11 00:10:34 +00:00
printf("\nTime: %d\n", PR_IntervalToMilliseconds(endTime - startTime));
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
rv = NS_ShutdownXPCOM(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
2000-07-11 00:10:34 +00:00
return 0;
}