DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / java / GPBinFiles.java
1 //
2 // Body of GPFiles interface.
3 // This file implements the code of the GPFiles.cp file.
4 // dwc August 1999.
7 package CP.GPBinFiles;
9 import java.io.*;
10 import CP.CPJ.CPJ;
11 import CP.GPFiles.GPFiles.*;
13 public class GPBinFiles {
15 public static int length(GPBinFiles_FILE cpf) {
16 return (int) cpf.length;
17 }
19 public static GPBinFiles_FILE findLocal(char[] fileName)
20 throws IOException {
21 String currDir = System.getProperty("user.dir");
22 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
23 cpf.f = new File(currDir, CP.CPJ.CPJ.MkStr(fileName));
24 if (!cpf.f.exists()) {
25 return null;
26 } else {
27 cpf.rf = new RandomAccessFile(cpf.f,"r");
28 cpf.length = cpf.rf.length();
29 return cpf;
30 }
31 }
33 public static GPBinFiles_FILE findOnPath(char[] pathName,
34 char[] fileName) throws IOException {
35 //
36 // Use MkStr, to trim space from end of char arrray.
37 //
38 String pName = CP.CPJ.CPJ.MkStr(pathName);
39 String fName = CP.CPJ.CPJ.MkStr(fileName);
41 String nextDir;
42 String thisPath = System.getProperty(pName);
43 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
44 boolean found = false;
45 boolean pathFinished = false;
46 int length = thisPath.length();
47 int nextPathStart = -1, nextPathEnd = -1;
49 while (!found && !pathFinished) {
50 nextPathStart = nextPathEnd + 1;
51 nextPathEnd = thisPath.indexOf(CP.GPFiles.GPFiles.pathSep,nextPathStart);
52 if (nextPathEnd < 0)
53 nextPathEnd = length;
54 nextDir = thisPath.substring(nextPathStart,nextPathEnd);
55 cpf.f = new File(nextDir,fName);
56 found = cpf.f.exists();
57 pathFinished = nextPathEnd >= length;
58 }
59 if (found) {
60 cpf.rf = new RandomAccessFile(cpf.f,"r");
61 cpf.length = cpf.rf.length();
62 return cpf;
63 } else {
64 return null;
65 }
66 }
68 public static char[] getFullPathName(GPBinFiles_FILE cpf) {
69 return cpf.f.getPath().toCharArray();
70 }
72 public static GPBinFiles_FILE openFile(char[] fileName)throws IOException{
73 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
74 cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
75 if (!cpf.f.exists()) {
76 return null;
77 } else {
78 cpf.rf = new RandomAccessFile(cpf.f,"rw");
79 cpf.length = cpf.rf.length();
80 return cpf;
81 }
82 }
84 public static GPBinFiles_FILE openFileRO(char[] fileName)throws IOException{
85 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
86 cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
87 if (!cpf.f.exists()) {
88 return null;
89 } else {
90 cpf.rf = new RandomAccessFile(cpf.f,"r");
91 cpf.length = cpf.rf.length();
92 return cpf;
93 }
94 }
96 public static void CloseFile(GPBinFiles_FILE cpf) throws IOException {
97 cpf.rf.close();
98 }
100 public static GPBinFiles_FILE createFile(char[] fileName)throws IOException {
101 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
102 cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
103 cpf.rf = new RandomAccessFile(cpf.f,"rw");
104 cpf.rf.setLength(0);
105 cpf.length = 0;
106 // cpf.length = cpf.rf.length();
107 return cpf;
108 }
110 public static GPBinFiles_FILE createPath(char[] fileName)throws IOException {
111 String fName = CP.CPJ.CPJ.MkStr(fileName);
112 int ix = fName.lastIndexOf(File.separatorChar);
113 if (ix > 0) {
114 File path = new File(fName.substring(0,ix));
115 if (!path.exists()) { boolean ok = path.mkdirs(); }
116 }
117 GPBinFiles_FILE cpf = new GPBinFiles_FILE();
118 cpf.f = new File(fName);
119 cpf.rf = new RandomAccessFile(cpf.f,"rw");
120 cpf.rf.setLength(0);
121 cpf.length = 0;
122 // cpf.length = cpf.rf.length();
123 return cpf;
124 }
126 public static boolean EOF(GPBinFiles_FILE cpf) throws IOException {
127 return cpf.rf.getFilePointer() >= cpf.length;
130 public static int readByte(GPBinFiles_FILE cpf) throws IOException {
131 return cpf.rf.readUnsignedByte();
132 }
134 public static int readNBytes(GPBinFiles_FILE cpf, byte[] buff,
135 int numBytes) throws IOException {
136 return cpf.rf.read(buff,0,numBytes);
137 }
139 public static void WriteByte(GPBinFiles_FILE cpf,int b) throws IOException{
140 cpf.rf.write(b);
141 }
143 public static void WriteNBytes(GPBinFiles_FILE cpf,byte[] buff,
144 int numBytes) throws IOException {
145 cpf.rf.write(buff,0,numBytes);
146 }