1 package com.tapina.robe.swi.clib.stdio;
2
3 import com.tapina.robe.runtime.*;
4 import com.tapina.robe.swi.SharedCLibrary;
5 import com.tapina.robe.swi.clib.Stub;
6
7 import java.io.IOException;
8 import java.io.RandomAccessFile;
9
10 /***
11 * This function attempts to open the file specified in a1 for random access according to the mode in a2 .
12 * Returns: Pointer to the stream, or a null pointer if the file cannot be opened.
13 */
14 public class FOpen extends Stub {
15 public void executeStub(Environment environment) {
16 final int[] R = environment.getCpu().R;
17 final MemoryMap memoryMap = environment.getMemoryMap();
18 final String fileName = memoryMap.getString0(R[0]);
19 String mode = memoryMap.getString0(R[1]);
20 mode = mode.replaceFirst("b", "");
21 mode = mode.replaceFirst("w", "rw");
22 RandomAccessFile file;
23 try {
24 file = new RandomAccessFile(FilenameUtils.acornToNative(fileName), mode);
25 final FilePointer filePointer = FilePointer.getInstance(file);
26
27 final RawDataBlock fileDescriptor = memoryMap.createSystemDataBlock(SharedCLibrary.FILE_DESCRIPTOR_SIZE);
28 R[0] = fileDescriptor.getAddress();
29 ByteArrayUtils.putInt(fileDescriptor.getBytes(), 0, filePointer.getHandle());
30 } catch (IOException e) {
31 log.warning("fopen() error: " + e.getMessage());
32 R[0] = 0;
33 } catch (IllegalArgumentException e) {
34 log.warning("fopen() error: " + e.getMessage());
35 R[0] = 0;
36 }
37 }
38 }