|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Object | +--net.sourceforge.foobase.CBFDatabase
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-offset | Length in bytes | Contents |
|---|---|---|
| 0 | 4 | Number of games + 1 |
| ... | ... | ... |
| 4 * (i + 1) | 4 | Location 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:
| Byte-value | Description |
|---|---|
| 0xff | Marks start of a new variation |
| 0x80 | Marks end of variation |
| 0x01 - 0x7E | Number of move from movelist (first move is 1) |
| 0x81 - 0xFE | Move has comments attached to it: clear high bit to obtain regular move number (first move is 1) |
CBFUtil.
The list of fields given below may end at any given moment at
the marker for the next comment block:
| Byte-length | Description |
|---|---|
| 1 | evaluation string (e.g. "blunder") |
| 1 | position evalutation string (e.g. "with compensation") |
| 1 | move evaluation string (e.g. "better is") |
| 1 | unknown for now: these bytes appear to be
0x00 |
| up to just before next 0xFF byte | Annotation string to move |
In order to keep things interesting, there is a feature to store annotated text on the game level. If present, such comment is appended to the "regular" comments for the game.
Board for information on piece encoding in the cells.
| Field Summary | |
static java.lang.String |
__version
|
protected java.lang.String |
_dbnam
Name of the database. |
protected java.io.RandomAccessFile |
_gms
Games data file. |
protected java.io.RandomAccessFile |
_idx
Index file. |
| Fields inherited from interface net.sourceforge.foobase.IsGameDatabase |
__version |
| Constructor Summary | |
CBFDatabase()
Default public constructor. |
|
| Method Summary | |
void |
add(Game g)
Add a new Game to the database. |
protected void |
appendMoves(java.io.ByteArrayOutputStream bos,
java.io.ByteArrayOutputStream cos,
java.util.Vector mvs,
Board brd,
int halfmove)
Build move list for game. |
protected void |
barf(java.io.RandomAccessFile f,
long o,
byte[] bar,
int ofs,
int len)
Write specified byte array to file at given position. |
protected void |
barfByte(java.io.RandomAccessFile f,
long o,
byte b)
Write out one single byte to file at given position. |
protected void |
barfInt(java.io.RandomAccessFile f,
long o,
int i)
Local helper routine to convert 32-bit integer from Java representation to intel-CPU format. |
protected void |
barfShort(java.io.RandomAccessFile f,
long o,
int s)
Write out a 16-bit integer to file at given position and in intel-CPU format. |
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). |
void |
close()
Close a database. |
void |
create()
Create a new database. |
void |
delete(Game g)
Remove a Game from the database. |
protected java.lang.String |
extractAnnotator(byte[] p,
int ofs)
Extract annotator information from specified string. |
protected int |
extractBlackELO(byte[] hdr)
Extract ELO of black player. |
protected java.lang.String |
extractECO(byte[] hdr)
Extract ECO code for game. |
protected java.lang.String |
extractRound(byte[] p)
Extract round information from specified string. |
protected int |
extractWhiteELO(byte[] hdr)
Extract ELO of white player. |
protected int |
extractYear(byte[] hdr)
Extract year game was played. |
Game |
get(int idx)
Retrieve a game based on its index in the database. |
int |
getGameID(Game g)
Return the game ID of the specified game in this database. |
java.util.Vector |
getGames()
Retrieve all games present in the database. |
java.lang.String |
getHeader(int idx)
Retrieve a game header text based on its index in the database. |
java.util.Vector |
getHeaders()
Retrieve the headers for all games present in the database. |
static CBFMoveGenerator |
getMoveGenerator()
Get single instance of CBFMoveGenerator class to use
for move generation. |
void |
open()
Open an existing database. |
protected void |
parseHeader(byte[] hdr,
Game g)
Extract all relevant information from the game header bytes. |
protected java.lang.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)
Prepare buffers to be written to the database. |
protected void |
processMoves(Game g,
byte[] mvs,
java.util.Vector notes)
Process contents of moves and comments byte arrays and convert them to the proper format as required by this package. |
void |
setName(java.lang.String nam)
Set name of Game collection. |
int |
size()
Returns the number of Games stored in the database. |
protected byte[] |
slurp(java.io.RandomAccessFile f,
long o,
int len)
Local helper method for reading a specified amount of bytes from a file, starting at the specified file offset. |
protected int |
slurpByte(java.io.RandomAccessFile f,
long o)
Local helper routine to read and convert unsigned 8-bit integer (i.e. |
protected int |
slurpInt(java.io.RandomAccessFile f,
long o)
Local helper routine to read and convert 32-bit integer in intel-CPU format to Java representation. |
protected int |
slurpShort(java.io.RandomAccessFile f,
long o)
Local helper routine to read and convert 16-bit integer in intel-CPU format to Java representation. |
void |
update(Game g)
Update a Game in the database. |
| Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
| Field Detail |
protected java.lang.String _dbnam
This name contains both the location (directory) and the pure name part of the database.
protected java.io.RandomAccessFile _idx
protected java.io.RandomAccessFile _gms
public static final java.lang.String __version
| Constructor Detail |
public CBFDatabase()
| Method Detail |
public static CBFMoveGenerator getMoveGenerator()
CBFMoveGenerator class to use
for move generation.CBFMoveGenerator to use for old-style chessbase
databases
public void setName(java.lang.String nam)
throws java.lang.Exception
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")
nam - String specifying the name of the database.
public void create()
throws java.lang.Exception
Note that if the database already exists, its index pointers will be reset, thus effectively emptying the database.
public void open()
throws java.lang.Exception
public int size()
throws java.lang.Exception
int containing the number of Games.
public java.util.Vector getGames()
throws java.lang.Exception
Vector containing the games present in the
database.
public java.util.Vector getHeaders()
throws java.lang.Exception
Vector containing the header strings for all
the games present in the database.
public java.lang.String getHeader(int idx)
throws java.lang.Exception
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.
idx - int zero-based index of the game to retrieveString containing the header information of the
specified game (index number, name of white, name of black) or
null in case the game is deleted.
public Game get(int idx)
throws java.lang.Exception
idx - int zero-based index of the game to retrieveGame containing the specified game
public void add(Game g)
throws java.lang.Exception
This method is not required to verify insertion of duplicate Games.
Note that the input parameter is changed: its game ID is set.
g - Game to add to the database.
protected byte[] prepareGameForOutput(Game g,
byte[] hdr)
throws java.lang.Exception
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.
g - Game to add to the database.hdr - byte[] to store the header information in
(this buffer should contain HEADER_LEN bytes)byte[] containing the data to write to the
database file (including everything but the
kitchen sink^H^H^H^H^H^H^H^H^H^H^H^H header)
public void update(Game g)
throws java.lang.Exception
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.
g - Game to update in the database.
public void delete(Game g)
throws java.lang.Exception
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.
g - Game to remove from the database.
public void close()
throws java.lang.Exception
public int getGameID(Game g)
g - Game to test for its presence in this databaseint containing the internal game ID in this
database for the specified game, or -1 if the game is
not stored in this database.
protected void parseHeader(byte[] hdr,
Game g)
hdr - byte[] containing the decoded header bytes read
from the databaseg - Game to initialize with the extracted header
informationprotected int extractYear(byte[] hdr)
hdr - byte[] containing the decoded header bytes read
from the databaseint containing the year that the game was played,
or 0 in case no year information was present in the headerprotected int extractWhiteELO(byte[] hdr)
hdr - byte[] containing the decoded header bytes read
from the databaseint containing the ELO points of white or
0 in case no such information was presentprotected int extractBlackELO(byte[] hdr)
hdr - byte[] containing the decoded header bytes read
from the databaseint containing the ELO points of black or
0 in case no such information was presentprotected java.lang.String extractECO(byte[] hdr)
hdr - byte[] containing the decoded header bytes read
from the databaseString containing the ECO code for the game
protected void parseHeaderText(byte[] txt,
int plen,
Game g)
txt - byte[] containing the decoded game header
text bytesplen - int containing the length of the players
section in txtg - Game to store information in
protected java.lang.String[] parseHeaderText(byte[] txt,
int plen)
txt - byte[] containing the decoded game header
text bytesplen - int containing the length of the players
section in txtg - Game to store information inString[] containing the different game info
strings. The returned items are like this:
| Subscript | Contents |
|---|---|
| 0 | Round |
| 1 | Annotator |
| 2 | Name white player |
| 3 | Name black player |
| 4 | Tournament |
Whenever a certain item is not present in the game, its entry is
set to null
protected java.lang.String extractRound(byte[] p)
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.
p - byte[] containing the text to search for round
informationString containing the round information or
null in case no round information was found.
protected java.lang.String extractAnnotator(byte[] p,
int ofs)
Annotator information is expected to be between brackets.
On return, the input parameter may be changed.
p - byte[] containing the text to search for annotator
informationofs - int containing the byte offset from where to
start looking for the annotator.String containing the annotator information or
null in case no annotator information was found.
protected void processMoves(Game g,
byte[] mvs,
java.util.Vector notes)
throws java.lang.Exception
g - Game to which the moves belong. The properties
of this object will be changed by this method.mvs - byte[] containing the game movesnotes - Vector containing the move comments. Note that
this Vector is gradually emptied until is is either empty
or contains only the game level annotation entry.protected byte[] buildPlayersInfo(Game g)
g - Game to grab information frombyte[] containing the players informationprotected byte[] buildSourceInfo(Game g)
g - Game to grab information frombyte[] containing the source information.
protected void appendMoves(java.io.ByteArrayOutputStream bos,
java.io.ByteArrayOutputStream cos,
java.util.Vector mvs,
Board brd,
int halfmove)
bos - ByteArrayOutputStream to write moves tocos - ByteArrayOutputStream to write comments tomvs - Vector containing the variation to processbrd - Board with setup at beginning of the
variationhalfmove - int containing the number of the halfmove
that is at the start of the variation (first move of the game is
halfmove one)
protected int slurpInt(java.io.RandomAccessFile f,
long o)
throws java.io.IOException
The intel format stores a 32-bit 0x12345678 as bytes 0x34, 0x12, 0x78, 0x56.
f - RandomAccessFile to read fromo - long offset to read fromint containing the java 32-bit integer value
protected int slurpShort(java.io.RandomAccessFile f,
long o)
throws java.io.IOException
The intel format stores a 16-bit 0x1234 as bytes 0x34, 0x12.
f - RandomAccessFile to read fromo - long offset to read fromint containing the java 16-bit integer value
protected int slurpByte(java.io.RandomAccessFile f,
long o)
throws java.io.IOException
f - RandomAccessFile to read fromo - long offset to read fromint containing the java 8-bit integer value
protected byte[] slurp(java.io.RandomAccessFile f,
long o,
int len)
throws java.io.IOException
Note that this method will block until all requested bytes are read (or until an end-of-file or an exception is detected)
f - RandomAccessFile to read fromo - long offset to read fromlen - int number of bytes to read.byte[] of length len bytes containing
the requested data.
protected void barfInt(java.io.RandomAccessFile f,
long o,
int i)
throws java.io.IOException
The intel format stores a 32-bit 0x12345678 as bytes 0x34, 0x12, 0x78, 0x56.
f - RandomAccessFile to read fromo - long offset to read fromi - int to write
protected void barfShort(java.io.RandomAccessFile f,
long o,
int s)
throws java.io.IOException
The intel format stores a 16-bit 0x1234 as bytes 0x34, 0x12.
f - RandomAccessFile to write too - long offset to write tos - int 16-bit integer to write
protected void barfByte(java.io.RandomAccessFile f,
long o,
byte b)
throws java.io.IOException
f - RandomAccessFile to write too - long offset to write tob - byte to write
protected void barf(java.io.RandomAccessFile f,
long o,
byte[] bar,
int ofs,
int len)
throws java.io.IOException
f - RandomAccessFile to write too - long offset to write tobar - byte[] to writeofs - int offset in bar from where to
write out its contentslen - int number of bytes to write
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||