-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIRPL.java
More file actions
146 lines (134 loc) · 3.86 KB
/
UIRPL.java
File metadata and controls
146 lines (134 loc) · 3.86 KB
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
import java.io.*;
import java.net.*;
import java.util.*;
public class UIRPL{
private BufferedReader in;
private PrintStream out;
private PileRPL pile;
UIRPL(int taillePile, String args[])throws IOException{
pile = new PileRPL(taillePile);
init_flux(args);
String log = "";
boolean loop = true;
while(loop){
String str = in.readLine();
log += str + "\n";
if (str.equals("exit"))
loop = false;
else {
String[] operands = str.split(" ");
try {
parse(operands);
}catch (NumberFormatException e){
out.println("Syntax error !");
}
out.println(pile);
}
}
if (!(args.length == 2 && args[0].equals("local")) && args.length != 3){
out.println("Voulez-vous sauvegardez votre session ? [filename/non]");
String logFile = in.readLine();
//LOG
if (logFile != null && !logFile.toLowerCase().equals("non")){
try {
logSession(logFile, log);
} catch (IOException e) {
out.println("La sauvegarde des logs a échoué");
}
}
}
//Close file
try {
exit();
} catch (IOException e) {
out.println("Erreur à la fermeture des fichiers");
}
}
public void exit() throws IOException {
this.in.close();
this.out.close();
}
public void logSession(String filename, String logs) throws IOException{
PrintWriter logFile = new PrintWriter( new BufferedWriter (new FileWriter(filename)));
logFile.write(logs);
logFile.close();
}
public void init_flux(String args[]) throws IOException{
if (args.length == 0 || (args.length == 1 && args[0].toLowerCase().equals("local"))){
this.in = new BufferedReader(new InputStreamReader(System.in));
this.out = new PrintStream(System.out);
}
else if (args.length == 2 && args[0].toLowerCase().equals("local")){
this.in = new BufferedReader( new FileReader(args[1]) );
this.out = new PrintStream(System.out);
}
else if ((args.length == 3 || args.length == 2) && args[0].toLowerCase().equals("network")){
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[1]));
Socket socket = serverSocket.accept();
this.out = new PrintStream(socket.getOutputStream());
if (args.length == 3)
this.in = new BufferedReader(new FileReader(args[2]));
else
this.in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
else
usage();
}
public void parse(String[] operands)throws NumberFormatException{
for (int i = 0; i < operands.length; i++){
try{
switch (operands[i]) {
case("+"):
case("add"):
this.pile.add();
break;
case("-"):
case("sou"):
this.pile.sou();
break;
case("*"):
case("mul"):
this.pile.mul();
break;
case("/"):
case("div"):
this.pile.div();
break;
case("push"):
break;
case("pop"):
this.pile.pop();
break;
case("drop"):
this.pile.drop();
break;
case("swap"):
this.pile.swap();
break;
case("clear"):
this.pile.clear();
break;
default:
this.pile.push(new ObjEmpilable(Integer.parseInt(operands[i])));
break;
}} catch (ArrayIndexOutOfBoundsException e){
this.out.println("Pas assez d'éléments dans la pile !");
}
}
}
public void usage(){
System.err.println("Syntax error !!! USAGE:\njava UIRPL:\n\trun calculator in interactive mode.\njava UIRPL local:\n\trun calculator in interactive mode.\njava UIRPL local LOGFILENAME:\n\tredo the session in LOGFILENAME\njava network PORT:\n\tCreate socket localhost:PORT and run calculatorover the network.\njava network PORT LOGFILENAME:\n\tCreate socket localhost:PORT and redo the session in LOGFILENAME");
System.exit(1);
}
public static void main(String args[]) throws Exception{
try{
UIRPL uiRPL = new UIRPL(50, args);
} catch(IOException e){
if (args.length == 1)
System.err.println("Ouverture du fichier " + args[0] + " impossible !");
else if (args.length == 2)
System.err.println("Ouverture du socket 127.0.0.1:" + args[1] + " impossible !");
System.exit(1);
}
}
}