00001 /*========================================================================= 00002 Program: bbtk 00003 Module: $RCSfile: bbstdStringToVector.h,v $ 00004 Language: C++ 00005 Date: $Date: 2008/10/17 08:18:27 $ 00006 Version: $Revision: 1.8 $ 00007 =========================================================================*/ 00008 00009 /* --------------------------------------------------------------------- 00010 00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) 00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux 00013 * 00014 * This software is governed by the CeCILL-B license under French law and 00015 * abiding by the rules of distribution of free software. You can use, 00016 * modify and/ or redistribute the software under the terms of the CeCILL-B 00017 * license as circulated by CEA, CNRS and INRIA at the following URL 00018 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 00019 * or in the file LICENSE.txt. 00020 * 00021 * As a counterpart to the access to the source code and rights to copy, 00022 * modify and redistribute granted by the license, users are provided only 00023 * with a limited warranty and the software's author, the holder of the 00024 * economic rights, and the successive licensors have only limited 00025 * liability. 00026 * 00027 * The fact that you are presently reading this means that you have had 00028 * knowledge of the CeCILL-B license and that you accept its terms. 00029 * ------------------------------------------------------------------------ */ 00030 00031 00032 #ifndef __bbstdStringToVector_INCLUDED_h__ 00033 #define __bbstdStringToVector_INCLUDED_h__ 00034 00035 #include "bbtkAtomicBlackBox.h" 00036 00037 namespace bbstd 00038 { 00039 //================================================================= 00040 // BlackBox declaration 00041 template <class T> 00042 class StringToVector : public bbtk::AtomicBlackBox 00043 { 00044 BBTK_TEMPLATE_BLACK_BOX_INTERFACE(StringToVector,bbtk::AtomicBlackBox,T); 00045 BBTK_DECLARE_INPUT(In,std::string); 00046 BBTK_DECLARE_OUTPUT(Out,std::vector<T>); 00047 BBTK_PROCESS(DoIt); 00048 void DoIt(); 00049 T decode_item(const std::string&); 00050 }; 00051 //================================================================= 00052 00053 //================================================================= 00054 // BlackBox description 00055 BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector,bbtk::AtomicBlackBox); 00056 BBTK_NAME("StringTo"+bbtk::HumanTypeName<std::vector<T> >()); 00057 BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr"); 00058 BBTK_DEFAULT_ADAPTOR(); 00059 BBTK_DESCRIPTION("Converts the content of the input string to a " 00060 +bbtk::HumanTypeName<std::vector<T> >() 00061 +" ("+bbtk::TypeName<std::vector<T> >()+")"); 00062 BBTK_TEMPLATE_INPUT(StringToVector, In,"Input",std::string); 00063 typedef std::vector<T> Tvector; 00064 BBTK_TEMPLATE_OUTPUT(StringToVector, Out,"Output",Tvector); 00065 BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(StringToVector); 00066 //================================================================= 00067 00068 //================================================================= 00069 template <class T> 00070 void StringToVector<T>::DoIt() 00071 { 00072 // std::cout << "StringToVector<"<<bbtk::TypeName<T>()<<">::DoIt()"<<std::endl; 00073 00074 typedef T type; 00075 std::string delimiters(" ,;"); 00076 std::string str(bbGetInputIn()); 00077 // std::cout << "'"<< str << "'"<<std::endl; 00078 // Skip delimiters at beginning. 00079 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); 00080 // Find first "non-delimiter". 00081 std::string::size_type pos = str.find_first_of(delimiters, lastPos); 00082 bbmOutputOut.clear(); 00083 while (std::string::npos != pos || std::string::npos != lastPos) 00084 { 00085 // Found a token, add it to the vector. 00086 bbmOutputOut.push_back( 00087 this->decode_item( str.substr(lastPos, pos - lastPos) ) ); 00088 // std::cout << this->bbGetOutputOut().back() << std::endl; 00089 // Skip delimiters. Note the "not_of" 00090 lastPos = str.find_first_not_of(delimiters, pos); 00091 // Find next "non-delimiter" 00092 pos = str.find_first_of(delimiters, lastPos); 00093 } 00094 // std::cout << "s=" << this->bbGetOutputOut().size() << std::endl; 00095 } 00096 //================================================================= 00097 00098 00099 } // namespace bbstd 00100 00101 #endif //__bbstdStringToVector_INCLUDED_h__