DEADSOFTWARE

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