1 #ifndef __bbstdStringToVector_INCLUDED_h__
2 #define __bbstdStringToVector_INCLUDED_h__
4 #include "bbtkAtomicBlackBox.h"
8 //=================================================================
9 // BlackBox declaration
11 class StringToVector : public bbtk::AtomicBlackBox
13 BBTK_USER_BLACK_BOX_INTERFACE(StringToVector,bbtk::AtomicBlackBox);
14 BBTK_DECLARE_INPUT(In,std::string);
15 BBTK_DECLARE_OUTPUT(Out,std::vector<T>);
18 T decode_item(const std::string&);
20 //=================================================================
22 //=================================================================
23 // BlackBox description
24 BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
25 BBTK_NAME("StringTo"+bbtk::HumanTypeName<std::vector<T> >());
26 BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
27 BBTK_CATEGORY("adaptor");
28 BBTK_DEFAULT_ADAPTOR();
29 BBTK_DESCRIPTION("Converts the content of the input string to a "
30 +bbtk::TypeName<std::vector<T> >()
31 +" ("+bbtk::TypeName<std::vector<T> >()+")");
32 BBTK_TEMPLATE_INPUT(StringToVector, In,"Input",std::string);
33 typedef std::vector<T> Tvector;
34 BBTK_TEMPLATE_OUTPUT(StringToVector, Out,"Output",Tvector);
35 BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
36 //=================================================================
38 //=================================================================
40 void StringToVector<T>::DoIt()
42 // std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl;
45 std::string delimiters(" ,;");
46 std::string str(bbGetInputIn());
47 // std::cout << "'"<< str << "'"<<std::endl;
48 // Skip delimiters at beginning.
49 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
50 // Find first "non-delimiter".
51 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
53 while (std::string::npos != pos || std::string::npos != lastPos)
55 // Found a token, add it to the vector.
56 bbmOutputOut.push_back(
57 this->decode_item( str.substr(lastPos, pos - lastPos) ) );
58 // std::cout << this->bbGetOutputOut().back() << std::endl;
59 // Skip delimiters. Note the "not_of"
60 lastPos = str.find_first_not_of(delimiters, pos);
61 // Find next "non-delimiter"
62 pos = str.find_first_of(delimiters, lastPos);
64 // std::cout << "s=" << this->bbGetOutputOut().size() << std::endl;
66 //=================================================================
71 #endif //__bbstdStringToVector_INCLUDED_h__