2 // Body of GPTextFiles interface.
3 // This file implements the code of the GPTextFiles.cp file.
7 package CP
.GPTextFiles
;
11 import CP
.GPFiles
.GPFiles
.*;
13 public class GPTextFiles
{
16 public static GPTextFiles_FILE
findLocal(char[] fileName
)
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()) {
24 cpf
.r
= new BufferedReader(new FileReader(cpf
.f
));
29 public static GPTextFiles_FILE
findOnPath(char[] pathName
,
30 char[] fileName
) throws IOException
{
32 // Use MkStr, to trim space from end of char arrray.
34 String pName
= CP
.CPJ
.CPJ
.MkStr(pathName
);
35 String fName
= CP
.CPJ
.CPJ
.MkStr(fileName
);
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
);
50 nextDir
= thisPath
.substring(nextPathStart
,nextPathEnd
);
51 cpf
.f
= new File(nextDir
,fName
);
52 found
= cpf
.f
.exists();
53 pathFinished
= nextPathEnd
>= length
;
56 cpf
.r
= new BufferedReader(new FileReader(cpf
.f
));
64 public static char[] GetFullpathName(GPTextFiles_FILE cpf
) {
65 return cpf
.f
.getPath().toCharArray();
68 public static GPTextFiles_FILE
openFile(char[] fileName
)
70 GPTextFiles_FILE cpf
= new GPTextFiles_FILE();
71 cpf
.f
= new File(CP
.CPJ
.CPJ
.MkStr(fileName
));
72 if (!cpf
.f
.exists()) {
75 cpf
.r
= new BufferedReader(new FileReader(cpf
.f
));
80 public static GPTextFiles_FILE
openFileRO(char[] fileName
)
82 return openFile(fileName
); // always read only in java?
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(); }
90 public static GPTextFiles_FILE
createFile(char[] fileName
)
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
));
97 } catch (IOException e
) {
102 public static GPTextFiles_FILE
createPath(char[] fileName
)
105 String fName
= CP
.CPJ
.CPJ
.MkStr(fileName
);
106 int ix
= fName
.lastIndexOf(File
.separatorChar
);
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
));
115 } catch (IOException e
) {
120 public static char readChar(GPTextFiles_FILE cpf
) throws IOException
{
121 if (cpf
.r
.ready()) { return (char) cpf
.r
.read(); }
125 public static int readNChars(GPTextFiles_FILE cpf
, char[] buff
,
126 int numChars
) throws IOException
{
127 return cpf
.r
.read(buff
,0,numChars
);
130 public static void WriteChar(GPTextFiles_FILE cpf
,char ch
)
135 public static void WriteEOL(GPTextFiles_FILE cpf
)
140 public static void WriteNChars(GPTextFiles_FILE cpf
, char[] buff
,
141 int numChars
) throws IOException
{
142 cpf
.w
.write(buff
,0,numChars
);