DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / java / CPJ.java
2 /** This is part of the body of the GPCP runtime support.
3 *
4 * Written November 1998, John Gough.
5 *
6 * CPJ and CPJrts contain the runtime helpers, these classes have
7 * most of the adapters for hooking into the various Java libraries.
8 * RTS.java has the user-accessible facilities of the runtime. The
9 * facilities in CPJrts are known to the compiler, but have no
10 * CP-accessible functions.
11 *
12 * There is a swindle involved here, for the bootstrap version
13 * of the compiler: any functions with OUT scalars will have
14 * a different signature in the old and new versions. This
15 * module implements both, by overloading the methods.
16 * There is also the method for simulating an Exec.
17 */
19 package CP.CPJ;
21 import java.io.*;
23 /* ------------------------------------------------------------ */
24 /* Support for CPJ.cp */
25 /* ------------------------------------------------------------ */
27 class CopyThread extends Thread
28 { //
29 // This is a crude adapter to connect two streams together.
30 // One use of this class is to connect the output and input
31 // threads of an forked-ed process to the standard input and
32 // output streams of the parent process.
33 //
34 InputStream in;
35 OutputStream out;
37 CopyThread(InputStream i, OutputStream o) {
38 in = i; out = o;
39 }
41 public void run() {
42 try {
43 for (int ch = in.read(); ch != -1; ch = in.read()) {
44 out.write(ch);
45 }
46 } catch(Exception e) {
47 return;
48 }
49 }
50 }
52 /* ------------------------------------------------------------ */
54 public final class CPJ
55 {
57 public static final String newLn = "\n";
59 public static String MkStr(char[] arr)
60 {
61 for (int i = 0; i < arr.length; i++) {
62 if (arr[i] == '\0')
63 return new String(arr, 0, i);
64 }
65 return null;
66 }
68 public static void MkArr(String str, char[] arr)
69 {
70 if (str == null) {
71 arr[0] = '\0'; return;
72 }
73 int len = str.length();
74 if (len >= arr.length)
75 len = arr.length - 1;
76 str.getChars(0, len, arr, 0);
77 arr[len] = '\0';
78 }
80 public static String JCat(String l, String r)
81 {
82 return l+r;
83 }
85 public static String GetProperty(String key)
86 {
87 return System.getProperty(key);
88 }
90 // OBSOLETE 2011 ?
91 /** Java compiler version */
92 public static void StrToReal(String str,
93 double[] o, // OUT param
94 boolean[] r) // OUT param
95 {
96 try {
97 o[0] = Double.valueOf(str.trim()).doubleValue();
98 r[0] = true;
99 } catch(Exception e) {
100 r[0] = false;
104 // OBSOLETE 2011 ?
105 /** Component Pascal compiler version */
106 public static double StrToReal(String str,
107 boolean[] r) // OUT param
109 try {
110 r[0] = true;
111 return Double.valueOf(str.trim()).doubleValue();
112 } catch(Exception e) {
113 r[0] = false;
114 return 0.0;
118 // OBSOLETE 2011 ?
119 /** Java compiler version */
120 public static void StrToInt(String str,
121 int[] o, // OUT param
122 boolean[] r) // OUT param
124 try {
125 o[0] = Integer.parseInt(str.trim());
126 r[0] = true;
127 } catch(Exception e) {
128 r[0] = false;
132 // OBSOLETE 2011 ?
133 /** Component Pascal compiler version */
134 public static int StrToInt(String str,
135 boolean[] r) // OUT param
137 try {
138 r[0] = true;
139 return Integer.parseInt(str.trim());
140 } catch(Exception e) {
141 r[0] = false;
142 return 0;
147 public static int ExecResult(String[] args)
149 try {
150 Process p = Runtime.getRuntime().exec(args);
151 CopyThread cOut = new CopyThread(p.getInputStream(), System.out);
152 cOut.start();
153 CopyThread cErr = new CopyThread(p.getErrorStream(), System.err);
154 cErr.start();
155 CopyThread cIn = new CopyThread(System.in, p.getOutputStream());
156 cIn.start();
157 return p.waitFor();
158 } catch(Exception e) {
159 System.err.println(e.toString());
160 return 1;
164 /* ------------------------------------------------------------ */
166 public static void DiagProperties()
168 System.getProperties().list(System.out);
171 public static void DiagClass(Object o)
173 System.out.print(o.getClass().getName());
177 /* ------------------------------------------------------------ */
178 /* ------------------------------------------------------------ */
179 /* ------------------------------------------------------------ */