View Javadoc

1   package com.tapina.robe.swi.clib.stdio;
2   
3   import com.tapina.robe.runtime.Environment;
4   import com.tapina.robe.runtime.MemoryMap;
5   import com.tapina.robe.swi.clib.Stub;
6   
7   import java.io.IOException;
8   
9   /***
10   * This function reads a3 objects of size specified by a2 into the array pointed to by a1, from the stream pointed to by a4.
11   * Returns: The number of objects successfully read.
12   */
13  public class FRead extends Stub {
14      public void executeStub(Environment environment) {
15          final int[] R = environment.getCpu().R;
16          final int readSize = R[2] * R[1];
17          if (readSize == 0) {
18              R[0] = 0;
19              return;
20          }
21          final MemoryMap memoryMap = environment.getMemoryMap();
22          FilePointer filePointer = FilePointer.find(memoryMap.getWord(R[3]));
23          try {
24              byte[] data = filePointer.read(readSize);
25              memoryMap.storeBytes(R[0], data, 0, data.length - (data.length % R[1]));
26              R[0] = data.length / R[1];
27          } catch (IOException e) {
28              R[0] = 0;
29          }
30      }
31  }