CPJrts.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /** This is the body of the GPCP runtime support.
  2. *
  3. * Written November 1998, John Gough.
  4. *
  5. *
  6. *
  7. */
  8. package CP.CPJrts;
  9. import java.lang.reflect.*;
  10. public class CPJrts
  11. {
  12. /* ==================================================================== *
  13. * MOD and DIV helpers. With correction factors *
  14. * ==================================================================== */
  15. public static int CpModI(int lVal, int rVal)
  16. {
  17. // A correction is required if the signs of
  18. // the two operands are different, but the
  19. // remainder is non-zero. Inc rem by rVal.
  20. int rslt = lVal % rVal;
  21. if ((lVal < 0 != rVal < 0) && (rslt != 0))
  22. rslt += rVal;
  23. return rslt;
  24. }
  25. public static int CpDivI(int lVal, int rVal)
  26. {
  27. // A correction is required if the signs of
  28. // the two operands are different, but the
  29. // remainder is non-zero. Dec quo by 1.
  30. int rslt = lVal / rVal;
  31. int remV = lVal % rVal;
  32. if ((lVal < 0 != rVal < 0) && (remV != 0))
  33. rslt--;
  34. return rslt;
  35. }
  36. public static long CpModL(long lVal, long rVal)
  37. {
  38. // A correction is required if the signs of
  39. // the two operands are different, but the
  40. // remainder is non-zero. Inc rem by rVal.
  41. long rslt = lVal % rVal;
  42. if ((lVal < 0 != rVal < 0) && (rslt != 0))
  43. rslt += rVal;
  44. return rslt;
  45. }
  46. public static long CpDivL(long lVal, long rVal)
  47. {
  48. // A correction is required if the signs of
  49. // the two operands are different, but the
  50. // remainder is non-zero. Dec quo by 1.
  51. long rslt = lVal / rVal;
  52. long remV = lVal % rVal;
  53. if ((lVal < 0 != rVal < 0) && (remV != 0))
  54. rslt--;
  55. return rslt;
  56. }
  57. /* ==================================================================== *
  58. * Various string and char-array helpers *
  59. * ==================================================================== */
  60. public static String CaseMesg(int i)
  61. {
  62. String s = "CASE-trap: selector = " + i;
  63. return s;
  64. }
  65. /* -------------------------------------------------------------------- */
  66. public static String WithMesg(Object o)
  67. {
  68. String c = o.getClass().getName();
  69. c = c.substring(c.lastIndexOf('.') + 1);
  70. c = "WITH else-trap: type = " + c;
  71. return c;
  72. }
  73. /* -------------------------------------------------------------------- */
  74. public static int ChrArrLength(char[] src)
  75. {
  76. int ix = 0;
  77. char ch;
  78. do {
  79. ch = src[ix];
  80. ix++;
  81. } while ((ch != '\0') && (ix < src.length));
  82. return ix-1;
  83. }
  84. /* -------------------------------------------------------------------- */
  85. public static int ChrArrLplus1(char[] src)
  86. {
  87. int ix = 0;
  88. char ch;
  89. do {
  90. ch = src[ix];
  91. ix++;
  92. } while (ch != '\0');
  93. return ix;
  94. }
  95. /* -------------------------------------------------------------------- */
  96. public static char[] JavaStrToChrOpen(String input)
  97. {
  98. int len = input.length();
  99. char[] str = new char[len+1];
  100. input.getChars(0, len, str, 0);
  101. str[len] = '\0';
  102. return str;
  103. }
  104. /* -------------------------------------------------------------------- */
  105. public static void JavaStrToFixChr(char[] out, String in)
  106. {
  107. int len = in.length();
  108. in.getChars(0, len, out, 0);
  109. out[len] = '\0';
  110. }
  111. /* -------------------------------------------------------------------- */
  112. public static String FixChToJavaStr(char[] arr)
  113. {
  114. // This truncation makes semantics same as .NET version
  115. int len = ChrArrLength(arr);
  116. return new String(arr, 0, len);
  117. }
  118. /* -------------------------------------------------------------------- */
  119. public static void ChrArrStrCopy(char[] dst, char[] src)
  120. {
  121. int ix = 0;
  122. char ch;
  123. do {
  124. ch = src[ix];
  125. dst[ix] = ch;
  126. ix++;
  127. } while (ch != '\0');
  128. }
  129. /* -------------------------------------------------------------------- */
  130. public static void ChrArrCheck(char[] src)
  131. {
  132. int ix = 0;
  133. char ch;
  134. do {
  135. ch = src[ix];
  136. if (ch > 0xFF) throw new Error("SHORT on array error");
  137. ix++;
  138. } while (ch != '\0');
  139. }
  140. /* -------------------------------------------------------------------- */
  141. public static int strCmp(char[] l, char[] r)
  142. {
  143. for (int ix = 0; ix < l.length && ix < r.length; ix++) {
  144. if (l[ix] < r[ix]) return -1;
  145. else if (l[ix] > r[ix]) return 1;
  146. else if (l[ix] == '\0') return 0;
  147. }
  148. if (l.length < r.length) return -1;
  149. else if (l.length < r.length) return 1;
  150. else return 0;
  151. }
  152. /* ==================================================================== *
  153. * Class reflection helper methods *
  154. * ==================================================================== */
  155. static final int boolN = 1;
  156. static final int sChrN = 2;
  157. static final int charN = 3;
  158. static final int byteN = 4;
  159. static final int sIntN = 5;
  160. static final int intN = 6;
  161. static final int lIntN = 7;
  162. static final int sReaN = 8;
  163. static final int realN = 9;
  164. static final int setN = 10;
  165. static final int anyRec = 11;
  166. static final int anyPtr = 12;
  167. static final int strN = 13;
  168. static final int sStrN = 14;
  169. static final int uBytN = 15;
  170. static final int metaN = 16;
  171. public static Class getClassByName(String name) {
  172. try {
  173. return Class.forName(name);
  174. } catch(Exception e) {
  175. System.out.println("CPJrts.getClassByName: " + e.toString());
  176. return null;
  177. }
  178. }
  179. public static Class getClassByOrd(int ord) {
  180. switch (ord) {
  181. case boolN: return Boolean.TYPE;
  182. case uBytN:
  183. case byteN:
  184. case sChrN: return Byte.TYPE;
  185. case charN: return Character.TYPE;
  186. case sIntN: return Short.TYPE;
  187. case setN:
  188. case intN: return Integer.TYPE;
  189. case lIntN: return Long.TYPE;
  190. case sReaN: return Float.TYPE;
  191. case realN: return Double.TYPE;
  192. case anyRec:
  193. case anyPtr: return getClassByName("java.lang.Object");
  194. case strN: return getClassByName("java.lang.String");
  195. case sStrN: return getClassByName("java.lang.String");
  196. case metaN: return getClassByName("java.lang.Class");
  197. default: return null;
  198. }
  199. }
  200. /* ==================================================================== *
  201. * Procedure variable reflection helper method *
  202. * ==================================================================== */
  203. public static Method getMth(String mod, String prc)
  204. {
  205. Class mCls = null;
  206. Method[] mths = null;
  207. try {
  208. mCls = Class.forName(mod);
  209. mths = mCls.getDeclaredMethods();
  210. for (int i = 0; i < mths.length; i++) {
  211. if (mths[i].getName().equals(prc))
  212. return mths[i];
  213. }
  214. return null;
  215. } catch(Exception e) {
  216. System.out.println("CPJrts.getMth: " + e.toString());
  217. return null;
  218. }
  219. }
  220. /* ==================================================================== *
  221. * String concatenation helper methods *
  222. * ==================================================================== */
  223. public static String ArrArrToString(char[] l, char[] r)
  224. {
  225. int llen = ChrArrLength(l);
  226. int rlen = ChrArrLength(r);
  227. StringBuffer buff = new StringBuffer(llen + rlen);
  228. return buff.append(l,0,llen).append(r,0,rlen).toString();
  229. }
  230. public static String ArrStrToString(char[] l, String r)
  231. {
  232. int llen = ChrArrLength(l);
  233. StringBuffer buff = new StringBuffer(3 * llen);
  234. return buff.append(l,0,llen).append(r).toString();
  235. }
  236. public static String StrArrToString(String l, char[] r)
  237. {
  238. int rlen = ChrArrLength(r);
  239. StringBuffer buff = new StringBuffer(3 * rlen);
  240. return buff.append(l).append(r,0,rlen).toString();
  241. }
  242. public static String StrStrToString(String l, String r)
  243. {
  244. StringBuffer buff = new StringBuffer(l);
  245. return buff.append(r).toString();
  246. }
  247. }