CPJ.java 4.4 KB

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