Skip to content

Commit 81c8e73

Browse files
authored
Part 2 is underway. Writing in RandomAccessFiles completed.
1 parent cdc8a84 commit 81c8e73

File tree

5 files changed

+292
-19
lines changed

5 files changed

+292
-19
lines changed

src/test/Main.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package test;
22
import util.FileOps;
3+
import util.FilePageAccess;
34
import util.Item;
45
import util.List;
56
import util.Node;
67

7-
8+
import java.io.*;
89
import java.io.FileNotFoundException;
910
import java.io.IOException;
1011
import java.util.Scanner;
@@ -16,7 +17,13 @@ public class Main {
1617

1718
public static void main(String[] args) throws IOException,FileNotFoundException {
1819

20+
//define useful thresholds.
21+
final int MAX_LINE = 80;
22+
final int MIN_WORD = 2;
23+
final int MAX_WORD = 20;
24+
final int PAGESIZE = 128;
1925

26+
//instantiate structures and objects.
2027
List list = new List();
2128
Scanner in = new Scanner(System.in);
2229
Session u = new Session();
@@ -29,9 +36,17 @@ public static void main(String[] args) throws IOException,FileNotFoundException
2936
System.exit(0);
3037
}
3138

32-
//open given .txt file, read line-wise
3339

34-
FileOps fops = new FileOps(args[0],list,80, 5 ,20);
40+
//open given .txt file, read line-wise.
41+
String fNam = args[0];
42+
String dict = fNam+".ndx";
43+
44+
45+
46+
//construct an instance of a utility class.
47+
FileOps fops = new FileOps(fNam,list,MAX_LINE, MIN_WORD ,MAX_WORD);
48+
FilePageAccess fpa= new FilePageAccess(PAGESIZE,MAX_WORD,dict);
49+
3550

3651
// read and fill the LineList
3752
fops.retrieveContext();
@@ -50,7 +65,7 @@ public static void main(String[] args) throws IOException,FileNotFoundException
5065

5166
if(inputCheck(u.getCmd()) != null)
5267
{
53-
execCommand(u,list,in,fops);
68+
execCommand(u,list,in,fops,fpa);
5469
}
5570

5671
System.out.println();
@@ -72,7 +87,7 @@ public static void main(String[] args) throws IOException,FileNotFoundException
7287

7388
}
7489

75-
private static void execCommand(Session u, List lines,Scanner input, FileOps f) throws FileNotFoundException, IOException
90+
private static void execCommand(Session u, List lines,Scanner input, FileOps f,FilePageAccess fpa) throws FileNotFoundException, IOException
7691
{
7792

7893

@@ -118,7 +133,11 @@ private static void execCommand(Session u, List lines,Scanner input, FileOps f)
118133
break;
119134
case "l":
120135
//print all lines, nodes of the file
121-
lines.print();
136+
137+
if(!u.isRaw())
138+
lines.print();
139+
else
140+
lines.printRaw();
122141
break;
123142
case "n":
124143
u.alterPrintMode();
@@ -189,7 +208,15 @@ private static void execCommand(Session u, List lines,Scanner input, FileOps f)
189208
break;
190209
case "c":
191210

192-
f.fillWordMap();
211+
//construct a dictionary and print the outcome.
212+
fpa.fillDictionary(f.fillWordMap());
213+
214+
//print info.
215+
System.out.print("Ok. Datapages of "+fpa.getPageSize()+" :"+fpa.getDataPages());
216+
217+
218+
219+
193220
break;
194221

195222

@@ -210,7 +237,7 @@ private static String inputCheck(String inp)
210237

211238
//using a regex to check input and another one to check whether to print message to user or not.
212239
String valid = "[atdlnpqwxc=#\\-\\$\\^\\+]";
213-
String printable = "[lp=#]";
240+
String printable = "[clp=#]";
214241

215242
Matcher m = Pattern.compile(valid).matcher(inp);
216243
Matcher mp = Pattern.compile(printable).matcher(inp);

src/test/Session.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public Session(boolean r, int curr, File fp, String command)
1515
currentLine = curr;
1616
f = fp;
1717
cmd = command;
18+
1819
}
1920

2021
public Session()

src/util/FileOps.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,55 @@
77

88
public class FileOps {
99

10-
File f;
11-
List lList;
10+
private File f;
11+
private List lList;
1212
private int minSize, maxSize;
1313
static int threshold;
14+
final int tokenSize = maxSize + 4;
1415

1516

16-
public FileOps(String fNam, List l, int t,int low, int up) throws FileNotFoundException
17+
public FileOps(String fNam,List l, int t,int low, int up) throws FileNotFoundException
1718
{
1819

1920
f = new File(fNam);
2021
minSize = low;
2122
maxSize = up;
2223
lList = l;
2324
threshold = t;
25+
2426

2527

2628

2729
}
30+
31+
public File getFile()
32+
{
33+
return this.f;
34+
}
35+
2836
public void retrieveContext() throws IOException,FileNotFoundException
2937
{
3038
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
31-
String line = r.readLine();
39+
String line;
3240
int lCount = 0;
3341
LineItem l;
3442

3543

3644

3745
//itterate through the file, line - wise
38-
while(line != null)
46+
while((line = r.readLine()) != null)
3947
{
4048

4149

4250
//process, get an Item back
4351
l = (LineItem) formatInput(line.trim(), lCount);
4452

4553
//store LineItem
54+
if(l != null)
55+
storeLine(lList,l);
4656

47-
storeLine(lList,l);
4857

49-
50-
//move to next line
51-
line = r.readLine();
58+
//count to next line.
5259
lCount++;
5360

5461
}
@@ -91,7 +98,7 @@ public void storeContext() throws IOException,FileNotFoundException
9198

9299

93100
/*
94-
* -Gets a String representing an ASCII text and a line Number. Returns a LineItem with info given.
101+
* -Gets a String representing an ASCII text and a line Number. Returns a LineItem with info given or null if given string is emptyLine.
95102
* -Trims given string - input in case that this exceeds threshold characters.
96103
*
97104
* */
@@ -109,6 +116,10 @@ public static Item formatInput(String lContext, int lNum)
109116

110117
}
111118

119+
// exclude the empty or whitespaced line.
120+
if(lContext.isEmpty())
121+
return null;
122+
112123
//create a LineItem
113124
LineItem l = new LineItem(lContext);
114125

@@ -162,6 +173,11 @@ public ArrayList<Word> fillWordMap()
162173
//sort the array list based on the "word"-key.
163174
Collections.sort(words);
164175

176+
for(int i=0; i<words.size(); i++)
177+
{
178+
System.out.println(words.get(i).getContext());
179+
}
180+
165181
///return the sorted arraylist.
166182

167183
return words;
@@ -170,6 +186,9 @@ public ArrayList<Word> fillWordMap()
170186

171187
}
172188

189+
190+
191+
173192

174193
/*
175194
* Stores a created line item in a double linked list.
@@ -182,7 +201,13 @@ public void storeLine(List l, LineItem line)
182201

183202
}
184203

185-
204+
public void printWordMap(ArrayList<Word> map)
205+
{
206+
for(int i = 0; i<map.size(); i++)
207+
{
208+
System.out.println(map.get(i).getContext()+" "+map.get(i).getLine());
209+
}
210+
}
186211

187212
private boolean isValidWord(String w)
188213
{

0 commit comments

Comments
 (0)