File: FileSystemIterator.ec

package info (click to toggle)
ecere-sdk 0.44.15-1
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 97,712 kB
  • ctags: 54,695
  • sloc: ansic: 593,042; makefile: 12,250; yacc: 4,955; lex: 707; objc: 259; python: 252; xml: 102
file content (132 lines) | stat: -rw-r--r-- 3,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifdef BUILDING_ECERE_COM
namespace sys;
import "Array"
#else
#ifdef ECERE_STATIC
public import static "ecere"
#else
public import "ecere"
#endif
#endif

public class NormalFileSystemIterator : FileSystemIterator
{
public:
   Array<StackFrame> stack { };

   char * extensions;
   property const char * extensions { set { delete extensions; if(value) extensions = CopyString(value); } }

   ~NormalFileSystemIterator()
   {
      delete extensions;
   }

   void Iterate(const char * startPath)
   {
      StackFrame frame;

      if(!OnInit(startPath))
         return;

      frame = StackFrame { };
      stack.Add(frame);
      frame.path = CopyString(startPath);
      frame.listing = FileListing { startPath, extensions = extensions };  // there should be a sorted = true/false

      if(iterateStartPath)
      {
         FileAttribs attribs = FileExists(startPath);
         // || attribs.isCDROM || attribs.isRemote || attribs.isRemovable || attribs.isServer || attribs.isShare || attribs.isSystem || attribs.isTemporary
         if(attribs.isDrive)
            OnVolume(startPath);
         else if(attribs.isDirectory)
            OnFolder(startPath);
         else if(attribs.isFile)
            OnFile(startPath);
      }

      while(stack.count)
      {
         if(frame.listing.Find())
         {
            bool peek = frame.listing.stats.attribs.isDirectory && OnFolder(frame.listing.path);
            if(!frame.listing.stats.attribs.isDirectory)
            {
               const char * path = frame.listing.path;
               OnFile(path);
            }
            else if(peek)
            {
               StackFrame newFrame { };
               stack.Add(newFrame);
               newFrame.path = CopyString(frame.listing.path);
               newFrame.listing = FileListing { newFrame.path, extensions = frame.listing.extensions };
               frame = newFrame;
            }
         }
         else
         {
            StackFrame parentFrame = stack.count > 1 ? stack[stack.count - 2] : null;
            OutFolder(parentFrame ? parentFrame.listing.path : startPath, !parentFrame);
            stack.lastIterator.Remove();
            delete frame;
            if(stack.count)
               frame = stack.lastIterator.data;
            else
               frame = null;
         }
      }
   }
}

public class FileSystemIterator
{
public:
   bool iterateStartPath;

   virtual bool OnInit(const char * startPath)
   {
      return true;
   }

   virtual bool OnFile(const char * filePath)
   {
      return true;
   }

   virtual bool OnFolder(const char * folderPath)
   {
      return true;
   }

   virtual bool OnVolume(const char * volumePath)
   {
      return true;
   }

   virtual void OutFolder(const char * folderPath, bool isRoot)
   {
   }
}
/*
static class IteratorThread : Thread
{
   void Temp()
   {
      //listing = FileListing { dir, extensions = filter.extensions };  // there should be a sorted = true/false
   }
}
*/
public class StackFrame
{
   int tag;
   char * path;
   FileListing listing;

   ~StackFrame()
   {
      delete path;
      //delete listing;
   }
};