Discussion:
wxDir question
(too old to reply)
Alejandro
2009-05-04 14:39:26 UTC
Permalink
Hi All,

I made a function to remove directories ("MyRemoveDir(const
wxFileName)"). I made an implementation of wxDirTraverser to enumerates
the directories to remove.
In the function MyRemoveDir, create a wxDir with the path of the
directory to remove(which is the parameter of the function), to invocate
wxDir::Traverse and enumerate subdirectories. Then when I try to remove
directories with the function wxRmdir() it fails, like when you try to
delete a file that is open.
I've seen that when a wxDir is created, it creates a struct(wxDirData)
and I think that this is the cause because wxRmdir fails(in the first
case). On the other hand wxDir help says: "wxDir is a portable
equivalent of Unix open/read/closedir functions..." but I couldn't close
a directory and wxDir isn't doing.

This is the code:

bool MyRemoveDir(const wxFileName &FnDir)
{
wxArrayString dirs;
wxDirTraverserListDirs traverser(dirs);
wxDir dir(FnDir.GetFullPath());
size_t count = dir.Traverse(traverser);

//....delete files to make empty the directories...

for(size_t i=0; i < dirs.Count(); i++)
if (!MyRemoveDir(wxFileName(dirs.Item(i),wxT(""))))
show_error_msg;

return wxRmdir(FnDir.GetPathWithSep().c_str()); //this fails
}

To fix it, I made this:

bool MyRemoveDir(const wxFileName &FnDir)
{
...like above...

//This is the change
wxDir *pdir = new wxDir(FnDir.GetFullPath());
size_t count = pdir->Traverse(traverser);
delete pdir;

like above
}

Is correct the second implementation or wxDir must provide a method to
close the directories?

Thanks.
Alejandro.
Vadim Zeitlin
2009-05-04 16:22:24 UTC
Permalink
On Mon, 04 May 2009 11:39:26 -0300 Alejandro <***@gmail.com> wrote:

A> This is the code:
A>
A> bool MyRemoveDir(const wxFileName &FnDir)
A> {
A> wxArrayString dirs;
A> wxDirTraverserListDirs traverser(dirs);
A> wxDir dir(FnDir.GetFullPath());
A> size_t count = dir.Traverse(traverser);
A>
A> //....delete files to make empty the directories...
A>
A> for(size_t i=0; i < dirs.Count(); i++)
A> if (!MyRemoveDir(wxFileName(dirs.Item(i),wxT(""))))
A> show_error_msg;
A>
A> return wxRmdir(FnDir.GetPathWithSep().c_str()); //this fails
A> }
A>
A> To fix it, I made this:
A>
A> bool MyRemoveDir(const wxFileName &FnDir)
A> {
A> ...like above...
A>
A> //This is the change
A> wxDir *pdir = new wxDir(FnDir.GetFullPath());
A> size_t count = pdir->Traverse(traverser);
A> delete pdir;
A>
A> like above
A> }

A simpler modification would be to do it like this:

bool MyRemoveDir(const wxFileName& fn)
{
...
size_t count;
{
wxDir dir(fn.GetFullPath());
count = dir.Traverse(traverser);
} // here wxDir object is destroyed
...
}

A> Is correct the second implementation or wxDir must provide a method to
A> close the directories?

We could indeed add a wxDir::Close() method and it should be simple
enough. If the lack of it bothers you enough to motivate providing a patch
for it, please do (see http://trac.wxwidgets.org/wiki/HowToSubmitPatches if
you hadn't this before) and we'd gratefully accept it.

Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
Loading...