1 /**********************************************************************/
2 /* J2CPS Files class for J2CPS */
4 /* (c) copyright QUT */
5 /**********************************************************************/
10 public class J2CPSFiles
implements FilenameFilter
{
12 private static final String classExt
= ".class";
13 private static final String symExt
= ".cps";
14 private static final String intExt
= ".cp";
15 private static final String dbName
= "index.dbi";
16 private static final String sepCh
= System
.getProperty("file.separator");
17 private static final char EOF
= '\0';
18 private static final char CR
= '\r';
19 private static final char LF
= '\n';
20 private static final char SP
= ' ';
21 private static final char TAB
= '\t';
22 private static String currDir
= System
.getProperty("user.dir");
23 private static String symDir
;
24 private static String
[] classPath
;
25 private static String
[] symPath
;
26 private static final char pathSep
=
27 System
.getProperty("path.separator").charAt(0);
31 * Method for FilenameFilter
35 public boolean accept (File dir
, String name
) {
36 return name
.endsWith(classExt
);
39 public static void SetSymDir(String sDir
) {
46 public static void GetPaths() {
47 classPath
= GetPath("java.class.path");
48 symPath
= GetPath("CPSYM");
51 private static String
GetPathFromProperty(String str
) {
52 String path
= System
.getProperty(str
);
54 // System.out.println("Property " + str + " = " + path);
58 private static String
GetPathFromEnvVar(String str
) {
59 String path
= System
.getenv(str
);
61 // System.out.println("Env. variable " + str + " = " + path);
65 private static String
[] GetPath(String prop
) {
67 // First look for the system property (preferred source)
68 String cPath
= GetPathFromProperty(prop
);
70 cPath
= GetPathFromEnvVar(prop
);
73 System
.out
.println("No variable for \"" + prop
+ "\", using \".\"");
76 System
.out
.println("Using \"" + prop
+ "\" path \"" + cPath
+ "\"");
78 int i
,count
=1,start
,end
;
79 for (i
=0; i
> -1 ; i
++ ) {
80 i
= cPath
.indexOf(pathSep
,i
);
81 if (i
> -1) { count
++; } else { i
--; }
83 paths
= new String
[count
+1];
86 while (start
< cPath
.length()) {
87 end
= cPath
.indexOf(pathSep
,start
);
89 end
= cPath
.length()+1;
90 paths
[i
] = cPath
.substring(start
);
92 paths
[i
] = cPath
.substring(start
,end
);
94 if (paths
[i
].equals(".")) { paths
[i
] = currDir
; }
101 public static File
getPackageFile(String name
) {
102 File inFile
= new File(currDir
,name
);
103 if (!inFile
.exists()) {
104 boolean found
= false;
105 for (int i
=0; (i
< classPath
.length
) && (!found
); i
++) {
106 if (ClassDesc
.verbose
) {
107 System
.out
.println("<" + classPath
[i
] + sepCh
+ name
+ ">");
109 inFile
= new File(classPath
[i
],name
);
110 found
= inFile
.exists();
113 System
.err
.println("Cannot open class directory <" + name
+ ">");
120 public static File
OpenClassFile(String name
) {
121 if (!name
.endsWith(classExt
)) { name
= name
.concat(classExt
); }
122 File inFile
= new File(currDir
,name
);
123 if (!inFile
.exists()) {
124 inFile
= FindClassFile(name
);
126 if (!inFile
.exists()) {
127 System
.err
.println("Cannot open class file <" + name
+ ">");
134 public static File
OpenClassFile(File dir
, String fName
) {
135 File inFile
= new File(dir
,fName
);
136 if (!inFile
.exists()) {
137 System
.err
.println("Cannot open class file <" + dir
.getName() +
138 sepCh
+ fName
+ ">");
145 public static File
FindClassFile(String name
) {
147 boolean found
= false;
148 if (!name
.endsWith(classExt
)) { name
= name
.concat(classExt
); }
149 for (int i
=0; (i
< classPath
.length
) && (!found
); i
++) {
150 if (ClassDesc
.verbose
) {
151 System
.out
.println("<" + classPath
[i
] + sepCh
+ name
+ ">");
153 inFile
= new File(classPath
[i
],name
);
154 found
= inFile
.exists();
157 System
.err
.println("Cannot open class file <" + name
+ ">");
163 public static File
FindSymbolFile(String name
)
164 throws FileNotFoundException
, IOException
{
166 boolean found
= false;
167 if (!name
.endsWith(symExt
)) { name
= name
.concat(symExt
); }
168 for (int i
=0; (i
< symPath
.length
) && (!found
); i
++) {
169 if (ClassDesc
.verbose
) {
170 System
.out
.println("<" + symPath
[i
] + sepCh
+ name
+ ">");
172 inFile
= new File(symPath
[i
],name
);
173 found
= inFile
.exists();
176 if (ClassDesc
.verbose
)
177 { System
.out
.println("Cannot find symbol file <" + name
+ ">"); }
183 public static DataOutputStream
CreateSymFile(String fileName
)
186 if (symDir
== null) { dirName
= currDir
; } else { dirName
= symDir
; }
187 if (ClassDesc
.verbose
) {
188 System
.out
.println("Creating symbolfile " + fileName
+ symExt
+
189 " in directory " + dirName
);
191 return new DataOutputStream(new FileOutputStream(
192 new File(dirName
,fileName
+ symExt
)));