[files] Detailed error message for fopen

This commit is contained in:
aNNiMON 2023-10-03 23:08:18 +03:00 committed by Victor Melnik
parent 34298bceb8
commit 39b94a0113

View File

@ -124,16 +124,18 @@ public final class files implements Module {
@Override @Override
public Value execute(Value[] args) { public Value execute(Value[] args) {
Arguments.checkAtLeast(1, args.length); Arguments.checkOrOr(1, 2, args.length);
final File file = Console.fileInstance(args[0].asString()); final File file = Console.fileInstance(args[0].asString());
try { try {
if (args.length > 1) { final String mode = (args.length == 2)
return process(file, args[1].asString().toLowerCase()); ? args[1].asString().toLowerCase()
} : "r";
return process(file, "r"); return process(file, mode);
} catch (IOException ioe) { } catch (IOException ioe) {
return NumberValue.MINUS_ONE; final int key = -files.size() - 1;
files.put(key, new FileInfo(ioe.getMessage()));
return NumberValue.of(key);
} }
} }
@ -167,8 +169,10 @@ public final class files implements Module {
public Value execute(Value[] args) { public Value execute(Value[] args) {
if (args.length < 1) throw new ArgumentsMismatchException("File descriptor expected"); if (args.length < 1) throw new ArgumentsMismatchException("File descriptor expected");
final int key = args[0].asInt(); final int key = args[0].asInt();
final FileInfo fileInfo = files.get(key);
if (key < 0) throw new ArgumentsMismatchException(fileInfo.error);
try { try {
return execute(files.get(key), args); return execute(fileInfo, args);
} catch (IOException ioe) { } catch (IOException ioe) {
return NumberValue.MINUS_ONE; return NumberValue.MINUS_ONE;
} }
@ -578,12 +582,17 @@ public final class files implements Module {
private static class FileInfo { private static class FileInfo {
File file; File file;
String error;
DataInputStream dis; DataInputStream dis;
DataOutputStream dos; DataOutputStream dos;
BufferedReader reader; BufferedReader reader;
BufferedWriter writer; BufferedWriter writer;
public FileInfo(File file, DataInputStream dis, DataOutputStream dos, BufferedReader reader, BufferedWriter writer) { FileInfo(String errorMessage) {
this.error = errorMessage;
}
FileInfo(File file, DataInputStream dis, DataOutputStream dos, BufferedReader reader, BufferedWriter writer) {
this.file = file; this.file = file;
this.dis = dis; this.dis = dis;
this.dos = dos; this.dos = dos;