View Javadoc

1   package com.tapina.robe.swi.clib.stdio;
2   
3   import com.tapina.robe.runtime.Environment;
4   import com.tapina.robe.swi.clib.CLibrary;
5   import com.tapina.robe.swi.clib.Stub;
6   
7   import java.io.IOException;
8   
9   /***
10   * This function closes the file stream pointed to by stream. The stream is now invalid.
11   * Any data currently buffered to be written out will be written out before closure.
12   * If the buffer was allocated automatically (the usual case), the memory will be released.
13   * Returns: Zero if the file is closed, EOF if it is not.
14   */
15  public class FClose extends Stub {
16      public void executeStub(Environment environment) {
17          final int[] R = environment.getCpu().R;
18          if (R[0] == 0) {
19              return; // We allow calling this with a null pointer to do nothing.
20          }
21          FilePointer filePointer = FilePointer.find(environment.getMemoryMap().getWord(R[0]));
22          try {
23              filePointer.close();
24              R[0] = 0;
25          } catch (IOException e) {
26              log.warning("Error in fclose(): " + e.getMessage());
27              R[0] = CLibrary.EOF;
28          }
29      }
30  }