GPBinFiles.java 4.6 KB

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