DEADSOFTWARE

Mirror gpcp-32255
[gpcp-linux.git] / libs / csharp / GPBinFiles.cs
1 //
2 // Body of GPBinFiles interface.
3 // This file implements the code of the GPBinFiles.cp file.
4 // dwc August 1999. COOL version kjg May 2000
5 // kjg September 2000. Renamed from GPFiles to GPBinFiles.
6 // kjg March 2001. Version for Beta-2 libraries.
7 //
8 // Compile with: csc /t:library /r:GPFiles.dll GPBinFiles.cs
9 //
10 /* ------------------------------------------------------------ */
11 using System;
12 using GPFiles;
14 namespace GPBinFiles {
15 public class GPBinFiles {
17 /* ---------------------------------- */
19 private static System.String mkStr(char[] arr) {
20 int ix = 0;
21 char ch;
22 do {
23 ch = arr[ix]; ix++;
24 } while (ch != '\0');
25 return new System.String(arr,0,ix-1);
26 }
28 /* ---------------------------------- */
30 public static int length(FILE cpf) {
31 return (int) cpf.bufS.Length;
32 }
34 /* ---------------------------------- */
36 private static FILE open(System.String name) {
37 // Opens buffered filestream for reading.
38 try {
39 FILE cpf = new FILE();
40 cpf.path = name;
41 System.IO.FileStream fStr =
42 System.IO.File.Open(name, System.IO.FileMode.Open);
43 cpf.bufS = new System.IO.BufferedStream(fStr);
44 return cpf;
45 } catch {
46 return null;
47 }
48 }
50 private static FILE openRead(System.String name) {
51 // Opens buffered filestream for reading.
52 try {
53 FILE cpf = new FILE();
54 cpf.path = name;
55 System.IO.FileStream fStr = System.IO.File.OpenRead(name);
56 cpf.bufS = new System.IO.BufferedStream(fStr);
57 return cpf;
58 } catch {
59 return null;
60 }
61 }
63 /* =========================== */
65 /* ---------------------------------- */
67 public static FILE findLocal(char[] fileName)
68 {
69 System.String name = mkStr(fileName);
70 return open(name);
71 }
73 /* ---------------------------------- */
75 public static FILE findOnPath(
76 char[] pathName,
77 char[] fileName)
78 {
79 //
80 // Use mkStr, to trim space from end of char arrray.
81 //
82 System.String pName = mkStr(pathName);
83 System.String fName = mkStr(fileName);
84 System.String nName = "";
86 System.String nextDir;
87 System.String thisPath = System.Environment.GetEnvironmentVariable(pName);
88 //
89 // System.Console.WriteLine(pName);
90 // System.Console.WriteLine(thisPath);
91 //
92 FILE cpf = new FILE();
93 bool found = false;
94 bool pathFinished = false;
95 int length = thisPath.Length;
96 int nextLength;
97 int nextPathStart;
98 int nextPathEnd = -1;
100 while (!found && !pathFinished) {
101 nextPathStart = nextPathEnd + 1;
102 nextPathEnd = thisPath.IndexOf(GPFiles.GPFiles.pathSep, nextPathStart);
103 if (nextPathEnd < 0)
104 nextPathEnd = length;
105 nextLength = nextPathEnd - nextPathStart;
106 nextDir = thisPath.Substring(nextPathStart, nextLength);
107 nName = nextDir + GPFiles.GPFiles.fileSep + fName;
108 found = System.IO.File.Exists(nName);
109 pathFinished = nextPathEnd >= length;
110 }
111 if (found) {
112 return openRead(nName);
113 } else
114 return null;
117 /* ---------------------------------- */
119 public static char[] getFullPathName(FILE cpf) {
120 return cpf.path.ToCharArray(); // not really correct!
123 /* ---------------------------------- */
125 public static FILE openFile(char[] fileName) {
126 System.String name = mkStr(fileName);
127 return open(name);
130 public static FILE openFileRO(char[] fileName) {
131 System.String name = mkStr(fileName);
132 return openRead(name);
135 /* ---------------------------------- */
137 public static void CloseFile(FILE cpf) {
138 if (cpf.bufS != null)
139 cpf.bufS.Close();
142 /* ---------------------------------- */
144 public static FILE createFile(char[] arr) {
145 FILE cpf = new FILE();
146 try {
147 System.String name = mkStr(arr);
148 System.IO.FileStream fStr = System.IO.File.Create(name);
149 cpf.path = name;
150 cpf.bufS = new System.IO.BufferedStream(fStr);
151 return cpf;
152 } catch {
153 return null;
155 }
157 /* ---------------------------------- */
159 public static FILE createPath(char[] fileName)
161 System.String fName = mkStr(fileName);
162 try {
163 int ix = fName.LastIndexOf(GPFiles.GPFiles.fileSep);
164 if (ix > 0) {
165 System.String path = fName.Substring(0,ix);
166 // Check if exists first?
167 if (!System.IO.Directory.Exists(path)) {
168 System.IO.DirectoryInfo junk = System.IO.Directory.CreateDirectory(path);
171 FILE cpf = new FILE();
172 cpf.path = fName;
173 System.IO.FileStream fStr = System.IO.File.Create(fName);
174 cpf.bufS = new System.IO.BufferedStream(fStr);
175 return cpf;
176 } catch {
177 return null;
181 /* ---------------------------------- */
183 public static bool EOF(FILE cpf) {
184 return cpf.bufS.Position >= cpf.bufS.Length;
187 /* ---------------------------------- */
189 public static int readByte(FILE cpf) {
190 if (cpf.bufS != null)
191 return (int) cpf.bufS.ReadByte();
192 else
193 throw new System.Exception("File not open for reading");
194 }
196 /* ---------------------------------- */
198 public static int readNBytes(FILE cpf,
199 byte[] buff,
200 int want)
202 if (cpf.bufS != null)
203 return cpf.bufS.Read(buff, 0, want);
204 else
205 throw new System.Exception("File not open for reading");
206 }
208 /* ---------------------------------- */
210 public static void WriteByte(FILE cpf, int b) {
211 if (cpf.bufS != null)
212 cpf.bufS.WriteByte((byte) b);
213 else
214 throw new System.Exception("File not open for reading");
215 }
217 /* ---------------------------------- */
219 public static void WriteNBytes(FILE cpf,
220 byte[] buff,
221 int want)
223 if (cpf.bufS != null)
224 cpf.bufS.Write(buff, 0, want);
225 else
226 throw new System.Exception("File not open for reading");
227 }
228 } // end of class GPBinFiles
230 /* ------------------------------------------------------------ */
231 /* File-descriptor for GPBinFiles */
232 /* ------------------------------------------------------------ */
234 public class FILE : GPFiles.FILE
236 public System.IO.BufferedStream bufS;
237 } // end of class FILE
239 /* ------------------------------------------------------------ */
240 } // end of GPBinFiles.
241 /* ------------------------------------------------------------ */