nl.bluering
Class CoolFileHandle

java.lang.Object
  |
  +--nl.bluering.CoolFileHandle

public class CoolFileHandle
extends java.lang.Object

A CoolFileHandle can be used to read files. It is cool because it can tokenize the file: with get you can ask for a token that matches the given regular expression. This makes it ideal for building parsers with. Another cool feature is that you can rewind the file to a previous position (also very handy for parsing).


Constructor Summary
CoolFileHandle(java.io.File ff)
          parses the given file.
CoolFileHandle(java.lang.String text)
          creates a CFH for parsing the given string.
 
Method Summary
 void buffer(boolean on)
          buffer(true) enables buffering buffer(false) disables buffering.
 void close()
          closes the CFH.
 boolean EOF()
           
 java.lang.String get(Expression pattern)
          get returns a part of the stream that matches the given regular expression.
 int getpos()
          get the current position.
 boolean lastin(java.lang.String s)
           
static void main(java.lang.String[] ps)
          test the class.
 void pushback(java.lang.String s)
           
 void setpos(int i)
          with setpos you can return to a previous position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CoolFileHandle

public CoolFileHandle(java.lang.String text)
               throws java.lang.Exception
creates a CFH for parsing the given string. Good for testing.
Parameters:
text - the string to parse
Throws:
java.lang.Exception - in case something went wrong with the file.

CoolFileHandle

public CoolFileHandle(java.io.File ff)
               throws java.lang.Exception
parses the given file.
Parameters:
ff - the file to open
Throws:
java.lang.Exception - in case something went wrong with the file.
Method Detail

close

public void close()
closes the CFH. You need to do this when you are finished. If an exception was thrown, the CFH is already closed. But it doesn't hurt to close a CFH twice.

EOF

public boolean EOF()

getpos

public int getpos()
get the current position. It only works if buffering is on..
Returns:
-1 if buffering s off, or another integer that you should not inspect but only give to setpos.

setpos

public void setpos(int i)
with setpos you can return to a previous position. Do not use it to jump forward. It only works if you turned buffering on.
Parameters:
i - an integer that you got from getpos()

buffer

public void buffer(boolean on)
buffer(true) enables buffering buffer(false) disables buffering. To flush the buffer, you should do buffer(false);buffer(true);

lastin

public boolean lastin(java.lang.String s)

pushback

public void pushback(java.lang.String s)

get

public java.lang.String get(Expression pattern)
                     throws java.lang.Exception
get returns a part of the stream that matches the given regular expression. It returns null if there is no match. the known expressions are(in string notation):
in(abc)
matches one character if it is a,b or c
out(abc)
matches one character that is not in abc
ignore(sub1)
tries to get sub1 from the stream, but does not return it
insert(abc)
returns abc without doing anything with the file .You can say it is the opposite of ignore.
not(sub1)
returns the empty string if it can't get sub1
or(sub1,sub2)
first tries to get sub1, then tries sub2
quote(abc)
tries to get "abc" from the string
and(sub1,sub2)
tries to get sub1 followed by sub2 from the stream
few(sub1)
tries to get any number of sub1 from the stream. It is lazy, so it will try to get as few as possible first. Note that few is more efficient than many
many(sub1)
tries to get any number of sub1 from the stream. It is greedy, so it tries to get as much sub1 as possible. Note that this is just a search order difference, because it will backtrack if the next element fails.
allin(abc)
This parses all characters that appear in abc. It does not do backtracking like many or few. It is thus very efficient.
allout(abc)
The opposite of allin: it gets all characters outside abc
maybe(sub1)
parses sub1 if available, otherwise just return the empty string.
dontcare(sub1)
This is equal to ignore(maybe(sub1))
Throws:
java.lang.Exception - if a technical error with the current file happened

main

public static void main(java.lang.String[] ps)
test the class. You do not need this function, unless you are modifying this class .