@@ -124,16 +124,18 @@ private static class fopen implements Function {
124
124
125
125
@ Override
126
126
public Value execute (Value [] args ) {
127
- Arguments .checkAtLeast ( 1 , args .length );
127
+ Arguments .checkOrOr ( 1 , 2 , args .length );
128
128
129
129
final File file = Console .fileInstance (args [0 ].asString ());
130
130
try {
131
- if (args .length > 1 ) {
132
- return process ( file , args [1 ].asString ().toLowerCase ());
133
- }
134
- return process (file , "r" );
131
+ final String mode = (args .length == 2 )
132
+ ? args [1 ].asString ().toLowerCase ()
133
+ : "r" ;
134
+ return process (file , mode );
135
135
} catch (IOException ioe ) {
136
- return NumberValue .MINUS_ONE ;
136
+ final int key = -files .size () - 1 ;
137
+ files .put (key , new FileInfo (ioe .getMessage ()));
138
+ return NumberValue .of (key );
137
139
}
138
140
}
139
141
@@ -167,8 +169,10 @@ private abstract static class FileFunction implements Function {
167
169
public Value execute (Value [] args ) {
168
170
if (args .length < 1 ) throw new ArgumentsMismatchException ("File descriptor expected" );
169
171
final int key = args [0 ].asInt ();
172
+ final FileInfo fileInfo = files .get (key );
173
+ if (key < 0 ) throw new ArgumentsMismatchException (fileInfo .error );
170
174
try {
171
- return execute (files . get ( key ) , args );
175
+ return execute (fileInfo , args );
172
176
} catch (IOException ioe ) {
173
177
return NumberValue .MINUS_ONE ;
174
178
}
@@ -578,12 +582,17 @@ private static Function fileToBoolean(FileToBooleanFunction f) {
578
582
579
583
private static class FileInfo {
580
584
File file ;
585
+ String error ;
581
586
DataInputStream dis ;
582
587
DataOutputStream dos ;
583
588
BufferedReader reader ;
584
589
BufferedWriter writer ;
585
590
586
- public FileInfo (File file , DataInputStream dis , DataOutputStream dos , BufferedReader reader , BufferedWriter writer ) {
591
+ FileInfo (String errorMessage ) {
592
+ this .error = errorMessage ;
593
+ }
594
+
595
+ FileInfo (File file , DataInputStream dis , DataOutputStream dos , BufferedReader reader , BufferedWriter writer ) {
587
596
this .file = file ;
588
597
this .dis = dis ;
589
598
this .dos = dos ;
0 commit comments