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.CLibrary;
6 import com.tapina.robe.swi.clib.Stub;
7
8 import java.io.IOException;
9
10 /***
11 * This function reads input from the stream in a1 under control of the format string in a2.
12 * Each subsequent argument is a pointer to a object in which to place the result.
13 * If there are more arguments than conversion specifications, the extra arguments are evaluated but not used.
14 * Returns: Number of successful inputs, or EOF if there was an input error.
15 */
16 public class FScanF extends Stub {
17 public void executeStub(Environment environment) {
18 final int[] R = environment.getCpu().R;
19 final MemoryMap memoryMap = environment.getMemoryMap();
20 final FilePointer filePointer = FilePointer.find(memoryMap.getWord(R[0]));
21 final String formatString = memoryMap.getString0(R[1]);
22 if (!formatString.equals("%s")) {
23 log.severe("Only scanf with %s is supported");
24 R[0] = CLibrary.EOF;
25 } else {
26 StringBuffer result = new StringBuffer();
27 try {
28 do {
29 char c = (char) filePointer.getc();
30 if (Character.isWhitespace(c)) {
31 filePointer.ungetc(c);
32 break;
33 }
34 result.append(c);
35 } while (true);
36 memoryMap.storeString0(R[2], result.toString());
37 R[0] = 1;
38 } catch (IOException e) {
39 log.warning("Error during scanf(): " + e.getMessage());
40 R[0] = CLibrary.EOF;
41 }
42 }
43 }
44 }