Source code: com/hartmath/lib/HFileList.java
1 /*
2 * HFileList.java
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 package com.hartmath.lib;
19
20 import java.io.*;
21 import java.util.*;
22
23 /**
24 This class parses a subtree for files that match a pattern.
25 The pattern may contain one or more * and ? as usual.
26 The class delivers an enumerator for the files, or may be subclassed
27 to handle the files directly. The routines directory and file can
28 be used to return, if more scanning is necessary.
29 */
30
31 public class HFileList
32 { Vector V=new Vector();
33 boolean Stop;
34 public HFileList (String dir, String filter)
35 { Stop=false;
36 char f[]=filter.toCharArray();
37 File file=new File(dir);
38 if (file.isDirectory()) find(file,f);
39 }
40 public HFileList (String dir)
41 { Stop=false;
42 File file=new File(dir);
43 if (file.isDirectory()) find(file,null);
44 }
45 void find (File dir, char filter[])
46 { if (!directory(dir)) return;
47 String list[]=dir.list();
48 for (int i=0; i<list.length; i++)
49 { File file=new File(dir,list[i]);
50 if (file.isDirectory()) find(file,filter);
51 else if (match(file.getName().toCharArray(),0,filter,0))
52 { Stop=!file(file);
53 if (Stop) break;
54 V.addElement(file);
55 }
56 if (Stop) break;
57 }
58 parsed(dir);
59 }
60 boolean match (char filename[], int n, char filter[], int m)
61 { if (filter==null) return true;
62 if (m>=filter.length) return n>=filename.length;
63 if (n>=filename.length) return m==filter.length-1 && filter[m]=='*';
64 if (filter[m]=='?')
65 { return match(filename,n+1,filter,m+1);
66 }
67 if (filter[m]=='*')
68 { if (m==filter.length-1) return true;
69 for (int i=n; i<filename.length; i++)
70 { if (match(filename,i,filter,m+1)) return true;
71 }
72 return false;
73 }
74 if (filter[m]==filename[n]) return match(filename,n+1,filter,m+1);
75 return false;
76 }
77 public Enumeration files ()
78 { return V.elements();
79 }
80 /**
81 @param file The directory that has been found.
82 @return false if recursion should stop here.
83 (i.e. that directory needs not be parsed).
84 */
85 public boolean directory (File dir)
86 { return true;
87 }
88 /**
89 @param file The file that has been found.
90 @return false if you need no more file at all.
91 */
92 public boolean file (File file)
93 { return true;
94 }
95 /**
96 @param parsed The directory that has been parsed.
97 */
98 public void parsed (File dir)
99 {
100 }
101 }
102