View Javadoc

1   package com.tapina.robe.swi.clib.string;
2   
3   import com.tapina.robe.runtime.ByteArray;
4   import com.tapina.robe.runtime.Environment;
5   import com.tapina.robe.runtime.MemoryMap;
6   import com.tapina.robe.swi.clib.Stub;
7   
8   import java.io.UnsupportedEncodingException;
9   
10  /***
11   * This function copies up to a3 characters from a2 to a1. If a null character is reached,
12   * the remaining characters are filled with nulls. str1 and str2 should not overlap.
13   * Returns: a1
14   */
15  public class StrNCpy extends Stub {
16      public void executeStub(Environment environment) {
17          final int[] R = environment.getCpu().R;
18          final MemoryMap memoryMap = environment.getMemoryMap();
19  
20          final int count = R[2];
21          final String s = memoryMap.getStringN(R[1], count);
22          final ByteArray out = memoryMap.getByteArray(R[0], count, true);
23          try {
24              final byte[] bytes = s.getBytes("ISO-8859-1");
25              System.arraycopy(bytes, 0, out.getArray(), out.getOffset(), bytes.length);
26              for (int i = bytes.length; i < count; i++) {
27                  out.set(i, (byte) 0);
28              }
29          } catch (UnsupportedEncodingException e) {
30              throw new RuntimeException(e);
31          }
32      }
33  }