x
This website is using cookies. We use cookies to ensure that we give you the best experience on our website. More info. That's Fine
HPC:Factor Logo 
 
Latest Forum Activity

PocketC, recursive functions and a missing function

fragenmensch Page Icon Posted 2005-11-27 1:23 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Hi!

I have got Console from Rainer Keuchel, it all works fine, except one thing: The LS command doesn't accept patterns and recursive listing, so I wanted to code a replacement at least for the recursive listing with the code:

dirlist(string path){ string out,rpath,fn; int cnt=0,fa; rpath=path+"*"; out=fileenum(1,rpath); while(out){ fn=path+out+"\\"; fa=GetFileAttr(fn); if((fa&0x00000010)&&fa!=-1){ putsl(path+out); cnt=cnt+dirlist(fn); cnt++; } out=fileenum(0,rpath); } out=fileenum(1,rpath); while(out){ fn=path+out; if(!(GetFileAttr(fn)&0x00000010)){ putsl(path+out); cnt++; } out=fileenum(0,rpath); } return cnt; } main(){ showconsole(); putsl(dirlist("\\Windows\\Programme\\" )); }

Why does this code not list all dirs???
 Top of the page
Snappy! Page Icon Posted 2005-11-28 8:55 AM
#
Avatar image of Snappy!
H/PC Elder

Posts:
1,712
Location:
New Mexico, US
Status:
I see that you are doing a recursive call right now, but your function dirlist seem a bit more complex than it should or need be.

Maybe you can dump the output for us?


See my comments inside

dirlist(string path)
{
  string out,rpath,fn;
int cnt=0,fa;
rpath=path+"*";

// enumerate files in rpath
out=fileenum(1,rpath);
while(out)
{
// if current file is a directory, recurse
fn=path+out+"\\"; // <--- this is a wrong assumption
fa=GetFileAttr(fn);
if((fa&0x00000010)&&fa!=-1)
{
putsl(path+out);
cnt=cnt+dirlist(fn);
cnt++; // <--- might as well put this outside
}
// begin suggested mod
else if (!(GetFileAttr(fn)&0x00000010)) // If file is a normal file bla bla bla
{
putsl(path+out);
}
cnt++; // <--- might as well put this outside
// end suggested mod

out=fileenum(0,rpath); // <--- get next file in enum list
}

// Why have another loop to list out files when it can be done above?
out=fileenum(1,rpath);
while(out)
{
fn=path+out;
if(!(GetFileAttr(fn)&0x00000010))
{
putsl(path+out);
cnt++;
}
out=fileenum(0,rpath);
}

return cnt;
}

main()
{
showconsole();
putsl(dirlist("\\Windows\\Programme\\" ));
}


Not too sure about specific syntax for PocketC, but here's how I would write the main loop in dirlist:

// enumerate files in rpath out=fileenum(1,rpath); while(out) { // if current file is a directory, recurse fn=path+out; fa=GetFileAttr(fn); if((fa&0x00000010)&&fa!=-1) { putsl(path+out+"\\"; cnt=cnt+dirlist(fn); } else if (!(GetFileAttr(fn)&0x00000010)) // If file is a normal file bla bla bla { putsl(path+out); } cnt++; // <--- might as well put this outside out=fileenum(0,rpath); // <--- get next file in enum list }


I am assuming you got the string syntax correct so I am not doing checks on that. Also, I am assuming that the Fileattribute check flags are working fine to start with.

Hope this helps.

Edited by Snappy! 2005-11-28 8:59 AM
 Top of the page
fragenmensch Page Icon Posted 2005-11-28 1:21 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Snappy! - 2005-11-28 2:55 PM

I see that you are doing a recursive call right now, but your function dirlist seem a bit more complex than it should or need be.

Maybe you can dump the output for us?


See my comments inside

dirlist(string path)
{
string out,rpath,fn;
int cnt=0,fa;
rpath=path+"*";

// enumerate files in rpath
out=fileenum(1,rpath);
while(out)
{
// if current file is a directory, recurse
fn=path+out+"\\"; // <--- this is a wrong assumption
fa=GetFileAttr(fn);
if((fa&0x00000010)&&fa!=-1)
{
putsl(path+out);
cnt=cnt+dirlist(fn);
cnt++; // <--- might as well put this outside
}
// begin suggested mod
else if (!(GetFileAttr(fn)&0x00000010)) // If file is a normal file bla bla bla
{
putsl(path+out);
}
cnt++; // <--- might as well put this outside
// end suggested mod

out=fileenum(0,rpath); // <--- get next file in enum list
}

// Why have another loop to list out files when it can be done above?
out=fileenum(1,rpath);
while(out)
{
fn=path+out;
if(!(GetFileAttr(fn)&0x00000010))
{
putsl(path+out);
cnt++;
}
out=fileenum(0,rpath);
}

return cnt;
}

main()
{
showconsole();
putsl(dirlist("\\Windows\\Programme\\" ));
}


Not too sure about specific syntax for PocketC, but here's how I would write the main loop in dirlist:

// enumerate files in rpath out=fileenum(1,rpath); while(out) { // if current file is a directory, recurse fn=path+out; fa=GetFileAttr(fn); if((fa&0x00000010)&&fa!=-1) { putsl(path+out+"\\"; cnt=cnt+dirlist(fn); } else if (!(GetFileAttr(fn)&0x00000010)) // If file is a normal file bla bla bla { putsl(path+out); } cnt++; // <--- might as well put this outside out=fileenum(0,rpath); // <--- get next file in enum list }


I am assuming you got the string syntax correct so I am not doing checks on that. Also, I am assuming that the Fileattribute check flags are working fine to start with.

Hope this helps.

The flags are correct, although I was nearly before throwing my PDA away because of the lacking docuents about binary comparing. I have a good idea now what the error is:

1)Function call #1 calls FileEnum
2)Function call #2 calls FileEnum
here is the bug: Call #2 resets the buffer in FileEnum.......

And to the question why I did the whole loop again, without the recurse:
PHP, Perl, and normal Win32 DIR list directories first, but FileEnum works at a random share.

THX for your answer,

Marco

PS: I will take a look if I can dump the otput for my root dir.
 Top of the page
Jump to forum:
Seconds to generate: 0.187 - Cached queries : 60 - Executed queries : 9