diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f18d469..6cefc4b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -8,10 +8,28 @@ target_link_libraries(test1 PRIVATE ) add_executable(test2 - variantsExample.cpp + contentExample.cpp ) target_link_libraries(test2 PRIVATE telemetry::telemetry telemetry::appFs ) + +add_executable(test3 + holderExample.cpp +) + +target_link_libraries(test3 PRIVATE + telemetry::telemetry + telemetry::appFs +) + +add_executable(test4 + utilsExample.cpp +) + +target_link_libraries(test4 PRIVATE + telemetry::telemetry + telemetry::appFs +) diff --git a/examples/basicExample.cpp b/examples/basicExample.cpp index 803fcff..9b3412f 100644 --- a/examples/basicExample.cpp +++ b/examples/basicExample.cpp @@ -46,7 +46,7 @@ int main() { std::string fusePath = "fusedir"; // Linking root dir to the chosen directory on disk. - telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath); + telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath, true, false); fuse.start(); // The filesystem is still just empty. // / diff --git a/examples/hiolderExample.cpp b/examples/holderExample.cpp similarity index 50% rename from examples/hiolderExample.cpp rename to examples/holderExample.cpp index d14c523..968a81b 100644 --- a/examples/hiolderExample.cpp +++ b/examples/holderExample.cpp @@ -1,7 +1,7 @@ /** * @file * @author Daniel Pelanek - * @brief + * @brief Shows how to use the holder class * * SPDX-License-Identifier: BSD-3-Clause */ @@ -12,17 +12,25 @@ class IOwnTelemetryFiles { public: void configTelemetry(const std::shared_ptr& rootDir) { + // Create a telemetry file const telemetry::FileOps fileOps = {nullptr, nullptr}; - auto someFile = rootDir->addFile("stats", fileOps); + auto someFile = rootDir->addFile("someFile", fileOps); + // We also need to add the file to the holder. + m_telemetryHolder.add(someFile); + } + // If you want to disable callback of files before the + // is destroyed. + void disableFiles() { + m_telemetryHolder.disableFiles(); } private: telemetry::Holder m_telemetryHolder; -} +}; int main() { // Same as basic example @@ -34,8 +42,16 @@ int main() { telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath); fuse.start(); - const IOwnTelemetryFiles object; + // Object that collects it's own telemetry or owns + // telemetry files or directories. + // The files callbacks get disabled when the telemetry holder + // is destroyed. + IOwnTelemetryFiles object; + // Configuration of telemetry inside of the object. object.configTelemetry(telemetryRootNode); + + // Disable callbacks before the objects destructor + object.disableFiles(); } diff --git a/examples/utilsExample.cpp b/examples/utilsExample.cpp new file mode 100644 index 0000000..b005c47 --- /dev/null +++ b/examples/utilsExample.cpp @@ -0,0 +1,83 @@ +/** + * @file + * @author Daniel Pelanek + * @brief + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include + +// example help +#include +#include + +void traverseDir(std::shared_ptr& dir, int depth) { + // Using utils to check if a directory is a root directory + if(telemetry::utils::isRootDirectory(dir->getFullPath())) { + std::cout << "start\n"; + } + + // looping through files and dirs in directory + // listEntries returns their names as string + for(auto node_name : dir->listEntries()) { + auto node = dir->getEntry(node_name); + + std::cout << std::string(depth * 4, ' ') << node_name << "\n"; + + // Using utils to check if a node is a directory + if(telemetry::utils::isDirectory(node)) { + // dynamic_pointer_cast is needed to get a file or dir from node + auto newDir = std::dynamic_pointer_cast(node); + traverseDir(newDir, depth + 1); + + // Using utils to check if a node is a file + } else if(telemetry::utils::isFile(node)) { + auto file = std::dynamic_pointer_cast(node); + + // Manually reading file content + try { + std::cout << telemetry::contentToString(file->read()); + } catch(std::exception& ex) { + std::cout << ex.what() << "\n"; + } + } + } +} + +int main() { + // Same as basic example + std::shared_ptr telemetryRootNode; + telemetryRootNode = telemetry::Directory::create(); + + std::string fusePath = "fusedir"; + + telemetry::appFs::AppFsFuse fuse = telemetry::appFs::AppFsFuse(telemetryRootNode, fusePath); + fuse.start(); + + // Let's create a more complex dir structure + telemetry::FileOps const emptyFileOps + = {nullptr, nullptr}; + + auto file1 = telemetryRootNode->addFile("file1", emptyFileOps); + auto file2 = telemetryRootNode->addFile("file2", emptyFileOps); + auto dir1 = telemetryRootNode->addDir("dir1"); + + auto file3 = dir1->addFile("file3", emptyFileOps); + auto dir2 = dir1->addDir("dir2"); + auto dir3 = dir1->addDir("dir3"); + + const int nFiles = 5; + for(int index = 0; index < nFiles; index++) { + auto file = dir2->addFile("file" + std::to_string(index + 4), emptyFileOps); + } + + auto file9 = dir3->addFile("file9", emptyFileOps); + + // And now traverse the directory with the use of telemetry utils + traverseDir(telemetryRootNode, 0); + + // Utils can also give you a node from path + auto node = telemetry::utils::getNodeFromPath(telemetryRootNode, "/dir1/dir2/file5"); +} \ No newline at end of file