1 package com.tapina.robe.swi.clib.stdio;
2
3 import com.tapina.robe.runtime.Environment;
4 import com.tapina.robe.swi.clib.Stub;
5
6 import java.io.IOException;
7
8 /***
9 * This function sets the file position indicator for the file pointed to by a1. The position is set using a
10 * combination of a2 and a3. a2 gives the offset from the position set in a3. a3 should be either SEEK_SET ,
11 * SEEK_CUR or SEEK_END . For files not opened in binary mode, pos should be either zero, or a value returned by ftell
12 * with from equal to SEEK_SET.
13 * Returns: Zero if successful.
14 */
15 public class FSeek extends Stub {
16 public static final int SEEK_SET = 0;
17 public static final int SEEK_CUR = 1;
18 public static final int SEEK_END = 2;
19
20 public void executeStub(Environment environment) {
21 final int[] R = environment.getCpu().R;
22 FilePointer filePointer = FilePointer.find(environment.getMemoryMap().getWord(R[0]));
23 try {
24 switch (R[2]) {
25 case SEEK_SET:
26 filePointer.setPosition(R[1]);
27 break;
28 case SEEK_CUR:
29 filePointer.setPosition(filePointer.getPosition() + R[1]);
30 break;
31 case SEEK_END:
32 filePointer.setPosition(filePointer.getExtent() + R[1]);
33 break;
34 default:
35 log.severe("Unknown fseek(): " + R[2]);
36 R[0] = -1;
37 return;
38 }
39 R[0] = 0;
40 } catch (IOException e) {
41 log.warning("Error during fseek(): " + e.getMessage());
42 R[0] = -1;
43 }
44 }
45 }