Loading a CAN database

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 a handle
status = kvaDbOpen(&db_handle);
// Load a database file
status = kvaDbCreate(db_handle, "db_name", "database.dbc");
// Close the handle since we're done with the database
status = kvaDbClose(&db_handle);


Example. Passing null instead of a filename on the other hand would have created a new database:

// 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;
char filename[256];
strcpy(filename, "database.dbc");
// Open a handle
status = kvaDbOpen(&db_handle);
// Load a database file
status = kvaDbCreate(db_handle, "db_name", filename);
// If kvadblib fails to open the database 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.
else if (status == kvaDbErr_DbFileParse)
{
printf("Could not parse the file '%s'.\n", filename);
errstr = (char*) malloc(buflen * sizeof(char));
status = kvaDbGetLastParseError(errstr, &buflen);
// If the array is too short, adjust it and try anew
if (status == kvaDbErr_BufferSize)
{
free(errstr);
errstr = (char*) malloc(buflen*sizeof(char));
status = kvaDbGetLastParseError(errstr, &buflen);
}
printf("%s", errstr);
free(errstr);
}
// Close the handle since we're done with the database
status = kvaDbClose(&db_handle);