123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /** This is the body of the GPCP runtime support.
- *
- * Written November 1998, John Gough.
- *
- *
- *
- */
- package CP.CPJrts;
- import java.lang.reflect.*;
- public class CPJrts
- {
- /* ==================================================================== *
- * MOD and DIV helpers. With correction factors *
- * ==================================================================== */
- public static int CpModI(int lVal, int rVal)
- {
- // A correction is required if the signs of
- // the two operands are different, but the
- // remainder is non-zero. Inc rem by rVal.
- int rslt = lVal % rVal;
- if ((lVal < 0 != rVal < 0) && (rslt != 0))
- rslt += rVal;
- return rslt;
- }
- public static int CpDivI(int lVal, int rVal)
- {
- // A correction is required if the signs of
- // the two operands are different, but the
- // remainder is non-zero. Dec quo by 1.
- int rslt = lVal / rVal;
- int remV = lVal % rVal;
- if ((lVal < 0 != rVal < 0) && (remV != 0))
- rslt--;
- return rslt;
- }
- public static long CpModL(long lVal, long rVal)
- {
- // A correction is required if the signs of
- // the two operands are different, but the
- // remainder is non-zero. Inc rem by rVal.
- long rslt = lVal % rVal;
- if ((lVal < 0 != rVal < 0) && (rslt != 0))
- rslt += rVal;
- return rslt;
- }
- public static long CpDivL(long lVal, long rVal)
- {
- // A correction is required if the signs of
- // the two operands are different, but the
- // remainder is non-zero. Dec quo by 1.
- long rslt = lVal / rVal;
- long remV = lVal % rVal;
- if ((lVal < 0 != rVal < 0) && (remV != 0))
- rslt--;
- return rslt;
- }
- /* ==================================================================== *
- * Various string and char-array helpers *
- * ==================================================================== */
- public static String CaseMesg(int i)
- {
- String s = "CASE-trap: selector = " + i;
- return s;
- }
- /* -------------------------------------------------------------------- */
- public static String WithMesg(Object o)
- {
- String c = o.getClass().getName();
- c = c.substring(c.lastIndexOf('.') + 1);
- c = "WITH else-trap: type = " + c;
- return c;
- }
- /* -------------------------------------------------------------------- */
- public static int ChrArrLength(char[] src)
- {
- int ix = 0;
- char ch;
- do {
- ch = src[ix];
- ix++;
- } while ((ch != '\0') && (ix < src.length));
- return ix-1;
- }
- /* -------------------------------------------------------------------- */
- public static int ChrArrLplus1(char[] src)
- {
- int ix = 0;
- char ch;
- do {
- ch = src[ix];
- ix++;
- } while (ch != '\0');
- return ix;
- }
- /* -------------------------------------------------------------------- */
- public static char[] JavaStrToChrOpen(String input)
- {
- if (input == null)
- return null;
- int len = input.length();
- char[] str = new char[len+1];
- input.getChars(0, len, str, 0);
- str[len] = '\0';
- return str;
- }
- /* -------------------------------------------------------------------- */
- public static void JavaStrToFixChr(char[] out, String in)
- {
- int len = in.length();
- in.getChars(0, len, out, 0);
- out[len] = '\0';
- }
- /* -------------------------------------------------------------------- */
- public static String FixChToJavaStr(char[] arr)
- {
- if (arr == null) return null;
- //
- // This truncation makes semantics same as .NET version
- //
- int len = ChrArrLength(arr);
- return new String(arr, 0, len);
- }
- /* -------------------------------------------------------------------- */
- public static void ChrArrStrCopy(char[] dst, char[] src)
- {
- int ix = 0;
- char ch;
- do {
- ch = src[ix];
- dst[ix] = ch;
- ix++;
- } while (ch != '\0');
- }
- /* -------------------------------------------------------------------- */
- public static void ChrArrCheck(char[] src)
- {
- int ix = 0;
- char ch;
- do {
- ch = src[ix];
- if (ch > 0xFF) throw new Error("SHORT on array error");
- ix++;
- } while (ch != '\0');
- }
- /* -------------------------------------------------------------------- */
- public static int strCmp(char[] l, char[] r)
- {
- for (int ix = 0; ix < l.length && ix < r.length; ix++) {
- if (l[ix] < r[ix]) return -1;
- else if (l[ix] > r[ix]) return 1;
- else if (l[ix] == '\0') return 0;
- }
- if (l.length < r.length) return -1;
- else if (l.length < r.length) return 1;
- else return 0;
- }
- /* ==================================================================== *
- * Class reflection helper methods *
- * ==================================================================== */
- static final int boolN = 1;
- static final int sChrN = 2;
- static final int charN = 3;
- static final int byteN = 4;
- static final int sIntN = 5;
- static final int intN = 6;
- static final int lIntN = 7;
- static final int sReaN = 8;
- static final int realN = 9;
- static final int setN = 10;
- static final int anyRec = 11;
- static final int anyPtr = 12;
- static final int strN = 13;
- static final int sStrN = 14;
- static final int uBytN = 15;
- static final int metaN = 16;
- public static Class getClassByName(String name) {
- try {
- return Class.forName(name);
- } catch(Exception e) {
- System.out.println("CPJrts.getClassByName: " + e.toString());
- return null;
- }
- }
- public static Class getClassByOrd(int ord) {
- switch (ord) {
- case boolN: return Boolean.TYPE;
- case uBytN:
- case byteN:
- case sChrN: return Byte.TYPE;
- case charN: return Character.TYPE;
- case sIntN: return Short.TYPE;
- case setN:
- case intN: return Integer.TYPE;
- case lIntN: return Long.TYPE;
- case sReaN: return Float.TYPE;
- case realN: return Double.TYPE;
- case anyRec:
- case anyPtr: return getClassByName("java.lang.Object");
- case strN: return getClassByName("java.lang.String");
- case sStrN: return getClassByName("java.lang.String");
- case metaN: return getClassByName("java.lang.Class");
- default: return null;
- }
- }
- /* ==================================================================== *
- * Procedure variable reflection helper method *
- * ==================================================================== */
- public static Method getMth(String mod, String prc)
- {
- Class mCls = null;
- Method[] mths = null;
- try {
- mCls = Class.forName(mod);
- mths = mCls.getDeclaredMethods();
- for (int i = 0; i < mths.length; i++) {
- if (mths[i].getName().equals(prc))
- return mths[i];
- }
- return null;
- } catch(Exception e) {
- System.out.println("CPJrts.getMth: " + e.toString());
- return null;
- }
- }
- /* ==================================================================== *
- * String concatenation helper methods *
- * ==================================================================== */
- public static String ArrArrToString(char[] l, char[] r)
- {
- int llen = ChrArrLength(l);
- int rlen = ChrArrLength(r);
- StringBuffer buff = new StringBuffer(llen + rlen);
- return buff.append(l,0,llen).append(r,0,rlen).toString();
- }
- public static String ArrStrToString(char[] l, String r)
- {
- int llen = ChrArrLength(l);
- StringBuffer buff = new StringBuffer(3 * llen);
- return buff.append(l,0,llen).append(r).toString();
- }
- public static String StrArrToString(String l, char[] r)
- {
- int rlen = ChrArrLength(r);
- StringBuffer buff = new StringBuffer(3 * rlen);
- return buff.append(l).append(r,0,rlen).toString();
- }
- public static String StrStrToString(String l, String r)
- {
- StringBuffer buff = new StringBuffer(l);
- return buff.append(r).toString();
- }
- }
|