Skip to content

Commit 7eb1524

Browse files
committed
Initiallise repo
1 parent dafb379 commit 7eb1524

File tree

490 files changed

+45804
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

490 files changed

+45804
-0
lines changed

AP3/.metadata/.lock

Whitespace-only changes.

AP3/.metadata/.log

Lines changed: 255 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.util.concurrent.LinkedBlockingQueue;
4+
import java.util.concurrent.ConcurrentSkipListSet;
5+
import java.util.regex.*;
6+
7+
8+
9+
public class fileCrawler {
10+
public static class worker implements Runnable{
11+
private Pattern p;
12+
private ConcurrentSkipListSet<String> fileList;
13+
private LinkedBlockingQueue<String> dirList;
14+
15+
worker(Pattern pat, ConcurrentSkipListSet<String> fileList, LinkedBlockingQueue<String> dirList)
16+
{
17+
this.p = pat;
18+
this.fileList = fileList;
19+
this.dirList = dirList;
20+
}
21+
22+
public void CslsAdd(File dir){
23+
File entries[] = dir.listFiles(); // make a list of all the files and folders in the directory given
24+
for (File entry : entries ) { //for every element in the list
25+
if (entry.isFile()) { // check if it is a file
26+
Matcher m = p.matcher(entry.getName()); // see if it matches the pattern
27+
if (m.matches()){ // if so
28+
fileList.add(dir.getPath() + "/" + entry.getName()); // add the path and string name to the file list of the file.
29+
}
30+
}
31+
}
32+
33+
}
34+
35+
public void run(){
36+
File file = null; //have to define it before because the LBQ.take is in a try statement
37+
while (true){ // run forever
38+
try {
39+
file = new File(dirList.take()); // take the first dir off the work queue (waiting if it is empty)
40+
CslsAdd(file); // add any files matching the pattern into the file list
41+
} catch (InterruptedException e) {
42+
43+
while (!dirList.isEmpty()){ // once interrupted keep taking things of the work queue until it is empty
44+
file = new File(dirList.poll());
45+
CslsAdd(file);
46+
47+
48+
}
49+
return; // leave the infinite loop once the work queue is empty
50+
}
51+
}
52+
53+
}
54+
55+
}
56+
57+
/*totally copied*/
58+
public static String cvtPattern(String str) {
59+
StringBuilder pat = new StringBuilder();
60+
int start, length;
61+
62+
pat.append('^');
63+
if (str.charAt(0) == '\'') { // double quoting on Windows
64+
start = 1;
65+
length = str.length() - 1;
66+
} else {
67+
start = 0;
68+
length = str.length();
69+
}
70+
for (int i = start; i < length; i++) {
71+
switch(str.charAt(i)) {
72+
case '*': pat.append('.'); pat.append('*'); break;
73+
case '.': pat.append('\\'); pat.append('.'); break;
74+
case '?': pat.append('.'); break;
75+
default: pat.append(str.charAt(i)); break;
76+
}
77+
}
78+
pat.append('$');
79+
return new String(pat);
80+
}
81+
82+
83+
84+
/*all copied except changed print statement to LBQ.add(name)*/
85+
public void processDirectory(String name, LinkedBlockingQueue<String> dirList) {
86+
try {
87+
88+
File file = new File(name); // create a File object
89+
90+
if (file.isDirectory()) { //if file is directory
91+
dirList.add(name); // then add to work queue
92+
String entries[] = file.list(); // create a list of all files and folders in the directory given
93+
if (entries != null) {
94+
95+
for (String entry : entries ) {
96+
if (entry.compareTo(".") == 0)
97+
continue;
98+
if (entry.compareTo("..") == 0)
99+
continue;
100+
101+
processDirectory(name+"/"+entry, dirList);
102+
}
103+
}
104+
}
105+
}
106+
catch (Exception e) {
107+
System.err.println("Error processing "+ name +": "+e);
108+
}
109+
}
110+
111+
public static void main( String Arg[] ) throws IOException{
112+
fileCrawler FC = new fileCrawler();
113+
int noThreads = 5;
114+
115+
ConcurrentSkipListSet<String> fileList = new ConcurrentSkipListSet<String>();
116+
LinkedBlockingQueue<String> dirList = new LinkedBlockingQueue<String>();
117+
118+
if ((System.getenv("CRAWLER_THREADS") != null)){
119+
noThreads = Integer.parseInt(System.getenv("CRAWLER_THREADS"));
120+
}
121+
122+
Thread workers[] = new Thread[noThreads];
123+
124+
/* 2 lines copied*/
125+
String pattern = cvtPattern(Arg[0]);
126+
Pattern p = Pattern.compile(pattern);
127+
128+
/*worker stuff can't really change*/
129+
worker wt = new worker(p, fileList, dirList);
130+
131+
for (int i = 0;i<workers.length;i++){
132+
133+
workers[i] = new Thread(wt);
134+
workers[i].start();
135+
}
136+
137+
/* makes it work with no directory argument*/
138+
if (Arg.length == 1){
139+
FC.processDirectory(".", dirList);
140+
}
141+
else {
142+
for (int i = 1; i<Arg.length; i++)
143+
FC.processDirectory(Arg[i], dirList);
144+
}
145+
146+
/*interrupts the workers once the worker queue*/
147+
for (Thread t : workers){
148+
t.interrupt();
149+
}
150+
/*join workers once they are done*/
151+
for (Thread k : workers){
152+
try {
153+
k.join();
154+
}
155+
catch(Exception e){
156+
}
157+
}
158+
/*print out everything in the File list*/
159+
while (!fileList.isEmpty()){
160+
System.out.println(fileList.pollFirst());
161+
}
162+
}
163+
164+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.File;
2+
import java.util.concurrent.ConcurrentLinkedQueue;
3+
import java.util.concurrent.PriorityBlockingQueue;
4+
import java.util.regex.*;
5+
6+
7+
public class fileCrawler {
8+
9+
10+
11+
public void processDirectory(String name, ConcurrentLinkedQueue<String> CLQ, String pat, PriorityBlockingQueue<String> PBQ) {
12+
try {
13+
14+
File file = new File(name); // create a File object
15+
16+
if (file.isDirectory()) { // a directory - could be symlink
17+
//System.out.println("check1");
18+
CLQ.add(name);
19+
String entries[] = file.list();
20+
if (entries != null) { // not a symlink;
21+
}
22+
for (String entry : entries ) {
23+
if (entry.compareTo(".") == 0)
24+
continue;
25+
if (entry.compareTo("..") == 0)
26+
continue;
27+
//System.out.println("check2");
28+
processDirectory(name+"/"+entry, CLQ, pat, PBQ);
29+
}
30+
31+
}
32+
else {
33+
//System.out.println("check3");
34+
String pattern = Regex.cvtPattern(pat);
35+
Pattern p = Pattern.compile(pattern);
36+
Matcher m = p.matcher(name);
37+
if (m.matches()){
38+
//System.out.println("check4");
39+
PBQ.add(name);
40+
}
41+
}
42+
43+
44+
}
45+
catch (Exception e) {
46+
System.err.println("Error processing "+ name +": "+e);
47+
}
48+
}
49+
50+
51+
52+
public static void main( String Arg[] ){
53+
String fileName = Arg[1];
54+
fileCrawler FC = new fileCrawler();
55+
if (fileName.charAt(fileName.length()-1)== ']'){
56+
fileName = fileName.substring(0, fileName.length()-2);
57+
}
58+
if (fileName.charAt(0)== '['){
59+
fileName = fileName.substring(1);
60+
}
61+
62+
PriorityBlockingQueue<String> PBQ = new PriorityBlockingQueue<String>();
63+
ConcurrentLinkedQueue<String> CLQ = new ConcurrentLinkedQueue<String>();
64+
FC.processDirectory(fileName, CLQ, Arg[0], PBQ);
65+
66+
while (!PBQ.isEmpty()){
67+
//System.out.println("check");
68+
System.out.println(PBQ.poll());
69+
}
70+
}
71+
72+
73+
74+
}
75+
76+
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.util.concurrent.LinkedBlockingQueue;
4+
import java.util.concurrent.ConcurrentSkipListSet;
5+
import java.util.regex.*;
6+
7+
8+
public class fileCrawler {
9+
10+
public static class worker implements Runnable{
11+
private Pattern p;
12+
private ConcurrentSkipListSet<String> CSLS;
13+
private LinkedBlockingQueue<String> LBQ;
14+
15+
worker(Pattern pat, ConcurrentSkipListSet<String> CSLS, LinkedBlockingQueue<String> LBQ)
16+
{
17+
this.p = pat;
18+
this.CSLS = CSLS;
19+
this.LBQ = LBQ;
20+
}
21+
22+
public void run ( ) {
23+
File file = null;
24+
String fileName = null;
25+
//while (true){
26+
try {
27+
fileName = LBQ.take();
28+
file = new File(fileName);
29+
} catch (InterruptedException e) {
30+
System.err.println("Error processing "+ file.getName() +": "+e);
31+
e.printStackTrace();
32+
}
33+
File entries[] = file.listFiles();
34+
for (File entry : entries ) {
35+
if (!entry.isDirectory()) {
36+
Matcher m = p.matcher(entry.getName());
37+
if (m.matches()){
38+
CSLS.add(file + "/" + entry);
39+
}
40+
}
41+
42+
// }
43+
}
44+
}
45+
}
46+
47+
48+
public static String cvtPattern(String str) {
49+
StringBuilder pat = new StringBuilder();
50+
int start, length;
51+
52+
pat.append('^');
53+
if (str.charAt(0) == '\'') { // double quoting on Windows
54+
start = 1;
55+
length = str.length() - 1;
56+
} else {
57+
start = 0;
58+
length = str.length();
59+
}
60+
for (int i = start; i < length; i++) {
61+
switch(str.charAt(i)) {
62+
case '*': pat.append('.'); pat.append('*'); break;
63+
case '.': pat.append('\\'); pat.append('.'); break;
64+
case '?': pat.append('.'); break;
65+
default: pat.append(str.charAt(i)); break;
66+
}
67+
}
68+
pat.append('$');
69+
return new String(pat);
70+
}
71+
72+
73+
public void processDirectory(String name, LinkedBlockingQueue<String> LBQ) {
74+
try {
75+
76+
File file = new File(name); // create a File object
77+
78+
if (file.isDirectory()) { // a directory - could be symlink
79+
LBQ.add(name);
80+
String entries[] = file.list();
81+
if (entries != null) { // not a symlink;
82+
}
83+
for (String entry : entries ) {
84+
if (entry.compareTo(".") == 0)
85+
continue;
86+
if (entry.compareTo("..") == 0)
87+
continue;
88+
89+
processDirectory(name+"/"+entry, LBQ);
90+
}
91+
}
92+
}
93+
catch (Exception e) {
94+
System.err.println("Error processing "+ name +": "+e);
95+
}
96+
}
97+
98+
public static void main( String Arg[] ) throws IOException{
99+
fileCrawler FC = new fileCrawler();
100+
int noThreads = 2;
101+
102+
ConcurrentSkipListSet<String> CSLS = new ConcurrentSkipListSet<String>();
103+
LinkedBlockingQueue<String> LBQ = new LinkedBlockingQueue<String>();
104+
105+
if ((System.getenv("CRAWLER_THREADS") != null)){
106+
noThreads = Integer.parseInt(System.getenv("CRAWLER_THREADS"));
107+
}
108+
109+
Thread workers[] = new Thread[noThreads];
110+
111+
String pattern = cvtPattern(Arg[0]);
112+
Pattern p = Pattern.compile(pattern);
113+
114+
worker wt = new worker(p, CSLS, LBQ);
115+
116+
for (int i = 0;i<workers.length;i++){
117+
118+
workers[i] = new Thread(wt);
119+
workers[i].start();
120+
}
121+
if (Arg.length == 1){
122+
FC.processDirectory(".", LBQ);
123+
}
124+
else {
125+
for (int i = 1; i<Arg.length; i++)
126+
FC.processDirectory(Arg[i], LBQ);
127+
}
128+
129+
//while (!LBQ.isEmpty()){
130+
//System.out.println( LBQ.poll() );
131+
//}
132+
133+
for (Thread t : workers){
134+
t.interrupt();
135+
}
136+
for (Thread k : workers){
137+
try {
138+
k.join();
139+
}
140+
catch(Exception e){
141+
}
142+
}
143+
144+
while (!CSLS.isEmpty()){
145+
System.out.println(CSLS.pollFirst());
146+
}
147+
}
148+
149+
150+
151+
}
152+
153+

0 commit comments

Comments
 (0)