File size: 4,469 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

CDirEntry *FilesFind2(U8 *files_find_mask,I64 fuf_flags)
{
  CDrv *dv=Fs->cur_dv;
  CDirEntry *res=NULL;
  DrvChk(dv);
  switch (dv->fs_type) {
    case FSt_REDSEA:
      res=RedSeaFilesFind(files_find_mask,fuf_flags);
      break;
    case FSt_FAT32:
      res=FAT32FilesFind(files_find_mask,fuf_flags);
      break;
    case FSt_ISO9660:
      res=ISO1FilesFind(files_find_mask,fuf_flags);
      break;
    default:
      PrintErr("File System Not Supported\n");
      res=NULL;
  }
  if (res) {
    DirFilesSort(&res,SK_NAME);
    if (fuf_flags&(FUF_FLATTEN_TREE|FUF_JUST_FILES))
      DirFilesFlatten(res,&res,fuf_flags);
    if (fuf_flags&FUF_CLUS_ORDER)
      DirFilesSort(&res,SK_CLUS);
  }
  return res;
}

CDirEntry *FilesFind(U8 *files_find_mask,I64 fuf_flags=0)
{/* See ::/Doc/FileUtils.DD.

Find files and make a directory tree in memory.

When done, you free with DirEntryDel(),DirEntryDel2(),
DirTreeDel() or DirTreeDel2().

*/
  CDirEntry *res;
  CDirContext *dirc;
  if (fuf_flags&~FUG_FILES_FIND)
    throw('FUF');
  if (fuf_flags&FUF_SINGLE) {
    res=MAlloc(sizeof(CDirEntry));
    if (!FileFind(files_find_mask,res)) {
      Free(res);
      return NULL;
    }
  } else if (dirc=DirContextNew(files_find_mask,TRUE)) {
    res=FilesFind2(dirc->mask,fuf_flags);
    DirContextDel(dirc);
  } else
    return NULL;
  return res;
}

Bool FileFind(U8 *filename,CDirEntry *_de=NULL,I64 fuf_flags=0)
{//FUF_JUST_DIRS, FUF_JUST_FILES, FUF_Z_OR_NOT_Z, FUF_SCAN_PARENTS
//If you pass _de, you must Free(_de->full_name);
  I64 i,j,cur_dir_clus;
  U8 *altname,*curname,*full_name=NULL;
  CDirEntry de;
  CDirContext *dirc;
  Bool res=FALSE,old_silent;
  if (fuf_flags&~FUG_FILE_FIND)
    throw('FUF');
  if (!filename || *filename && filename[1]==':' && !Let2Drv(*filename,FALSE))
    return FALSE;
  altname=ToggleZorNotZ(filename);
  if (fuf_flags&FUF_Z_OR_NOT_Z)
    j=2;
  else
    j=1;
  for (i=0;i<j && !res;i++) {
    if (!i)
      curname=filename;
    else
      curname=altname;
    old_silent=Silent;
    if (!(dirc=DirContextNew(curname)))
      Silent(old_silent);
    else {
      Silent(old_silent);
      cur_dir_clus=Name2DirClus(dirc->dv,Fs->cur_dir);
      switch (dirc->dv->fs_type) {
        case FSt_REDSEA:
          res=RedSeaFileFind(dirc->dv,cur_dir_clus,dirc->mask,
                &de,fuf_flags);
          break;
        case FSt_FAT32:
          res=FAT32FileFind(dirc->dv,cur_dir_clus,dirc->mask,
                &de,fuf_flags);
          break;
        case FSt_ISO9660:
          res=ISO1FileFind(dirc->dv,cur_dir_clus,dirc->mask,
                &de,fuf_flags);
          break;
        default:
          PrintErr("File System Not Supported\n");
      }
      if (res && _de) {
        if (StrCmp(Fs->cur_dir,"/"))
          full_name=MStrPrint("%C:%s/%s",
                Drv2Let(Fs->cur_dv),Fs->cur_dir,de.name);
        else
          full_name=MStrPrint("%C:/%s",Drv2Let(Fs->cur_dv),de.name);
      }
      DirContextDel(dirc);
    }
  }
  for (i=0;i<j && !res && fuf_flags&FUF_SCAN_PARENTS;i++) {
    if (!i)
      curname=filename;
    else
      curname=altname;
    old_silent=Silent;
    if (!(dirc=DirContextNew(curname)))
      Silent(old_silent);
    else {
      Silent(old_silent);
      cur_dir_clus=Name2DirClus(dirc->dv,Fs->cur_dir);
      while (!res && StrCmp(Fs->cur_dir,"/")) {
        Cd("..");
        cur_dir_clus=Name2DirClus(dirc->dv,Fs->cur_dir);
        switch (dirc->dv->fs_type) {
          case FSt_REDSEA:
            res=RedSeaFileFind(dirc->dv,cur_dir_clus,
                  dirc->mask,&de,fuf_flags);
            break;
          case FSt_FAT32:
            res=FAT32FileFind(dirc->dv,cur_dir_clus,
                  dirc->mask,&de,fuf_flags);
            break;
          case FSt_ISO9660:
            res=ISO1FileFind(dirc->dv,cur_dir_clus,
                  dirc->mask,&de,fuf_flags);
            break;
          default:
            PrintErr("File System Not Supported\n");
        }
      }
      if (res && _de) {
        if (StrCmp(Fs->cur_dir,"/"))
          full_name=MStrPrint("%C:%s/%s",
                Drv2Let(Fs->cur_dv),Fs->cur_dir,de.name);
        else
          full_name=MStrPrint("%C:/%s",
                Drv2Let(Fs->cur_dv),de.name);
      }
      DirContextDel(dirc);
    }
  }
  if (_de) {
    if (res) {
      MemCpy(_de,&de,sizeof(CDirEntry));
      _de->full_name=full_name;
    } else
      MemSet(_de,0,sizeof(CDirEntry));
  }
  Free(altname);
  return res;
}