added README to examples and improved database version check when used from Sourcetrail
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
|
||||
## Poetry Indexer Example
|
||||
|
||||
### Requirements
|
||||
|
||||
* CMake
|
||||
* C++ Compiler
|
||||
|
||||
### Building
|
||||
|
||||
* Run cmake with `BUILD_EXAMPLES=ON`
|
||||
* Build target `poetry_indexer`
|
||||
|
||||
### Running from Command Line
|
||||
|
||||
Invoke binary located in the build directory:
|
||||
|
||||
```
|
||||
$ poetry_indexer path/to/database_file.srctrldb path/to/examples/cpp_poetry_indexer/data/e_e_cummings_-_in_just-.txt
|
||||
```
|
||||
|
||||
### Running with Sourcetrail
|
||||
|
||||
Open `poetry_indexer.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>./poetry_indexer $DB_PATH $DB_VERSION $SOURCE_PATH</custom_command>
|
||||
<custom_command>./poetry_indexer $DB_PATH $SOURCE_PATH $DB_VERSION</custom_command>
|
||||
<exclude_filters>
|
||||
<exclude_filter>@POETRY_INDEXER_DATA_PATH@/COPYRIGHT.txt</exclude_filter>
|
||||
</exclude_filters>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
@@ -23,51 +24,60 @@ int main(int argc, const char *argv[])
|
||||
{
|
||||
sourcetrail::SourcetrailDBWriter dbWriter;
|
||||
|
||||
std::cout << "SourcetrailDB Poetry Indexer" << std::endl;
|
||||
std::cout << "\nSourcetrailDB Poetry Indexer" << 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: poetry_indexer <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;
|
||||
}
|
||||
|
||||
if (!dbWriter.open(dbPath))
|
||||
{
|
||||
std::cout << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!dbWriter.beginTransaction())
|
||||
{
|
||||
std::cout << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fileId = dbWriter.recordFile(sourcePath);
|
||||
if (fileId == 0)
|
||||
{
|
||||
std::cout << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::ifstream fileStream(sourcePath.c_str());
|
||||
if (!fileStream.good())
|
||||
{
|
||||
std::cout << "error: opening file failed: " << sourcePath << std::endl;
|
||||
std::cerr << "error: opening file failed: " << sourcePath << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Opening Database: " << dbPath << std::endl;
|
||||
if (!dbWriter.open(dbPath))
|
||||
{
|
||||
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Starting Indexing: " << sourcePath << std::endl;
|
||||
|
||||
if (!dbWriter.beginTransaction())
|
||||
{
|
||||
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fileId = dbWriter.recordFile(sourcePath);
|
||||
if (fileId == 0)
|
||||
{
|
||||
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -191,15 +201,17 @@ int main(int argc, const char *argv[])
|
||||
|
||||
if (!dbWriter.commitTransaction())
|
||||
{
|
||||
std::cout << "error: " << dbWriter.getLastError() << std::endl;
|
||||
std::cerr << "error: " << dbWriter.getLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user