]> Creatis software - bbtk.git/blob - packages/std/src/bbstdStringToVector.h
cd39769bf02bb28ca0165b5b7f1807dccaca84ee
[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 //=================================================================
10 // UserBlackBox declaration
11 template <class T>
12 class StringToVector : public bbtk::UserBlackBox
13 {  
14   BBTK_USER_BLACK_BOX_INTERFACE(StringToVector,bbtk::UserBlackBox);
15   BBTK_DECLARE_INPUT(In,std::string);
16   BBTK_DECLARE_OUTPUT(Out,std::vector<T>);
17   BBTK_PROCESS(DoIt);
18   void DoIt(); 
19   T decode_item(const std::string&);
20 };
21 //=================================================================
22  
23
24
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 //=================================================================
37
38
39
40 //=================================================================
41 template <class T>
42 void StringToVector<T>::DoIt()
43 {
44   //  std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl;
45
46   typedef T type;
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);
54   bbmOutputOut.clear();
55   while (std::string::npos != pos || std::string::npos != lastPos)
56     {
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);
65     }
66   //  std::cout << "s=" << this->bbGetOutputOut().size() << std::endl;
67 }
68 //=================================================================
69
70
71 } // namespace bbstd
72
73 #endif //__bbstdStringToVector_INCLUDED_h__
74