2
0

J2CPSFiles.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) John Gough 2016-2017
  3. */
  4. package j2cpsfiles;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.FileOutputStream;
  8. import java.io.DataOutputStream;
  9. import java.io.File;
  10. /**
  11. *
  12. * @author john
  13. */
  14. public class j2cpsfiles /*implements FilenameFilter*/ {
  15. private static final String CLASSEXT = ".class";
  16. private static final String SYMFILEXT = ".cps";
  17. // private static final String CPEXT = ".cp";
  18. // private static final String dbName = "index.dbi";
  19. private static final char EOF = '\0';
  20. private static final char CR = '\r';
  21. private static final char LF = '\n';
  22. private static final char SP = ' ';
  23. private static final char TAB = '\t';
  24. private static final String CURRDIR =
  25. System.getProperty("user.dir");
  26. private static final String FILESEP =
  27. System.getProperty("file.separator");
  28. private static final String PATHSEPSTRING =
  29. System.getProperty("path.separator");
  30. private static final char PATHSEP =
  31. PATHSEPSTRING.charAt(0);
  32. /**
  33. * Destination directory for symbol files.
  34. */
  35. private static String dstDir = ".";
  36. /**
  37. * The installation root directory.
  38. */
  39. private static String rootName = ".";
  40. /**
  41. * The root of the package class-file tree.
  42. */
  43. private static File pkgRoot = new File(CURRDIR);
  44. private static String[] classPath;
  45. /**
  46. * Locations to search for symbol files.
  47. */
  48. private static String[] symPath;
  49. private static boolean verbose = false;
  50. private static boolean summary = false;
  51. public static void SetVerbose( boolean v ) { verbose = v; }
  52. public static void SetSummary( boolean v ) { summary = v; }
  53. public static void SetDstDir(String sDir) {
  54. if (!sDir.equals("."))
  55. dstDir = "." + FILESEP + sDir;
  56. }
  57. public static void SetPackageRootDir(String rDir) {
  58. rootName = "." + FILESEP + rDir;
  59. pkgRoot = new File(CURRDIR, rDir);
  60. }
  61. /**
  62. * This method is called after all arguments have been parsed.
  63. */
  64. public static void GetPaths(boolean ignoreCpsym) {
  65. if (summary) {
  66. System.out.printf("Current directory \".\" is <%s>\n", CURRDIR);
  67. if (!rootName.equals("."))
  68. System.out.printf(
  69. "Using <%s> as package-root directory\n", rootName);
  70. if (!dstDir.equals("."))
  71. System.out.printf("Using <%s> as symbol destination directory\n", dstDir);
  72. }
  73. classPath = GetPathArray("java.class.path");
  74. if (ignoreCpsym) {
  75. symPath = new String[] { dstDir };
  76. } else {
  77. String[] tmp = GetPathArray("CPSYM");
  78. symPath = new String[tmp.length + 1];
  79. symPath[0] = dstDir;
  80. for (int i = 0; i < tmp.length; i++)
  81. symPath[i+1] = tmp[i];
  82. }
  83. }
  84. private static String GetPathFromProperty(String str) {
  85. String path = System.getProperty(str);
  86. return path;
  87. }
  88. private static String GetPathFromEnvVar(String str) {
  89. String path = System.getenv(str);
  90. return path;
  91. }
  92. private static String[] GetPathArray(String prop) {
  93. // First look for the system property (preferred source)
  94. String cPath = GetPathFromProperty(prop);
  95. if (cPath == null)
  96. cPath = GetPathFromEnvVar(prop);
  97. if (cPath == null) {
  98. System.err.println("No variable for \"" + prop + "\", using \".\"");
  99. cPath = ".";
  100. } else if (summary)
  101. System.out.println("Using \"" + prop + "\" path \"" + cPath + "\"");
  102. String[] splits = cPath.split(PATHSEPSTRING);
  103. return splits;
  104. }
  105. public static File getPackageFile(String name) {
  106. File inFile = new File(pkgRoot,name);
  107. if (!inFile.exists()) {
  108. boolean found = false;
  109. for (int i=0; (i < classPath.length) && (!found); i++) {
  110. if (verbose) {
  111. System.out.println("<" + classPath[i] + FILESEP + name + ">");
  112. }
  113. inFile = new File(classPath[i],name);
  114. found = inFile.exists();
  115. }
  116. if (!found) {
  117. System.err.println(
  118. "Cannot open package directory <" + name + ">, quitting");
  119. //
  120. // Is this too severe?
  121. //
  122. System.exit(0);
  123. }
  124. } else {
  125. System.out.print("INFO: opened package directory <" + name + ">");
  126. if (summary)
  127. System.out.print(" from package-root <" + rootName + ">");
  128. System.out.println();
  129. }
  130. return inFile;
  131. }
  132. public static File OpenClassFile(String name) {
  133. if (!name.endsWith(CLASSEXT)) { name = name.concat(CLASSEXT); }
  134. File inFile = new File(CURRDIR,name);
  135. if (!inFile.exists()) {
  136. inFile = FindClassFile(name);
  137. }
  138. if (!inFile.exists()) {
  139. System.err.println("Cannot open class file <" + name + ">");
  140. System.exit(0);
  141. }
  142. return inFile;
  143. }
  144. public static File OpenClassFile(File dir, String fName) {
  145. File inFile = new File(dir,fName);
  146. if (!inFile.exists()) {
  147. System.err.println("Cannot open class file <" + dir.getName() +
  148. FILESEP + fName + ">");
  149. System.exit(0);
  150. }
  151. return inFile;
  152. }
  153. public static File FindClassFile(String name) {
  154. File inFile = null;
  155. boolean found = false;
  156. if (!name.endsWith(CLASSEXT)) { name = name.concat(CLASSEXT); }
  157. for (int i=0; (i < classPath.length) && (!found); i++) {
  158. if (verbose) {
  159. System.out.println("<" + classPath[i] + FILESEP + name + ">");
  160. }
  161. inFile = new File(classPath[i],name);
  162. found = inFile.exists();
  163. }
  164. if (!found) {
  165. System.err.println("Cannot open class file <" + name + ">");
  166. System.exit(1);
  167. }
  168. return inFile;
  169. }
  170. public static File FindSymbolFile(String name)
  171. throws FileNotFoundException, IOException {
  172. File inFile = null;
  173. boolean found = false;
  174. if (!name.endsWith(SYMFILEXT)) {
  175. name = name.concat(SYMFILEXT);
  176. }
  177. for (int i=0; (i < symPath.length) && (!found); i++) {
  178. if (verbose) {
  179. System.out.println("Seeking <" + symPath[i] + FILESEP + name + ">");
  180. }
  181. inFile = new File(symPath[i],name);
  182. found = inFile.exists();
  183. }
  184. if (!found) {
  185. if (verbose) {
  186. System.out.println("Cannot find symbol file <" + name + ">");
  187. }
  188. return null;
  189. } else {
  190. //char[] arr = inFile.getPath().toCharArray();
  191. return inFile;
  192. }
  193. }
  194. public static DataOutputStream CreateSymFile(String fileName)
  195. throws IOException {
  196. String dirName = (dstDir == null ? CURRDIR : dstDir);
  197. System.out.print("INFO: Creating symbolfile <" + fileName + SYMFILEXT + ">");
  198. if (summary)
  199. System.out.print(" in directory <" + dirName + ">");
  200. System.out.println();
  201. return new DataOutputStream(new FileOutputStream(
  202. new File(dirName,fileName + SYMFILEXT)));
  203. }
  204. }