//\r
package com.stevesoft.pat.wrap;\r
\r
-import com.stevesoft.pat.*;\r
import java.io.*;\r
\r
+import com.stevesoft.pat.*;\r
+\r
/** Provides a wrapper for a RandomAccessFile so that it\r
can be searched by Regex. */\r
-public class RandomAccessFileWrap implements StringLike {\r
+public class RandomAccessFileWrap\r
+ implements StringLike\r
+{\r
\r
- long offset = 0;\r
- public void setOffset(long o) {\r
- offset = o;\r
- i0 = iend = 0;\r
- }\r
- public long getOffset() {\r
- return offset;\r
- }\r
- RandomAccessFile raf;\r
- int i0=0,iend=0;\r
- byte[] buf = new byte[1024];\r
+ long offset = 0;\r
+ public void setOffset(long o)\r
+ {\r
+ offset = o;\r
+ i0 = iend = 0;\r
+ }\r
\r
- public int getBufferSize() {\r
- return buf.length;\r
- }\r
+ public long getOffset()\r
+ {\r
+ return offset;\r
+ }\r
\r
- public void setBufferSize(int bs) {\r
- buf = new byte[bs];\r
- i0 = iend = 0;\r
- }\r
+ RandomAccessFile raf;\r
+ int i0 = 0, iend = 0;\r
+ byte[] buf = new byte[1024];\r
\r
- public RandomAccessFileWrap(String file) throws IOException {\r
- this.raf = new RandomAccessFile(file,"r");\r
- }\r
- public RandomAccessFileWrap(RandomAccessFile raf) {\r
- this.raf = raf;\r
+ public int getBufferSize()\r
+ {\r
+ return buf.length;\r
+ }\r
+\r
+ public void setBufferSize(int bs)\r
+ {\r
+ buf = new byte[bs];\r
+ i0 = iend = 0;\r
+ }\r
+\r
+ public RandomAccessFileWrap(String file)\r
+ throws IOException\r
+ {\r
+ this.raf = new RandomAccessFile(file, "r");\r
+ }\r
+\r
+ public RandomAccessFileWrap(RandomAccessFile raf)\r
+ {\r
+ this.raf = raf;\r
+ }\r
+\r
+ public char charAt(int i)\r
+ {\r
+ if (i >= i0 && i < iend)\r
+ {\r
+ return (char) buf[i - i0];\r
}\r
\r
- public char charAt(int i) {\r
- if(i >= i0 && i < iend)\r
- return (char)buf[i-i0];\r
-\r
- try {\r
- i0 = i-5;\r
- //if(i0+offset<0) i0=(int)(-offset);\r
- if(i0<0) i0=0;\r
- raf.seek(i0+offset);\r
- iend = i0+raf.read(buf,0,buf.length);\r
-\r
- if(i >= i0 && i < iend)\r
- return (char)buf[i-i0];\r
- } catch(Throwable t) {}\r
-\r
- throw new ArrayIndexOutOfBoundsException("Out of bounds for file:"+\r
- " i="+i+\r
- ", Final Buffer: i0="+i0+\r
- " iend="+iend);\r
+ try\r
+ {\r
+ i0 = i - 5;\r
+ //if(i0+offset<0) i0=(int)(-offset);\r
+ if (i0 < 0)\r
+ {\r
+ i0 = 0;\r
+ }\r
+ raf.seek(i0 + offset);\r
+ iend = i0 + raf.read(buf, 0, buf.length);\r
+\r
+ if (i >= i0 && i < iend)\r
+ {\r
+ return (char) buf[i - i0];\r
+ }\r
}\r
+ catch (Throwable t)\r
+ {}\r
\r
- public String toString() { throw new Error("Not implemented"); }\r
- public int length() {\r
- try {\r
- long len = raf.length()-offset;\r
- if(len > Integer.MAX_VALUE)\r
- return Integer.MAX_VALUE;\r
- return (int)len;\r
- } catch(IOException ioe) {\r
- return 0;\r
+ throw new ArrayIndexOutOfBoundsException("Out of bounds for file:" +\r
+ " i=" + i +\r
+ ", Final Buffer: i0=" + i0 +\r
+ " iend=" + iend);\r
+ }\r
+\r
+ public String toString()\r
+ {\r
+ throw new Error("Not implemented");\r
+ }\r
+\r
+ public int length()\r
+ {\r
+ try\r
+ {\r
+ long len = raf.length() - offset;\r
+ if (len > Integer.MAX_VALUE)\r
+ {\r
+ return Integer.MAX_VALUE;\r
}\r
+ return (int) len;\r
}\r
- public String substring(int i1,int i2) {\r
- StringBuffer sb = new StringBuffer();\r
- for(int i=i1;i<i2;i++)\r
- sb.append(charAt(i));\r
- return sb.toString();\r
+ catch (IOException ioe)\r
+ {\r
+ return 0;\r
}\r
- public Object unwrap() { return raf; }\r
-\r
- public static void main(String[] files) throws IOException {\r
- for(int i=0;i<files.length;i++) {\r
- RandomAccessFileWrap fw =\r
- new RandomAccessFileWrap(new RandomAccessFile(files[i],"r"));\r
- Regex r = new Regex("toString\\(\\) *(?@{})");\r
- r.setGFlag(true);\r
- r.optimize();\r
- System.out.print(files[i]+" ");\r
- int j=0;\r
- do {\r
- if(r.searchFrom(fw,j)) {\r
- System.out.println("Matched at index: "+\r
- r.matchedFrom());\r
- j=r.matchedTo();\r
- } else\r
- System.out.println("not found");\r
- System.out.println(r.stringMatched());\r
- } while(r.didMatch());\r
- }\r
+ }\r
+\r
+ public String substring(int i1, int i2)\r
+ {\r
+ StringBuffer sb = new StringBuffer();\r
+ for (int i = i1; i < i2; i++)\r
+ {\r
+ sb.append(charAt(i));\r
}\r
+ return sb.toString();\r
+ }\r
+\r
+ public Object unwrap()\r
+ {\r
+ return raf;\r
+ }\r
\r
- public BasicStringBufferLike newStringBufferLike() {\r
- return new StringBufferWrap();\r
+ public static void main(String[] files)\r
+ throws IOException\r
+ {\r
+ for (int i = 0; i < files.length; i++)\r
+ {\r
+ RandomAccessFileWrap fw =\r
+ new RandomAccessFileWrap(new RandomAccessFile(files[i], "r"));\r
+ Regex r = new Regex("toString\\(\\) *(?@{})");\r
+ r.setGFlag(true);\r
+ r.optimize();\r
+ System.out.print(files[i] + " ");\r
+ int j = 0;\r
+ do\r
+ {\r
+ if (r.searchFrom(fw, j))\r
+ {\r
+ System.out.println("Matched at index: " +\r
+ r.matchedFrom());\r
+ j = r.matchedTo();\r
+ }\r
+ else\r
+ {\r
+ System.out.println("not found");\r
+ }\r
+ System.out.println(r.stringMatched());\r
+ }\r
+ while (r.didMatch());\r
}\r
+ }\r
\r
- public int indexOf(char c) {\r
- for(int i=0;i<length();i++)\r
- if(charAt(i)==c)\r
- return i;\r
- return -1;\r
+ public BasicStringBufferLike newStringBufferLike()\r
+ {\r
+ return new StringBufferWrap();\r
+ }\r
+\r
+ public int indexOf(char c)\r
+ {\r
+ for (int i = 0; i < length(); i++)\r
+ {\r
+ if (charAt(i) == c)\r
+ {\r
+ return i;\r
+ }\r
}\r
+ return -1;\r
+ }\r
}\r