CPJrts.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if (input == null)
  99. return null;
  100. int len = input.length();
  101. char[] str = new char[len+1];
  102. input.getChars(0, len, str, 0);
  103. str[len] = '\0';
  104. return str;
  105. }
  106. /* -------------------------------------------------------------------- */
  107. public static void JavaStrToFixChr(char[] out, String in)
  108. {
  109. int len = in.length();
  110. in.getChars(0, len, out, 0);
  111. out[len] = '\0';
  112. }
  113. /* -------------------------------------------------------------------- */
  114. public static String FixChToJavaStr(char[] arr)
  115. {
  116. if (arr == null) return null;
  117. //
  118. // This truncation makes semantics same as .NET version
  119. //
  120. int len = ChrArrLength(arr);
  121. return new String(arr, 0, len);
  122. }
  123. /* -------------------------------------------------------------------- */
  124. public static void ChrArrStrCopy(char[] dst, char[] src)
  125. {
  126. int ix = 0;
  127. char ch;
  128. do {
  129. ch = src[ix];
  130. dst[ix] = ch;
  131. ix++;
  132. } while (ch != '\0');
  133. }
  134. /* -------------------------------------------------------------------- */
  135. public static void ChrArrCheck(char[] src)
  136. {
  137. int ix = 0;
  138. char ch;
  139. do {
  140. ch = src[ix];
  141. if (ch > 0xFF) throw new Error("SHORT on array error");
  142. ix++;
  143. } while (ch != '\0');
  144. }
  145. /* -------------------------------------------------------------------- */
  146. public static int strCmp(char[] l, char[] r)
  147. {
  148. for (int ix = 0; ix < l.length && ix < r.length; ix++) {
  149. if (l[ix] < r[ix]) return -1;
  150. else if (l[ix] > r[ix]) return 1;
  151. else if (l[ix] == '\0') return 0;
  152. }
  153. if (l.length < r.length) return -1;
  154. else if (l.length < r.length) return 1;
  155. else return 0;
  156. }
  157. /* ==================================================================== *
  158. * Class reflection helper methods *
  159. * ==================================================================== */
  160. static final int boolN = 1;
  161. static final int sChrN = 2;
  162. static final int charN = 3;
  163. static final int byteN = 4;
  164. static final int sIntN = 5;
  165. static final int intN = 6;
  166. static final int lIntN = 7;
  167. static final int sReaN = 8;
  168. static final int realN = 9;
  169. static final int setN = 10;
  170. static final int anyRec = 11;
  171. static final int anyPtr = 12;
  172. static final int strN = 13;
  173. static final int sStrN = 14;
  174. static final int uBytN = 15;
  175. static final int metaN = 16;
  176. public static Class getClassByName(String name) {
  177. try {
  178. return Class.forName(name);
  179. } catch(Exception e) {
  180. System.out.println("CPJrts.getClassByName: " + e.toString());
  181. return null;
  182. }
  183. }
  184. public static Class getClassByOrd(int ord) {
  185. switch (ord) {
  186. case boolN: return Boolean.TYPE;
  187. case uBytN:
  188. case byteN:
  189. case sChrN: return Byte.TYPE;
  190. case charN: return Character.TYPE;
  191. case sIntN: return Short.TYPE;
  192. case setN:
  193. case intN: return Integer.TYPE;
  194. case lIntN: return Long.TYPE;
  195. case sReaN: return Float.TYPE;
  196. case realN: return Double.TYPE;
  197. case anyRec:
  198. case anyPtr: return getClassByName("java.lang.Object");
  199. case strN: return getClassByName("java.lang.String");
  200. case sStrN: return getClassByName("java.lang.String");
  201. case metaN: return getClassByName("java.lang.Class");
  202. default: return null;
  203. }
  204. }
  205. /* ==================================================================== *
  206. * Procedure variable reflection helper method *
  207. * ==================================================================== */
  208. public static Method getMth(String mod, String prc)
  209. {
  210. Class mCls = null;
  211. Method[] mths = null;
  212. try {
  213. mCls = Class.forName(mod);
  214. mths = mCls.getDeclaredMethods();
  215. for (int i = 0; i < mths.length; i++) {
  216. if (mths[i].getName().equals(prc))
  217. return mths[i];
  218. }
  219. return null;
  220. } catch(Exception e) {
  221. System.out.println("CPJrts.getMth: " + e.toString());
  222. return null;
  223. }
  224. }
  225. /* ==================================================================== *
  226. * String concatenation helper methods *
  227. * ==================================================================== */
  228. public static String ArrArrToString(char[] l, char[] r)
  229. {
  230. int llen = ChrArrLength(l);
  231. int rlen = ChrArrLength(r);
  232. StringBuffer buff = new StringBuffer(llen + rlen);
  233. return buff.append(l,0,llen).append(r,0,rlen).toString();
  234. }
  235. public static String ArrStrToString(char[] l, String r)
  236. {
  237. int llen = ChrArrLength(l);
  238. StringBuffer buff = new StringBuffer(3 * llen);
  239. return buff.append(l,0,llen).append(r).toString();
  240. }
  241. public static String StrArrToString(String l, char[] r)
  242. {
  243. int rlen = ChrArrLength(r);
  244. StringBuffer buff = new StringBuffer(3 * rlen);
  245. return buff.append(l).append(r,0,rlen).toString();
  246. }
  247. public static String StrStrToString(String l, String r)
  248. {
  249. StringBuffer buff = new StringBuffer(l);
  250. return buff.append(r).toString();
  251. }
  252. }