diff --git a/fpga/include/villas/utils.hpp b/fpga/include/villas/utils.hpp new file mode 100644 index 000000000..b2b5b314c --- /dev/null +++ b/fpga/include/villas/utils.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include + +namespace villas { +namespace utils { + +std::vector +tokenize(std::string s, std::string delimiter); + +} // namespace utils +} // namespace villas + diff --git a/fpga/lib/CMakeLists.txt b/fpga/lib/CMakeLists.txt index a4962cb49..1d639f14e 100644 --- a/fpga/lib/CMakeLists.txt +++ b/fpga/lib/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES plugin.c plugin.cpp utils.c + utils.cpp list.c log.c log_config.c diff --git a/fpga/lib/utils.cpp b/fpga/lib/utils.cpp new file mode 100644 index 000000000..f0cd982f1 --- /dev/null +++ b/fpga/lib/utils.cpp @@ -0,0 +1,35 @@ +#include +#include + +#include "utils.hpp" + +namespace villas { +namespace utils { + +std::vector +tokenize(std::string s, std::string delimiter) +{ + std::vector tokens; + + size_t lastPos = 0; + size_t curentPos; + + while((curentPos = s.find(delimiter, lastPos)) != std::string::npos) { + const size_t tokenLength = curentPos - lastPos; + tokens.push_back(s.substr(lastPos, tokenLength)); + + // advance in string + lastPos = curentPos + delimiter.length(); + } + + // check if there's a last token behind the last delimiter + if(lastPos != s.length()) { + const size_t lastTokenLength = s.length() - lastPos; + tokens.push_back(s.substr(lastPos, lastTokenLength)); + } + + return tokens; +} + +} // namespace utils +} // namespace villas