GPTextFiles.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Body of GPTextFiles interface.
  3. // This file implements the code of the GPTextFiles.cp file.
  4. // dwc August 1999.
  5. package CP.GPTextFiles;
  6. import java.io.*;
  7. import CP.CPJ.CPJ;
  8. import CP.GPFiles.GPFiles.*;
  9. public class GPTextFiles {
  10. public static GPTextFiles_FILE findLocal(char[] fileName)
  11. throws IOException {
  12. String currDir = System.getProperty("user.dir");
  13. GPTextFiles_FILE cpf = new GPTextFiles_FILE();
  14. cpf.f = new File(currDir, CP.CPJ.CPJ.MkStr(fileName));
  15. if (!cpf.f.exists()) {
  16. return null;
  17. } else {
  18. cpf.r = new BufferedReader(new FileReader(cpf.f));
  19. return cpf;
  20. }
  21. }
  22. public static GPTextFiles_FILE findOnPath(char[] pathName,
  23. char[] fileName) throws IOException {
  24. //
  25. // Use MkStr, to trim space from end of char arrray.
  26. //
  27. String pName = CP.CPJ.CPJ.MkStr(pathName);
  28. String fName = CP.CPJ.CPJ.MkStr(fileName);
  29. String nextDir;
  30. String thisPath = System.getProperty(pName);
  31. GPTextFiles_FILE cpf = new GPTextFiles_FILE();
  32. boolean found = false;
  33. boolean pathFinished = false;
  34. int length = thisPath.length();
  35. int nextPathStart = -1, nextPathEnd = -1;
  36. while (!found && !pathFinished) {
  37. nextPathStart = nextPathEnd + 1;
  38. nextPathEnd = thisPath.indexOf(CP.GPFiles.GPFiles.pathSep,nextPathStart);
  39. if (nextPathEnd < 0)
  40. nextPathEnd = length;
  41. nextDir = thisPath.substring(nextPathStart,nextPathEnd);
  42. cpf.f = new File(nextDir,fName);
  43. found = cpf.f.exists();
  44. pathFinished = nextPathEnd >= length;
  45. }
  46. if (found) {
  47. cpf.r = new BufferedReader(new FileReader(cpf.f));
  48. return cpf;
  49. } else {
  50. return null;
  51. }
  52. }
  53. public static char[] GetFullpathName(GPTextFiles_FILE cpf) {
  54. return cpf.f.getPath().toCharArray();
  55. }
  56. public static GPTextFiles_FILE openFile(char[] fileName)
  57. throws IOException{
  58. GPTextFiles_FILE cpf = new GPTextFiles_FILE();
  59. cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
  60. if (!cpf.f.exists()) {
  61. return null;
  62. } else {
  63. cpf.r = new BufferedReader(new FileReader(cpf.f));
  64. return cpf;
  65. }
  66. }
  67. public static GPTextFiles_FILE openFileRO(char[] fileName)
  68. throws IOException{
  69. return openFile(fileName); // always read only in java?
  70. }
  71. public static void CloseFile(GPTextFiles_FILE cpf) throws IOException {
  72. if (cpf.w != null) { cpf.w.flush(); cpf.w.close();
  73. } else { cpf.r.close(); }
  74. }
  75. public static GPTextFiles_FILE createFile(char[] fileName)
  76. {
  77. try {
  78. GPTextFiles_FILE cpf = new GPTextFiles_FILE();
  79. cpf.f = new File(CP.CPJ.CPJ.MkStr(fileName));
  80. cpf.w = new PrintWriter(new FileWriter(cpf.f));
  81. return cpf;
  82. } catch (IOException e) {
  83. return null;
  84. }
  85. }
  86. public static GPTextFiles_FILE createPath(char[] fileName)
  87. {
  88. try {
  89. String fName = CP.CPJ.CPJ.MkStr(fileName);
  90. int ix = fName.lastIndexOf(File.separatorChar);
  91. if (ix > 0) {
  92. File path = new File(fName.substring(0,ix));
  93. if (!path.exists()) { boolean ok = path.mkdirs(); }
  94. }
  95. GPTextFiles_FILE cpf = new GPTextFiles_FILE();
  96. cpf.f = new File(fName);
  97. cpf.w = new PrintWriter(new FileWriter(cpf.f));
  98. return cpf;
  99. } catch (IOException e) {
  100. return null;
  101. }
  102. }
  103. public static char readChar(GPTextFiles_FILE cpf) throws IOException {
  104. if (cpf.r.ready()) { return (char) cpf.r.read(); }
  105. return (char) 0;
  106. }
  107. public static int readNChars(GPTextFiles_FILE cpf, char[] buff,
  108. int numChars) throws IOException {
  109. return cpf.r.read(buff,0,numChars);
  110. }
  111. public static void WriteChar(GPTextFiles_FILE cpf,char ch)
  112. throws IOException {
  113. cpf.w.write(ch);
  114. }
  115. public static void WriteEOL(GPTextFiles_FILE cpf)
  116. throws IOException {
  117. cpf.w.write('\n');
  118. }
  119. public static void WriteNChars(GPTextFiles_FILE cpf, char[] buff,
  120. int numChars) throws IOException {
  121. cpf.w.write(buff,0,numChars);
  122. }
  123. }