2
0

GPBinFiles.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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;
  13. namespace GPBinFiles {
  14. public class GPBinFiles {
  15. /* ---------------------------------- */
  16. private static System.String mkStr(char[] arr) {
  17. int ix = 0;
  18. char ch;
  19. do {
  20. ch = arr[ix]; ix++;
  21. } while (ch != '\0');
  22. return new System.String(arr,0,ix-1);
  23. }
  24. /* ---------------------------------- */
  25. public static int length(FILE cpf) {
  26. return (int) cpf.bufS.Length;
  27. }
  28. /* ---------------------------------- */
  29. private static FILE open(System.String name) {
  30. // Opens buffered filestream for reading.
  31. try {
  32. FILE cpf = new FILE();
  33. cpf.path = name;
  34. System.IO.FileStream fStr =
  35. System.IO.File.Open(name, System.IO.FileMode.Open);
  36. cpf.bufS = new System.IO.BufferedStream(fStr);
  37. return cpf;
  38. } catch {
  39. return null;
  40. }
  41. }
  42. private static FILE openRead(System.String name) {
  43. // Opens buffered filestream for reading.
  44. try {
  45. FILE cpf = new FILE();
  46. cpf.path = name;
  47. System.IO.FileStream fStr = System.IO.File.OpenRead(name);
  48. cpf.bufS = new System.IO.BufferedStream(fStr);
  49. return cpf;
  50. } catch {
  51. return null;
  52. }
  53. }
  54. /* =========================== */
  55. /* ---------------------------------- */
  56. public static FILE findLocal(char[] fileName)
  57. {
  58. System.String name = mkStr(fileName);
  59. return open(name);
  60. }
  61. /* ---------------------------------- */
  62. public static FILE findOnPath(
  63. char[] pathName,
  64. char[] fileName)
  65. {
  66. //
  67. // Use mkStr, to trim space from end of char arrray.
  68. //
  69. System.String pName = mkStr(pathName);
  70. System.String fName = mkStr(fileName);
  71. System.String nName = "";
  72. System.String nextDir;
  73. System.String thisPath = System.Environment.GetEnvironmentVariable(pName);
  74. //
  75. // System.Console.WriteLine(pName);
  76. // System.Console.WriteLine(thisPath);
  77. //
  78. FILE cpf = new FILE();
  79. bool found = false;
  80. bool pathFinished = false;
  81. int length = thisPath.Length;
  82. int nextLength;
  83. int nextPathStart;
  84. int nextPathEnd = -1;
  85. while (!found && !pathFinished) {
  86. nextPathStart = nextPathEnd + 1;
  87. nextPathEnd = thisPath.IndexOf(GPFiles.GPFiles.pathSep, nextPathStart);
  88. if (nextPathEnd < 0)
  89. nextPathEnd = length;
  90. nextLength = nextPathEnd - nextPathStart;
  91. nextDir = thisPath.Substring(nextPathStart, nextLength);
  92. nName = nextDir + GPFiles.GPFiles.fileSep + fName;
  93. found = System.IO.File.Exists(nName);
  94. pathFinished = nextPathEnd >= length;
  95. }
  96. if (found) {
  97. return openRead(nName);
  98. } else
  99. return null;
  100. }
  101. /* ---------------------------------- */
  102. public static char[] getFullPathName(FILE cpf) {
  103. return cpf.path.ToCharArray(); // not really correct!
  104. }
  105. /* ---------------------------------- */
  106. public static FILE openFile(char[] fileName) {
  107. System.String name = mkStr(fileName);
  108. return open(name);
  109. }
  110. public static FILE openFileRO(char[] fileName) {
  111. System.String name = mkStr(fileName);
  112. return openRead(name);
  113. }
  114. /* ---------------------------------- */
  115. public static void CloseFile(FILE cpf) {
  116. if (cpf.bufS != null)
  117. cpf.bufS.Close();
  118. }
  119. /* ---------------------------------- */
  120. public static FILE createFile(char[] arr) {
  121. FILE cpf = new FILE();
  122. try {
  123. System.String name = mkStr(arr);
  124. System.IO.FileStream fStr = System.IO.File.Create(name);
  125. cpf.path = name;
  126. cpf.bufS = new System.IO.BufferedStream(fStr);
  127. return cpf;
  128. } catch {
  129. return null;
  130. }
  131. }
  132. /* ---------------------------------- */
  133. public static FILE createPath(char[] fileName)
  134. {
  135. System.String fName = mkStr(fileName);
  136. try {
  137. int ix = fName.LastIndexOf(GPFiles.GPFiles.fileSep);
  138. if (ix > 0) {
  139. System.String path = fName.Substring(0,ix);
  140. // Check if exists first?
  141. if (!System.IO.Directory.Exists(path)) {
  142. System.IO.DirectoryInfo junk = System.IO.Directory.CreateDirectory(path);
  143. }
  144. }
  145. FILE cpf = new FILE();
  146. cpf.path = fName;
  147. System.IO.FileStream fStr = System.IO.File.Create(fName);
  148. cpf.bufS = new System.IO.BufferedStream(fStr);
  149. return cpf;
  150. } catch {
  151. return null;
  152. }
  153. }
  154. /* ---------------------------------- */
  155. public static bool EOF(FILE cpf) {
  156. return cpf.bufS.Position >= cpf.bufS.Length;
  157. }
  158. /* ---------------------------------- */
  159. public static int readByte(FILE cpf) {
  160. if (cpf.bufS != null)
  161. return (int) cpf.bufS.ReadByte();
  162. else
  163. throw new System.Exception("File not open for reading");
  164. }
  165. /* ---------------------------------- */
  166. public static int readNBytes(FILE cpf,
  167. byte[] buff,
  168. int want)
  169. {
  170. if (cpf.bufS != null)
  171. return cpf.bufS.Read(buff, 0, want);
  172. else
  173. throw new System.Exception("File not open for reading");
  174. }
  175. /* ---------------------------------- */
  176. public static void WriteByte(FILE cpf, int b) {
  177. if (cpf.bufS != null)
  178. cpf.bufS.WriteByte((byte) b);
  179. else
  180. throw new System.Exception("File not open for reading");
  181. }
  182. /* ---------------------------------- */
  183. public static void WriteNBytes(FILE cpf,
  184. byte[] buff,
  185. int want)
  186. {
  187. if (cpf.bufS != null)
  188. cpf.bufS.Write(buff, 0, want);
  189. else
  190. throw new System.Exception("File not open for reading");
  191. }
  192. } // end of class GPBinFiles
  193. /* ------------------------------------------------------------ */
  194. /* File-descriptor for GPBinFiles */
  195. /* ------------------------------------------------------------ */
  196. public class FILE : GPFiles.FILE
  197. {
  198. public System.IO.BufferedStream bufS;
  199. } // end of class FILE
  200. /* ------------------------------------------------------------ */
  201. } // end of GPBinFiles.
  202. /* ------------------------------------------------------------ */