Loading a CAN database from file

Load an existing database file with kvaDbCreate

The function kvaDbCreate can be used to both create empty databases and to load pre-existing ones from file. The behaviour depends on whether a filename is provided.
Example. Open an existing database.

KvaDbHnd db_handle;
KvaDbStatus status;
// Open handle
status = kvaDbOpen(&db_handle);
// Load database file
status = kvaDbCreate(db_handle, "db_name", "database.dbc");
// Close handle when done
status = kvaDbClose(&db_handle);

Passing null instead of a filename would have created a new database instead:

// Create new database
status = kvaDbCreate(db_handle, "db_name", NULL);

Example. Open a database file and check for errors.

KvaDbHnd db_handle;
KvaDbStatus status;
unsigned int buflen = 128;
char* errstr;
// Open handle
status = kvaDbOpen(&db_handle);
// Load database file
status = kvaDbCreate(db_handle, "db_name", "database.dbc");

If kvadblib fails to open the provided file, kvaDbErr_DbFileOpen is returned.

if (status == kvaDbErr_DbFileOpen)
{
printf("Could not open the file '%s'.\n", filename);
}

If parsing of the file fails, kvaDbErr_DbFileParse is returned. More information can then be retrieved by calling kvaDbGetLastParseError directly after kvaDbCreate.

else if (status == kvaDbErr_DbFileParse)
{
printf("Could not parse the file '%s'.\n", filename);
errstr = new char[buflen];
status = kvaDbGetLastParseError(errstr, &buflen);
if (status == kvaDbErr_BufferSize){
delete [] errstr;
errstr = new char[buflen];
status = kvaDbGetLastParseError(errstr, &buflen);
}
printf("%s", errstr);
delete [] errstr;
}

Finally, close the database handle.

// Close handle when done
status = kvaDbClose(&db_handle);