Save This Page
Home » openjdk-7 » net.sourceforge » foobase » [javadoc | source]
net.sourceforge.foobase
public class: CBFDatabase [javadoc | source]
java.lang.Object
   net.sourceforge.foobase.CBFDatabase

All Implemented Interfaces:
    IsGameDatabase

Implementation of old-style chessbase database.

This class is based on the sources of "CB Utilities" by Andy Duplain (version of 1-Apr-96, obtained from the Pittsburg University FTP site)

None of the code made it directly in this tool (obviously, because the original was written C ;-) but nonetheless, much of the information on which this source is based on, comes from that toolset.

The structure of the index file (.CBI file) is like this:

Byte-offsetLength in bytesContents
04Number of games + 1
.........
4 * (i + 1)4Location of game i (first game is 0) + (i + 2)
.........

The last entry in the index table contains the byte offset in the games datafile for the next game to enter. E.g. if the database contains 5 games, the index file will contain at byte offset 24 (next-game-index ( = 5 + 1) times 4 (length of entries in index file)) the 32-bit value (byte-length-games-file) + 12 (= 2 times index of next game (-= 5 + 1))

The data in the games file is seriously obfuscated, but the table below gives the logical structure without regard for the encoding schemes.

Each game is structured like this:

Field Summary
protected  String _dbnam    Name of the database.

This name contains both the location (directory) and the pure name part of the database. 

protected  RandomAccessFile _idx    Index file. 
protected  RandomAccessFile _gms    Games data file. 
public static final  String __version     
Constructor:
 public CBFDatabase() 
Method from net.sourceforge.foobase.CBFDatabase Summary:
add,   appendMoves,   barf,   barfByte,   barfInt,   barfShort,   buildPlayersInfo,   buildSourceInfo,   close,   create,   delete,   extractAnnotator,   extractBlackELO,   extractECO,   extractRound,   extractWhiteELO,   extractYear,   get,   getGameID,   getGames,   getHeader,   getHeaders,   getMoveGenerator,   open,   parseHeader,   parseHeaderText,   parseHeaderText,   prepareGameForOutput,   processMoves,   setName,   size,   slurp,   slurpByte,   slurpInt,   slurpShort,   update
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from net.sourceforge.foobase.CBFDatabase Detail:
 public  void add(Game g) throws Exception 
    Add a new Game to the database.

    This method is not required to verify insertion of duplicate Games.

    Note that the input parameter is changed: its game ID is set.

 protected  void appendMoves(ByteArrayOutputStream bos,
    ByteArrayOutputStream cos,
    Vector mvs,
    Board brd,
    int halfmove) 
    Build move list for game.
 protected  void barf(RandomAccessFile f,
    long o,
    byte[] bar,
    int ofs,
    int len) throws IOException 
    Write specified byte array to file at given position.
 protected  void barfByte(RandomAccessFile f,
    long o,
    byte b) throws IOException 
    Write out one single byte to file at given position.
 protected  void barfInt(RandomAccessFile f,
    long o,
    int i) throws IOException 
    Local helper routine to convert 32-bit integer from Java representation to intel-CPU format.

    The intel format stores a 32-bit 0x12345678 as bytes 0x34, 0x12, 0x78, 0x56.

 protected  void barfShort(RandomAccessFile f,
    long o,
    int s) throws IOException 
    Write out a 16-bit integer to file at given position and in intel-CPU format.

    The intel format stores a 16-bit 0x1234 as bytes 0x34, 0x12.

 protected byte[] buildPlayersInfo(Game g) 
    Build players info part of a game to store in the database.
 protected byte[] buildSourceInfo(Game g) 
    Build game source info (that is: tournament, round and annotator).
 public  void close() throws Exception 
    Close a database.
 public  void create() throws Exception 
    Create a new database.

    Note that if the database already exists, its index pointers will be reset, thus effectively emptying the database.

 public  void delete(Game g) throws Exception 
    Remove a Game from the database.

    Note that this method is only required to work on Games that were previously read from the same database as the update is being done.

 protected String extractAnnotator(byte[] p,
    int ofs) 
    Extract annotator information from specified string.

    Annotator information is expected to be between brackets.

    On return, the input parameter may be changed.

 protected int extractBlackELO(byte[] hdr) 
    Extract ELO of black player.
 protected String extractECO(byte[] hdr) 
    Extract ECO code for game.
 protected String extractRound(byte[] p) 
    Extract round information from specified string.

    Round information is expected to be between parenthesis or brackets, but a slash, followed by a left parenthesis is not a valid round prefix.

    On return, the input parameter may be changed in that the bytes that contained the round information are set to spaces.

 protected int extractWhiteELO(byte[] hdr) 
    Extract ELO of white player.
 protected int extractYear(byte[] hdr) 
    Extract year game was played.
 public Game get(int idx) throws Exception 
    Retrieve a game based on its index in the database.
 public int getGameID(Game g) 
    Return the game ID of the specified game in this database.
 public Vector getGames() throws Exception 
    Retrieve all games present in the database.
 public String getHeader(int idx) throws Exception 
    Retrieve a game header text based on its index in the database.

    The information retrieved by this method serves mainly to display an overview of the games stored in a database.

    Contrary to the get() method, this method does not throw an exception if the specified game is deleted.

 public Vector getHeaders() throws Exception 
    Retrieve the headers for all games present in the database.
 public static CBFMoveGenerator getMoveGenerator() 
    Get single instance of CBFMoveGenerator class to use for move generation.
 public  void open() throws Exception 
    Open an existing database.
 protected  void parseHeader(byte[] hdr,
    Game g) 
    Extract all relevant information from the game header bytes.
 protected String[] parseHeaderText(byte[] txt,
    int plen) 
    Process textual header information such as names of players, tournament etc.
 protected  void parseHeaderText(byte[] txt,
    int plen,
    Game g) 
    Process textual header information such as names of players, tournament etc.
 protected byte[] prepareGameForOutput(Game g,
    byte[] hdr) throws Exception 
    Prepare buffers to be written to the database.

    This method uses a somewhat dubious technique to return two pieces of data (well, two byte arrays): one is a fixed size array that needs to be pre-allocated and passed to the function as an argument, the other one is the function result.

 protected  void processMoves(Game g,
    byte[] mvs,
    Vector notes) throws Exception 
    Process contents of moves and comments byte arrays and convert them to the proper format as required by this package.
 public  void setName(String nam) throws Exception 
    Set name of Game collection.

    The meaning of this name is implementation dependent, that is, it is up to the implementing classes to interprete this name.

    For example, this may be the name of a PGN file or it may be the basename for an old-style Chessbase database (that will use files with this basename followed by the extensions ".CBI" and ".CBF")

 public int size() throws Exception 
    Returns the number of Games stored in the database.
 protected byte[] slurp(RandomAccessFile f,
    long o,
    int len) throws IOException 
    Local helper method for reading a specified amount of bytes from a file, starting at the specified file offset.

    Note that this method will block until all requested bytes are read (or until an end-of-file or an exception is detected)

 protected int slurpByte(RandomAccessFile f,
    long o) throws IOException 
    Local helper routine to read and convert unsigned 8-bit integer (i.e. one byte)
 protected int slurpInt(RandomAccessFile f,
    long o) throws IOException 
    Local helper routine to read and convert 32-bit integer in intel-CPU format to Java representation.

    The intel format stores a 32-bit 0x12345678 as bytes 0x34, 0x12, 0x78, 0x56.

 protected int slurpShort(RandomAccessFile f,
    long o) throws IOException 
    Local helper routine to read and convert 16-bit integer in intel-CPU format to Java representation.

    The intel format stores a 16-bit 0x1234 as bytes 0x34, 0x12.

 public  void update(Game g) throws Exception 
    Update a Game in the database.

    Note that this method is only required to work on Games that were previously read from the same database as the update is being done.