X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkCommon.txx;h=845e62fe520deb89def17b26d99c0e3fa7be1c98;hb=543b72e23ad001ac2a7743b9beacf48e2d0054ac;hp=d7c91405dda8f45ff76138c341784f4d05b16b4c;hpb=7fb3f08a03ea03edb40f5e73d59381c5a92935fb;p=clitk.git diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index d7c9140..845e62f 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -3,7 +3,7 @@ Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,20 +14,14 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ +===========================================================================**/ #ifndef CLITKCOMMON_TXX #define CLITKCOMMON_TXX -/** - ------------------------------------------------- - * @file clitkCommon.txx - * @author David Sarrut - * @date 18 May 2006 - * - -------------------------------------------------*/ //------------------------------------------------------- // Utility functions for text file parsing (author: joel schaerer) +//-------------------------------------------------------------------- template ElementType parse_value(std::string str) { @@ -37,7 +31,10 @@ ElementType parse_value(std::string str) assert(!parser.fail()); return value; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- template std::vector parse_string(std::string str,char delim) { @@ -49,7 +46,10 @@ std::vector parse_string(std::string str,char delim) } return result; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- template std::vector > parse_file(const char* filename,char delim) { @@ -62,6 +62,8 @@ std::vector > parse_file(const char* filename,char deli } return result; } +//-------------------------------------------------------------------- + //-------------------------------------------------------------------- // Convert float, double ... to string @@ -74,6 +76,19 @@ std::string toString(const T & t) } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://www.codeguru.com/forum/showthread.php?t=231054 +template +bool fromString(T& t, const std::string& s, + std::ios_base& (*f)(std::ios_base&)) +{ + std::istringstream iss(s); + return !(iss >> f >> t).fail(); +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- // Convert float*, double* ... to string template @@ -180,6 +195,7 @@ std::string GetTypeAsString() } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- template void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) @@ -202,5 +218,39 @@ void CloneImage(const typename ImageType::Pointer & input, typename ImageType::P } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl +template +void MapToVecFirst(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->first ); + } +} +template +void MapToVecSecond(const M & m, V & v) { + for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) { + v.push_back( it->second ); + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +//http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string +template +int inline findAndReplace(T& source, const T& find, const T& replace) +{ + int num=0; + int fLen = find.size(); + int rLen = replace.size(); + for (int pos=0; (pos=source.find(find, pos))!=T::npos; pos+=rLen) { + num++; + source.replace(pos, fLen, replace); + } + return num; +} +//-------------------------------------------------------------------- + #endif /* end #define CLITKCOMMON_TXX */