DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / csharp / GPTextFiles.cs
1 /* ------------------------------------------------------------ */
2 //
3 // Body of GPTextFiles interface.
4 // This file implements the code of the GPTextFiles.cp file.
5 // dwc August 1999, C# version May 2000, kjg.
6 // dwc May 2001. Version for Beta-2 libraries.
7 //
8 // Compile with : csc /t:library /r:GPFiles.dll GPTextFiles.cs
9 //
10 /* ------------------------------------------------------------ */
11 using System;
12 using GPFiles;
14 /* ------------------------------------------------------------ */
16 namespace GPTextFiles {
17 public class GPTextFiles
18 {
19 /* ---------------------------------- */
21 private static System.String mkStr(char[] arr) {
22 int ix = 0;
23 char ch;
24 do {
25 ch = arr[ix]; ix++;
26 } while (ch != '\0');
27 return new System.String(arr,0,ix-1);
28 }
30 /* ---------------------------------- */
32 private static FILE open(System.String name) {
33 try {
34 FILE cpf = new FILE();
35 cpf.path = name;
36 System.IO.FileStream fStr =
37 System.IO.File.Open(name, System.IO.FileMode.Open);
38 cpf.strR = new System.IO.StreamReader(fStr);
39 return cpf;
40 } catch {
41 return null;
42 }
43 }
45 private static FILE openRead(System.String name) {
46 try {
47 FILE cpf = new FILE();
48 cpf.path = name;
49 System.IO.FileStream fStr = System.IO.File.OpenRead(name);
50 cpf.strR = new System.IO.StreamReader(fStr);
51 return cpf;
52 } catch {
53 return null;
54 }
55 }
57 /* ---------------------------------- */
59 public static FILE findLocal(char[] fileName)
60 {
61 return open(mkStr(fileName));
62 }
64 /* ---------------------------------- */
66 public static FILE findOnPath(
67 char[] pathName, char[] fileName)
68 {
69 //
70 // Use mkStr, to trim space from end of char arrray.
71 //
72 System.String pName = mkStr(pathName);
73 System.String fName = mkStr(fileName);
74 System.String nName = "";
76 System.String nextDir;
77 System.String thisPath = System.Environment.GetEnvironmentVariable(pName);
78 //
79 // System.Console.WriteLine(pName);
80 // System.Console.WriteLine(thisPath);
81 //
82 FILE cpf = new FILE();
83 bool found = false;
84 bool pathFinished = false;
85 int length = thisPath.Length;
86 int nextLength;
87 int nextPathStart;
88 int nextPathEnd = -1;
90 while (!found && !pathFinished) {
91 nextPathStart = nextPathEnd + 1;
92 nextPathEnd = thisPath.IndexOf(GPFiles.GPFiles.pathSep, nextPathStart);
93 if (nextPathEnd < 0)
94 nextPathEnd = length;
95 nextLength = nextPathEnd - nextPathStart;
96 nextDir = thisPath.Substring(nextPathStart, nextLength);
97 nName = nextDir + GPFiles.GPFiles.fileSep + fName;
98 found = System.IO.File.Exists(nName);
99 pathFinished = nextPathEnd >= length;
100 }
101 if (found) {
102 return open(nName);
103 } else
104 return null;
107 /* ---------------------------------- */
109 public static char[] getFullPathName(FILE cpf) {
110 return cpf.path.ToCharArray();
113 /* ---------------------------------- */
115 public static FILE openFile(char[] fileName)
117 return open(mkStr(fileName));
120 /* ---------------------------------- */
122 public static FILE openFileRO(char[] fileName)
124 return openRead(mkStr(fileName));
127 /* ---------------------------------- */
129 public static void CloseFile(FILE cpf)
131 if (cpf.strW != null) {
132 cpf.strW.Close(); // Close does automatic Flush()
134 if (cpf.strR != null) {
135 cpf.strR.Close();
139 /* ---------------------------------- */
141 public static FILE createFile(char[] fileName)
143 FILE cpf = new FILE();
144 try {
145 System.String name = mkStr(fileName);
146 System.IO.FileStream fStr = System.IO.File.Create(name);
147 cpf.path = name;
148 cpf.strW = new System.IO.StreamWriter(fStr);
149 return cpf;
150 } catch {
151 return null;
153 }
155 /* ---------------------------------- */
157 public static FILE createPath(char[] fileName)
159 System.String fName = mkStr(fileName);
160 try {
161 int ix = fName.LastIndexOf(GPFiles.GPFiles.fileSep);
162 if (ix > 0) {
163 System.String path = fName.Substring(0,ix);
164 if (!System.IO.Directory.Exists(path)) {
165 System.IO.DirectoryInfo junk = System.IO.Directory.CreateDirectory(path);
168 FILE cpf = new FILE();
169 cpf.path = fName;
170 System.IO.FileStream fStr = System.IO.File.Create(fName);
171 cpf.strW = new System.IO.StreamWriter(fStr);
172 return cpf;
173 } catch {
174 return null;
178 /* ---------------------------------- */
180 public static char readChar(FILE cpf)
182 if (cpf.strR != null) {
183 int chr = cpf.strR.Read();
184 if (chr == -1)
185 return (char) 0;
186 else
187 return (char) chr;
188 } else
189 throw new System.Exception("File not open for reading");
190 }
192 /* ---------------------------------- */
194 public static int readNChars(
195 FILE cpf,
196 char[] buff,
197 int want)
199 return cpf.strR.Read(buff,0,want);
200 }
202 /* ---------------------------------- */
204 public static void WriteChar(FILE cpf, char ch)
206 if (cpf.strW != null) {
207 cpf.strW.Write(ch);
208 } else
209 throw new System.Exception("File not open for writing");
210 }
212 /* ---------------------------------- */
214 public static void WriteEOL(FILE cpf)
216 if (cpf.strW != null) {
217 cpf.strW.Write(Environment.NewLine);
218 } else
219 throw new System.Exception("File not open for writing");
220 }
222 /* ---------------------------------- */
224 public static void WriteNChars(
225 FILE cpf,
226 char[] buff,
227 int want)
229 if (cpf.strW != null) {
230 cpf.strW.Write(buff, 0, want);
231 } else
232 throw new System.Exception("File not open for writing");
233 }
234 } // end of class GPTextFiles
236 /* ------------------------------------------------------------ */
237 /* File-descriptor for GPTextFiles */
238 /* ------------------------------------------------------------ */
240 public class FILE : GPFiles.FILE
242 public System.IO.StreamReader strR;
243 public System.IO.StreamWriter strW;
244 } // end of class FILE
246 /* ------------------------------------------------------------ */
247 } // end of GPTextFiles.
248 /* ------------------------------------------------------------ */