ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / extras / 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 NormalFileSystemIterator : FileSystemIterator
13 {
14 public:
15    Array<StackFrame> stack { };
16
17    char * extensions;
18    property const char * extensions { set { delete extensions; if(value) extensions = CopyString(value); } }
19
20    ~NormalFileSystemIterator()
21    {
22       delete extensions;
23    }
24
25    void Iterate(const char * startPath)
26    {
27       StackFrame frame;
28
29       if(!OnInit(startPath))
30          return;
31
32       frame = StackFrame { };
33       stack.Add(frame);
34       frame.path = CopyString(startPath);
35       frame.listing = FileListing { startPath, extensions = extensions };  // there should be a sorted = true/false
36
37       if(iterateStartPath)
38       {
39          FileAttribs attribs = FileExists(startPath);
40          // || attribs.isCDROM || attribs.isRemote || attribs.isRemovable || attribs.isServer || attribs.isShare || attribs.isSystem || attribs.isTemporary
41          if(attribs.isDrive)
42             OnVolume(startPath);
43          else if(attribs.isDirectory)
44             OnFolder(startPath);
45          else if(attribs.isFile)
46             OnFile(startPath);
47       }
48
49       while(stack.count)
50       {
51          if(frame.listing.Find())
52          {
53             bool peek = frame.listing.stats.attribs.isDirectory && OnFolder(frame.listing.path);
54             if(!frame.listing.stats.attribs.isDirectory)
55             {
56                const char * path = frame.listing.path;
57                OnFile(path);
58             }
59             else if(peek)
60             {
61                StackFrame newFrame { };
62                stack.Add(newFrame);
63                newFrame.path = CopyString(frame.listing.path);
64                newFrame.listing = FileListing { newFrame.path, extensions = frame.listing.extensions };
65                frame = newFrame;
66             }
67          }
68          else
69          {
70             StackFrame parentFrame = stack.count > 1 ? stack[stack.count - 2] : null;
71             OutFolder(parentFrame ? parentFrame.listing.path : startPath, !parentFrame);
72             stack.lastIterator.Remove();
73             delete frame;
74             if(stack.count)
75                frame = stack.lastIterator.data;
76             else
77                frame = null;
78          }
79       }
80    }
81 }
82
83 public class FileSystemIterator
84 {
85 public:
86    bool iterateStartPath;
87
88    virtual bool OnInit(const char * startPath)
89    {
90       return true;
91    }
92
93    virtual bool OnFile(const char * filePath)
94    {
95       return true;
96    }
97
98    virtual bool OnFolder(const char * folderPath)
99    {
100       return true;
101    }
102
103    virtual bool OnVolume(const char * volumePath)
104    {
105       return true;
106    }
107
108    virtual void OutFolder(const char * folderPath, bool isRoot)
109    {
110    }
111 }
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 */
121 public class StackFrame
122 {
123    int tag;
124    char * path;
125    FileListing listing;
126
127    ~StackFrame()
128    {
129       delete path;
130       //delete listing;
131    }
132 };