added README to examples and improved database version check when used from Sourcetrail

This commit is contained in:
Eberhard Graether
2018-12-18 10:48:43 +01:00
parent f4cf5f171a
commit aa85d20e73
13 changed files with 190 additions and 140 deletions
+24
View File
@@ -0,0 +1,24 @@
## C++ API Example
### Requirements
* CMake
* C++ Compiler
### Building
* Run cmake with `BUILD_EXAMPLES=ON`
* Build target `cpp_api_example`
### Running from Command Line
Invoke binary located in the build directory:
```
$ cpp_api_example path/to/database_file.srctrldb path/to/examples/cpp_api_example/data/example.cpp
```
### Running with Sourcetrail
Open `cpp_api_example.srctrlprj` located in the build directory with Sourcetrail and index the project.
@@ -2,7 +2,7 @@
<config>
<source_groups>
<source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
<custom_command>./cpp_api_example $DB_PATH $DB_VERSION $SOURCE_PATH</custom_command>
<custom_command>./cpp_api_example $DB_PATH $SOURCE_PATH $DB_VERSION</custom_command>
<name>Custom Command Source Group</name>
<source_extensions>
<source_extension>.cpp</source_extension>
+24 -12
View File
@@ -1,3 +1,4 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
@@ -7,39 +8,48 @@ int main(int argc, const char *argv[])
{
sourcetrail::SourcetrailDBWriter dbWriter;
std::cout << "SourcetrailDB Poetry Indexer" << std::endl;
std::cout << "\nSourcetrailDB C++ API Example" << std::endl;
std::cout << std::endl;
std::cout << "SourcetrailDB version: " << dbWriter.getVersionString() << std::endl;
std::cout << "Supported database version: " << dbWriter.getSupportedDatabaseVersion() << std::endl;
std::cout << std::endl;
if (argc != 4)
if (argc < 3 || argc > 4)
{
std::cout << "usage: poetry_indexer <database_path> <database_version> <source_path>";
std::cout << "usage: cpp_api_example <database_path> <source_path> <optional:database_version>";
}
std::string dbPath = argv[1];
int dbVersion = std::stoi(argv[2]);
std::string sourcePath = argv[3];
std::string sourcePath = argv[2];
if (dbVersion != dbWriter.getSupportedDatabaseVersion())
int dbVersion = 0;
if (argc == 4)
{
std::cout << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion()
char* end;
dbVersion = strtol(argv[3], &end, 10);
}
if (dbVersion && dbVersion != dbWriter.getSupportedDatabaseVersion())
{
std::cerr << "error: binary only supports database version: " << dbWriter.getSupportedDatabaseVersion()
<< ". Requested version: " << dbVersion << std::endl;
return 1;
}
// open database by passing .srctrldb or .srctrldb_tmp path
std::cout << "Opening Database: " << dbPath << std::endl;
if (!dbWriter.open(dbPath))
{
std::cout << "error: " << dbWriter.getLastError() << std::endl;
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
return 1;
}
std::cout << "Starting Indexing..." << std::endl;
// start recording with faster speed
if (!dbWriter.beginTransaction())
{
std::cout << "error: " << dbWriter.getLastError() << std::endl;
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
return 1;
}
@@ -108,22 +118,24 @@ int main(int argc, const char *argv[])
// record error
dbWriter.recordError("Really? You missed that \";\" again?", false, { fileId, 22, 1, 22, 1 });
dbWriter.recordError("Really? You missed that \";\" again? (intentional error)", false, { fileId, 22, 1, 22, 1 });
// end recording
if (!dbWriter.commitTransaction())
{
std::cout << "error: " << dbWriter.getLastError() << std::endl;
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
return 1;
}
// check for errors before finishing
if (dbWriter.getLastError().size())
{
std::cout << "error: " << dbWriter.getLastError() << std::endl;
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
return 1;
}
std::cout << "done!" << std::endl;
return 0;
}