]> Creatis software - bbtk.git/blob - packages/std/src/bbstdStringToVector.h
Package descr
[bbtk.git] / packages / std / src / bbstdStringToVector.h
1 #ifndef __bbstdStringToVector_INCLUDED_h__
2 #define __bbstdStringToVector_INCLUDED_h__
3
4 #include "bbtkUserBlackBox.h"
5
6 namespace bbstd
7 {
8 //=================================================================
9 // UserBlackBox declaration
10 template <class T>
11 class StringToVector : public bbtk::UserBlackBox
12 {  
13   BBTK_USER_BLACK_BOX_INTERFACE(StringToVector,bbtk::UserBlackBox);
14     BBTK_DECLARE_INPUT(In,std::string);
15     BBTK_DECLARE_OUTPUT(Out,std::vector<T>);
16   BBTK_PROCESS(DoIt);
17   void DoIt(); 
18   T decode_item(const std::string&);
19 };
20
21 //=================================================================
22  
23 // UserBlackBox description
24   BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
25   BBTK_NAME("StringTo"+bbtk::TypeName<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 "+bbtk::TypeName<std::vector<T> >());
30     BBTK_TEMPLATE_INPUT(StringToVector, In,"Input",std::string);
31     typedef std::vector<T> Tvector;
32     BBTK_TEMPLATE_OUTPUT(StringToVector, Out,"Output",Tvector);
33   BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
34
35 //=================================================================
36
37 template <class T>
38 void StringToVector<T>::DoIt()
39 {
40   //  std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl;
41
42   typedef T type;
43   std::string delimiters(" ,;");
44   std::string str(bbGetInputIn()); 
45   //  std::cout << "'"<< str << "'"<<std::endl;
46   // Skip delimiters at beginning.
47   std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
48   // Find first "non-delimiter".
49   std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
50   bbmOutputOut.clear();
51   while (std::string::npos != pos || std::string::npos != lastPos)
52     {
53       // Found a token, add it to the vector.
54       bbmOutputOut.push_back( 
55                              this->decode_item( str.substr(lastPos, pos - lastPos) ) );
56       //      std::cout << this->bbGetOutputOut().back() << std::endl;
57       // Skip delimiters.  Note the "not_of"
58       lastPos = str.find_first_not_of(delimiters, pos);
59       // Find next "non-delimiter"
60       pos = str.find_first_of(delimiters, lastPos);
61     }
62   //  std::cout << "s=" << this->bbGetOutputOut().size() << std::endl;
63 }
64 //=================================================================
65
66
67 } // namespace bbstd
68
69 #endif //__bbstdStringToVector_INCLUDED_h__