J2CPSFiles.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**********************************************************************/
  2. /* J2CPS Files class for J2CPS */
  3. /* */
  4. /* (c) copyright QUT */
  5. /**********************************************************************/
  6. package J2CPS;
  7. import java.io.*;
  8. public class J2CPSFiles implements FilenameFilter {
  9. private static final String classExt = ".class";
  10. private static final String symExt = ".cps";
  11. private static final String intExt = ".cp";
  12. private static final String dbName = "index.dbi";
  13. private static final String sepCh = System.getProperty("file.separator");
  14. private static final char EOF = '\0';
  15. private static final char CR = '\r';
  16. private static final char LF = '\n';
  17. private static final char SP = ' ';
  18. private static final char TAB = '\t';
  19. private static String currDir = System.getProperty("user.dir");
  20. private static String symDir;
  21. private static String[] classPath;
  22. private static String[] symPath;
  23. private static final char pathSep =
  24. System.getProperty("path.separator").charAt(0);
  25. /*
  26. * Method for FilenameFilter
  27. */
  28. @Override
  29. public boolean accept (File dir, String name) {
  30. return name.endsWith(classExt);
  31. }
  32. public static void SetSymDir(String sDir) {
  33. symDir = sDir;
  34. if (symDir == null) {
  35. symDir = symPath[0];
  36. }
  37. }
  38. public static void GetPaths() {
  39. classPath = GetPath("java.class.path");
  40. symPath = GetPath("CPSYM");
  41. }
  42. private static String GetPathFromProperty(String str) {
  43. String path = System.getProperty(str);
  44. //if (path != null)
  45. // System.out.println("Property " + str + " = " + path);
  46. return path;
  47. }
  48. private static String GetPathFromEnvVar(String str) {
  49. String path = System.getenv(str);
  50. //if (path != null)
  51. // System.out.println("Env. variable " + str + " = " + path);
  52. return path;
  53. }
  54. private static String[] GetPath(String prop) {
  55. String paths[];
  56. // First look for the system property (preferred source)
  57. String cPath = GetPathFromProperty(prop);
  58. if (cPath == null)
  59. cPath = GetPathFromEnvVar(prop);
  60. if (cPath == null) {
  61. System.out.println("No variable for \"" + prop + "\", using \".\"");
  62. cPath = ".";
  63. } else
  64. System.out.println("Using \"" + prop + "\" path \"" + cPath + "\"");
  65. int i,count=1,start,end;
  66. for (i=0; i > -1 ; i++ ) {
  67. i = cPath.indexOf(pathSep,i);
  68. if (i > -1) { count++; } else { i--; }
  69. }
  70. paths = new String[count+1];
  71. paths[0] = currDir;
  72. start = 0; i = 1;
  73. while (start < cPath.length()) {
  74. end = cPath.indexOf(pathSep,start);
  75. if (end == -1) {
  76. end = cPath.length()+1;
  77. paths[i] = cPath.substring(start);
  78. } else {
  79. paths[i] = cPath.substring(start,end);
  80. }
  81. if (paths[i].equals(".")) { paths[i] = currDir; }
  82. i++;
  83. start = end+1;
  84. }
  85. return paths;
  86. }
  87. public static File getPackageFile(String name) {
  88. File inFile = new File(currDir,name);
  89. if (!inFile.exists()) {
  90. boolean found = false;
  91. for (int i=0; (i < classPath.length) && (!found); i++) {
  92. if (ClassDesc.verbose) {
  93. System.out.println("<" + classPath[i] + sepCh + name + ">");
  94. }
  95. inFile = new File(classPath[i],name);
  96. found = inFile.exists();
  97. }
  98. if (!found) {
  99. System.err.println("Cannot open class directory <" + name + ">");
  100. System.exit(0);
  101. }
  102. }
  103. return inFile;
  104. }
  105. public static File OpenClassFile(String name) {
  106. if (!name.endsWith(classExt)) { name = name.concat(classExt); }
  107. File inFile = new File(currDir,name);
  108. if (!inFile.exists()) {
  109. inFile = FindClassFile(name);
  110. }
  111. if (!inFile.exists()) {
  112. System.err.println("Cannot open class file <" + name + ">");
  113. System.exit(0);
  114. }
  115. return inFile;
  116. }
  117. public static File OpenClassFile(File dir, String fName) {
  118. File inFile = new File(dir,fName);
  119. if (!inFile.exists()) {
  120. System.err.println("Cannot open class file <" + dir.getName() +
  121. sepCh + fName + ">");
  122. System.exit(0);
  123. }
  124. return inFile;
  125. }
  126. public static File FindClassFile(String name) {
  127. File inFile = null;
  128. boolean found = false;
  129. if (!name.endsWith(classExt)) { name = name.concat(classExt); }
  130. for (int i=0; (i < classPath.length) && (!found); i++) {
  131. if (ClassDesc.verbose) {
  132. System.out.println("<" + classPath[i] + sepCh + name + ">");
  133. }
  134. inFile = new File(classPath[i],name);
  135. found = inFile.exists();
  136. }
  137. if (!found) {
  138. System.err.println("Cannot open class file <" + name + ">");
  139. System.exit(0);
  140. }
  141. return inFile;
  142. }
  143. public static File FindSymbolFile(String name)
  144. throws FileNotFoundException, IOException {
  145. File inFile = null;
  146. boolean found = false;
  147. if (!name.endsWith(symExt)) { name = name.concat(symExt); }
  148. for (int i=0; (i < symPath.length) && (!found); i++) {
  149. if (ClassDesc.verbose) {
  150. System.out.println("<" + symPath[i] + sepCh + name + ">");
  151. }
  152. inFile = new File(symPath[i],name);
  153. found = inFile.exists();
  154. }
  155. if (!found) {
  156. if (ClassDesc.verbose)
  157. { System.out.println("Cannot find symbol file <" + name + ">"); }
  158. return null;
  159. }
  160. return inFile;
  161. }
  162. public static DataOutputStream CreateSymFile(String fileName)
  163. throws IOException {
  164. String dirName;
  165. if (symDir == null) { dirName = currDir; } else { dirName = symDir; }
  166. if (ClassDesc.verbose) {
  167. System.out.println("Creating symbolfile " + fileName + symExt +
  168. " in directory " + dirName);
  169. }
  170. return new DataOutputStream(new FileOutputStream(
  171. new File(dirName,fileName + symExt)));
  172. }
  173. }