1 #ifndef __bbstdStringToVector_INCLUDED_h__
2 #define __bbstdStringToVector_INCLUDED_h__
4 #include "bbtkUserBlackBox.h"
9 //=================================================================
10 // UserBlackBox declaration
12 class StringToVector : public bbtk::UserBlackBox
14 BBTK_USER_BLACK_BOX_INTERFACE(StringToVector,bbtk::UserBlackBox);
15 BBTK_DECLARE_INPUT(In,std::string);
16 BBTK_DECLARE_OUTPUT(Out,std::vector<T>);
19 T decode_item(const std::string&);
21 //=================================================================
25 //=================================================================
26 // UserBlackBox description
27 BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
28 BBTK_NAME("StringTo"+bbtk::TypeName<std::vector<T> >());
29 BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
30 BBTK_DEFAULT_ADAPTOR();
31 BBTK_DESCRIPTION("Converts the content of the input string to a "+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 //=================================================================
40 //=================================================================
42 void StringToVector<T>::DoIt()
44 // std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl;
47 std::string delimiters(" ,;");
48 std::string str(bbGetInputIn());
49 // std::cout << "'"<< str << "'"<<std::endl;
50 // Skip delimiters at beginning.
51 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
52 // Find first "non-delimiter".
53 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
55 while (std::string::npos != pos || std::string::npos != lastPos)
57 // Found a token, add it to the vector.
58 bbmOutputOut.push_back(
59 this->decode_item( str.substr(lastPos, pos - lastPos) ) );
60 // std::cout << this->bbGetOutputOut().back() << std::endl;
61 // Skip delimiters. Note the "not_of"
62 lastPos = str.find_first_not_of(delimiters, pos);
63 // Find next "non-delimiter"
64 pos = str.find_first_of(delimiters, lastPos);
66 // std::cout << "s=" << this->bbGetOutputOut().size() << std::endl;
68 //=================================================================
73 #endif //__bbstdStringToVector_INCLUDED_h__