Contents Up Previous Next

wxExprDatabase

The wxExprDatabase class represents a database, or list, of Prolog-like expressions. Instances of this class are used for reading, writing and creating data files.

Derived from

wxList
wxObject

See also

wxExpr overview, wxExpr

Members

wxExprDatabase::wxExprDatabase
wxExprDatabase::~wxExprDatabase
wxExprDatabase::Append
wxExprDatabase::BeginFind
wxExprDatabase::ClearDatabase
wxExprDatabase::FindClause
wxExprDatabase::FindClauseByFunctor
wxExprDatabase::GetErrorCount
wxExprDatabase::HashFind
wxExprDatabase::Read
wxExprDatabase::ReadFromString
wxExprDatabase::Write


wxExprDatabase::wxExprDatabase

void wxExprDatabase(proioErrorHandler handler = 0)

Construct a new, unhashed database, with an optional error handler. The error handler must be a function returning a bool and taking an integer and a string argument. When an error occurs when reading or writing a database, this function is called. The error is given as the first argument (currently one of WXEXPR_ERROR_GENERAL, WXEXPR_ERROR_SYNTAX) and an error message is given as the second argument. If FALSE is returned by the error handler, processing of the wxExpr operation stops.

Another way of handling errors is simply to call wxExprDatabase::GetErrorCount after the operation, to check whether errors have occurred, instead of installing an error handler. If the error count is more than zero, wxExprDatabase::Write and wxExprDatabase::Read will return FALSE to the application.

For example:

bool myErrorHandler(int err, chat *msg)
{
  if (err == WXEXPR_ERROR_SYNTAX)
  {
    wxMessageBox(msg, "Syntax error");
  }
  return FALSE;
}

wxExprDatabase database(myErrorHandler);
wxExprDatabase(wxExprType type, const wxString&attribute, int size = 500, proioErrorHandler handler = 0)

Construct a new database hashed on a combination of the clause functor and a named attribute (often an integer identification).

See above for an explanation of the error handler.


wxExprDatabase::~wxExprDatabase

~wxExprDatabase()

Delete the database and contents.


wxExprDatabase::Append

void Append(wxExpr* clause)

Append a clause to the end of the database. If the database is hashing, the functor and a user-specified attribute will be hashed upon, giving the option of random access in addition to linear traversal of the database.


wxExprDatabase::BeginFind

void BeginFind()

Reset the current position to the start of the database. Subsequent wxExprDatabase::FindClause calls will move the pointer.


wxExprDatabase::ClearDatabase

void ClearDatabase()

Clears the contents of the database.


wxExprDatabase::FindClause

Various ways of retrieving clauses from the database. A return value of NULL indicates no (more) clauses matching the given criteria. Calling the functions repeatedly retrieves more matching clauses, if any.

wxExpr* FindClause(long id)

Find a clause based on the special "id'' attribute.

wxExpr* FindClause(const wxString& attribute, const wxString& value)

Find a clause which has the given attribute set to the given string or word value.

wxExpr* FindClause(const wxString& attribute, long value)

Find a clause which has the given attribute set to the given integer value.

wxExpr* FindClause(const wxString& attribute, float value)

Find a clause which has the given attribute set to the given floating point value.


wxExprDatabase::FindClauseByFunctor

wxExpr* FindClauseByFunctor(const wxString& functor)

Find the next clause with the specified functor.


wxExprDatabase::GetErrorCount

int GetErrorCount() const

Returns the number of errors encountered during the last read or write operation.


wxExprDatabase::HashFind

wxExpr* HashFind(const wxString& functor, long value) const

Finds the clause with the given functor and with the attribute specified in the database constructor having the given integer value.

For example,

// Hash on a combination of functor and integer "id" attribute when reading in
wxExprDatabase db(wxExprInteger, "id");

// Read it in
db.ReadProlog("data");

// Retrieve a clause with specified functor and id
wxExpr *clause = db.HashFind("node", 24);
This would retrieve a clause which is written: node(id = 24, ..., ).

wxExpr* HashFind(const wxString& functor, const wxString& value)

Finds the clause with the given functor and with the attribute specified in the database constructor having the given string value.


wxExprDatabase::Read

bool Read(const wxString& filename)

Reads in the given file, returning TRUE if successful.


wxExprDatabase::ReadFromString

bool ReadFromString(const wxString& buffer)

Reads a Prolog database from the given string buffer, returning TRUE if successful.


wxExprDatabase::Write

bool Write(FILE *stream)

bool Write(const wxString& filename)

Writes the database as a Prolog-format file.