GPTextFiles.cs 6.4 KB

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