-
Notifications
You must be signed in to change notification settings - Fork 1
/
MifAdder.java
109 lines (90 loc) · 3.32 KB
/
MifAdder.java
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
// Created by Emanuele Giuseppe Esposito
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
// This class takes an already created mif file and adds new informations.
public class MifAdder {
private ArrayList<String> oldword;
private int MEM_SIZE;
private int oldmifsize;
public MifAdder(String path, String imagename) throws IOException {
oldword = new ArrayList<>();
String lastaddress = "";
Scanner sc = new Scanner(new File(path));
boolean stopSearchingDepth = false;
while (sc.hasNext() ) {
String line = sc.nextLine();
if (line.replaceAll("[\u0000-\u001f]", "").equals("--finished")){
break;
}
else
{
lastaddress = line.split(" ")[0];
if (!(stopSearchingDepth))
{
String[] l = line.split(" ");
for (int i = 0; i < l.length; i++)
{
if (l[i].equals("DEPTH"))
{
MEM_SIZE = Integer.parseInt(l[i+2].replaceAll(";",""));
stopSearchingDepth = true;
}
}
}
if ((line.length() > 14) && (line.substring(0,13).equals("-- Image used"))){
line += " " + imagename;
}
oldword.add(line);
}
}
if (!(lastaddress.equals("END;"))){
oldmifsize = Integer.parseInt(lastaddress);
}
else{
throw new ArrayIndexOutOfBoundsException("Message");
}
}
public void CreateNewFile(String[] toAdd, String name) throws ArrayIndexOutOfBoundsException{
try{
PrintWriter writer;
writer = new PrintWriter(name+ ".mif", "UTF-8");
int j = 0;
while (!(oldword.get(j).equals("BEGIN"))){
writer.println(oldword.get(j));
j++;
}
writer.println("BEGIN");
for (String line: oldword.subList(j+ 1,oldword.size())){ //maybe index too large
writer.println(line);
}
if (oldmifsize + toAdd.length < MEM_SIZE){
writer.println("--additional image");
int k = 0;
while(k < toAdd.length){
oldmifsize++;
writer.println("" + oldmifsize + " : " + toAdd[k] + ";");
k ++;
}
if (oldmifsize < MEM_SIZE) {
writer.println("--finished");
oldmifsize++;
writer.println("[" + oldmifsize + ".." + (MEM_SIZE - 1) + "] : " + 0 + ";");
System.out.println("You still have "+ (MEM_SIZE - oldmifsize) + " free words!");
}
writer.println("");
writer.println("END;");
writer.close();
}
else {
throw new ArrayIndexOutOfBoundsException("Message");
}
}
catch(IOException e){
System.out.println("Error creating the mif file ");
System.out.println("Error : " + e);
}
}
}