DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / java / GPTextFiles.java
1 //
2 // Body of GPTextFiles interface.
3 // This file implements the code of the GPTextFiles.cp file.
4 // dwc August 1999.
7 package CP.GPTextFiles;
9 import java.io.*;
10 import CP.CPJ.CPJ;
11 import CP.GPFiles.GPFiles.*;
13 public class GPTextFiles {
16 public static GPTextFiles_FILE findLocal(char[] fileName)
17 throws IOException {
18 String currDir = System.getProperty("user.dir");
19 GPTextFiles_FILE cpf = new GPTextFiles_FILE();
20 cpf.f = new File(currDir, CP.CPJ.CPJ.MkStr(fileName));
21 if (!cpf.f.exists()) {
22 return null;
23 } else {
24 cpf.r = new BufferedReader(new FileReader(cpf.f));
25 return cpf;
26 }
27 }
29 public static GPTextFiles_FILE findOnPath(char[] pathName,
30 char[] fileName) throws IOException {
31 //
32 // Use MkStr, to trim space from end of char arrray.
33 //
34 String pName = CP.CPJ.CPJ.MkStr(pathName);
35 String fName = CP.CPJ.CPJ.MkStr(fileName);
37 String nextDir;
38 String thisPath = System.getProperty(pName);
39 GPTextFiles_FILE cpf = new GPTextFiles_FILE();
40 boolean found = false;
41 boolean pathFinished = false;
42 int length = thisPath.length();
43 int nextPathStart = -1, nextPathEnd = -1;
45 while (!found && !pathFinished) {
46 nextPathStart = nextPathEnd + 1;
47 nextPathEnd = thisPath.indexOf(CP.GPFiles.GPFiles.pathSep,nextPathStart);
48 if (nextPathEnd < 0)
49 nextPathEnd = length;
50 nextDir = thisPath.substring(nextPathStart,nextPathEnd);
51 cpf.f = new File(nextDir,fName);
52 found = cpf.f.exists();
53 pathFinished = nextPathEnd >= length;
54 }
55 if (found) {
56 cpf.r = new BufferedReader(new FileReader(cpf.f));
57 return cpf;
58 } else {
59 return null;
60 }
61 }
64 public static char[] GetFullpathName(GPTextFiles_FILE cpf) {
65 return cpf.f.getPath().toCharArray();
66 }
68 public static GPTextFiles_FILE openFile(char[] fileName)
69 throws IOException{
70 GPTextFiles_FILE cpf = new GPTextFiles_FILE();
71 cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
72 if (!cpf.f.exists()) {
73 return null;
74 } else {
75 cpf.r = new BufferedReader(new FileReader(cpf.f));
76 return cpf;
77 }
78 }
80 public static GPTextFiles_FILE openFileRO(char[] fileName)
81 throws IOException{
82 return openFile(fileName); // always read only in java?
83 }
85 public static void CloseFile(GPTextFiles_FILE cpf) throws IOException {
86 if (cpf.w != null) { cpf.w.flush(); cpf.w.close();
87 } else { cpf.r.close(); }
88 }
90 public static GPTextFiles_FILE createFile(char[] fileName)
91 {
92 try {
93 GPTextFiles_FILE cpf = new GPTextFiles_FILE();
94 cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
95 cpf.w = new PrintWriter(new FileWriter(cpf.f));
96 return cpf;
97 } catch (IOException e) {
98 return null;
99 }
100 }
102 public static GPTextFiles_FILE createPath(char[] fileName)
104 try {
105 String fName = CP.CPJ.CPJ.MkStr(fileName);
106 int ix = fName.lastIndexOf(File.separatorChar);
107 if (ix > 0) {
108 File path = new File(fName.substring(0,ix));
109 if (!path.exists()) { boolean ok = path.mkdirs(); }
111 GPTextFiles_FILE cpf = new GPTextFiles_FILE();
112 cpf.f = new File(fName);
113 cpf.w = new PrintWriter(new FileWriter(cpf.f));
114 return cpf;
115 } catch (IOException e) {
116 return null;
118 }
120 public static char readChar(GPTextFiles_FILE cpf) throws IOException {
121 if (cpf.r.ready()) { return (char) cpf.r.read(); }
122 return (char) 0;
123 }
125 public static int readNChars(GPTextFiles_FILE cpf, char[] buff,
126 int numChars) throws IOException {
127 return cpf.r.read(buff,0,numChars);
128 }
130 public static void WriteChar(GPTextFiles_FILE cpf,char ch)
131 throws IOException {
132 cpf.w.write(ch);
133 }
135 public static void WriteEOL(GPTextFiles_FILE cpf)
136 throws IOException {
137 cpf.w.write('\n');
138 }
140 public static void WriteNChars(GPTextFiles_FILE cpf, char[] buff,
141 int numChars) throws IOException {
142 cpf.w.write(buff,0,numChars);
143 }