CPJrts.java 8.8 KB

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