]> Creatis software - bbtk.git/blob - packages/std/src/bbstdStringToVector.h
=== MAJOR RELEASE ====
[bbtk.git] / packages / std / src / bbstdStringToVector.h
1 #ifndef __bbstdStringToVector_INCLUDED_h__
2 #define __bbstdStringToVector_INCLUDED_h__
3
4 #include "bbtkAtomicBlackBox.h"
5
6 namespace bbstd
7 {
8   //=================================================================
9   // BlackBox declaration
10   template <class T>
11   class StringToVector : public bbtk::AtomicBlackBox
12   {  
13     BBTK_TEMPLATE_BLACK_BOX_INTERFACE(StringToVector,bbtk::AtomicBlackBox,T);
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   // BlackBox description
24   BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector,bbtk::AtomicBlackBox);
25   BBTK_NAME("StringTo"+bbtk::HumanTypeName<std::vector<T> >());
26   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
27   BBTK_DEFAULT_ADAPTOR();
28   BBTK_DESCRIPTION("Converts the content of the input string to a "
29                    +bbtk::HumanTypeName<std::vector<T> >()
30                    +" ("+bbtk::TypeName<std::vector<T> >()+")");
31   BBTK_TEMPLATE_INPUT(StringToVector, In,"Input",std::string);
32   typedef std::vector<T> Tvector;
33   BBTK_TEMPLATE_OUTPUT(StringToVector, Out,"Output",Tvector);
34   BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector);
35   //=================================================================
36   
37   //=================================================================
38   template <class T>
39   void StringToVector<T>::DoIt()
40   {
41     //  std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl;
42     
43     typedef T type;
44     std::string delimiters(" ,;");
45     std::string str(bbGetInputIn()); 
46     //  std::cout << "'"<< str << "'"<<std::endl;
47     // Skip delimiters at beginning.
48     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
49     // Find first "non-delimiter".
50     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
51     bbmOutputOut.clear();
52     while (std::string::npos != pos || std::string::npos != lastPos)
53       {
54         // Found a token, add it to the vector.
55         bbmOutputOut.push_back( 
56                                this->decode_item( str.substr(lastPos, pos - lastPos) ) );
57         //      std::cout << this->bbGetOutputOut().back() << std::endl;
58         // Skip delimiters.  Note the "not_of"
59         lastPos = str.find_first_not_of(delimiters, pos);
60         // Find next "non-delimiter"
61         pos = str.find_first_of(delimiters, lastPos);
62       }
63     //  std::cout << "s=" << this->bbGetOutputOut().size() << std::endl;
64   }
65   //=================================================================
66   
67   
68 } // namespace bbstd
69
70 #endif //__bbstdStringToVector_INCLUDED_h__