GPBinFiles.java 4.5 KB

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