package erjang.m.prim_file;

import java.util.ArrayList;
import java.util.List;

import erjang.BIF;
import erjang.EBinary;
import erjang.ENative;
import erjang.EObject;
import erjang.ERT;
import erjang.ESeq;
import erjang.EString;

public class Native extends ENative
{
	@BIF
	public static EObject internal_name2native(EObject arg) {
		if (arg.testAtom() != null) {
			throw ERT.badarg(arg);
		}
		else if (arg.testBinary() != null) {
			EBinary bin = arg.testBinary();
			byte[] binbytes = bin.getByteArray();
			byte[] outbytes = new byte[binbytes.length + 1];
			
			System.arraycopy(binbytes, 0, outbytes, 0, binbytes.length);
			outbytes[outbytes.length - 1] = 0x0;
			
			return new EBinary(outbytes);
		}
		else {
			ESeq input = arg.testSeq();
			List<Byte> bytes = new ArrayList<Byte>();
			byte[] out;
			
			while (!input.isNil()) {
				Integer el = input.head().testSmall().intValue();
				bytes.add(el.byteValue());
				input = input.tail();
			}
			out = new byte[bytes.size() + 1];
			for (int i = 0; i < bytes.size(); ++i) {
				out[i] = bytes.get(i);
			}
			out[out.length - 1] = 0x0;
			
			return new EBinary(out);
		}
	}
	
	@BIF
	public static EObject internal_native2name(EObject arg) {
		EBinary bin = arg.testBinary();
		byte[] binbytes;
		
		if (bin == null) {
			throw ERT.badarg(arg);
		}
		binbytes = bin.getByteArray();
		String out = new String(binbytes);
		
		return new EString(out);
	}
}

@@ -223,16 +223,41 @@
 EString;
+%0Aimport erjang.driver.IO;
 %0A%0Apublic
@@ -290,24 +290,298 @@
 ive%0A%7B%0A%09@BIF%0A
+%09public static EObject is_translatable(EObject arg) %7B%0A%09%09EBinary bin = arg.testBinary();%0A%09%09if (bin == null) throw ERT.badarg(arg);%0A%09%09%0A%09%09try %7B%0A%09%09%09new String(bin.getByteArray(), IO.UTF8);%0A%09%09%09return ERT.TRUE;%0A%09%09%7D catch (RuntimeException e) %7B%0A%09%09%09return ERT.FALSE;%0A%09%09%7D%0A%09%7D%0A%09%0A%09@BIF%0A
 %09public stat

Add prim_file:is_translatable/1 BIF