Explorer; libede: Fixes to compile, warnings; fixed single window coming up when...
[ede] / libede / src / FileSystemIterator.ec
1 #ifdef BUILDING_ECERE_COM
2 namespace sys;
3 import "Array"
4 #else
5 #ifdef ECERE_STATIC
6 public import static "ecere"
7 #else
8 public import "ecere"
9 #endif
10 #endif
11
12 public class FileSystemIterator : InterfaceFileSystemIterator
13 {
14 public:
15    property char * extensions { set { delete extensions; if(value) extensions = CopyString(value); } get { return extensions; } }
16
17    public bool Iterate(char * startPath, bool followLinks)
18    {
19       bool result = true;
20       bool listDirEntries;
21       Array<FileSystemIteratorStackFrame> stack { };
22       FileSystemIteratorStackFrame frame;
23
24       if(iterateStartPath)
25       {
26          char name[MAX_LOCATION] = "";
27          FileStats stats;
28          //if(followLinks)
29             FileGetStats(startPath, stats);
30          /*else
31             FileGetStatsLink(startPath, stats);*/
32          GetLastDirectory(startPath, name);
33          listDirEntries = OnObject(owner, name, startPath, stats, true);
34          if(listDirEntries)
35          {
36             OnEnteringDirectory(owner, startPath);
37             // FileListing should have a sorted = true/false property
38             stack.Add((frame = { { startPath, extensions = extensions } }));
39          }
40       }
41
42       if(listDirEntries)
43       {
44          while(frame)
45          {
46             if(frame.listing.Find())
47             {
48                FileStats stats = /*followLinks ? */frame.listing.stats /*: frame.listing.lstats*/;
49                listDirEntries = OnObject(owner, frame.listing.name, frame.listing.path, stats, !iterateStartPath && stack.count == 1);
50                if(stats.attribs.isDirectory)
51                {
52                   if(listDirEntries)
53                   {
54                      OnEnteringDirectory(owner, frame.listing.path);
55                      stack.Add((frame = { { frame.listing.path, extensions = frame.listing.extensions } }));
56                   }
57                   else
58                      result = false;
59                }
60             }
61             else
62             {
63                if(stack.count > (iterateStartPath ? 0 : 1))
64                   OnLeavingDirectory(owner, frame.listing.directory);
65                delete frame;
66                stack.lastIterator.Remove();
67                frame = stack.count == 0 ? null : stack.lastIterator.data;
68             }
69          }
70          if(stack.count != 0)
71             PrintLn("dddddddd");
72          stack.Free();
73       }
74       delete stack;
75       return result;
76    }
77
78 private:
79    char * extensions;
80
81    ~FileSystemIterator()
82    {
83       delete extensions;
84    }
85 }
86
87 public class FileSystemIteratorStackFrame : struct
88 {
89 public:
90    FileListing listing;
91
92 private:
93 }
94
95 public class InterfaceFileSystemIterator
96 {
97 public:
98    Window owner; // any_object owner; <-- this makes it so that uses of owner are not compiling...
99    // the idea here is to provide iterators with the ability to use arbitrary data when used by instanciation only not defivation.
100    void * data; // any_object data; <-- this... probably the same
101    bool iterateStartPath;
102
103    virtual bool Iterate(char * startPath, bool followLinks);
104    virtual bool any_object::OnObject(/*any_object data, */const char * name, const char * path, FileStats stats, bool isRootObject);
105    virtual void any_object::OnEnteringDirectory(/*any_object data, */const char * path);
106    virtual void any_object::OnLeavingDirectory(/*any_object data, */const char * path);
107
108 private:
109 }
110
111 // TODO: implement threaded iteration somehow....
112 /*
113 static class IteratorThread : Thread
114 {
115    void Temp()
116    {
117       //listing = FileListing { dir, extensions = filter.extensions };  // there should be a sorted = true/false
118    }
119 }
120 */