X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkCommon.txx;h=5ee83bdca8f4f3efc23ae2aa8dc12ad28a2fe36e;hb=39562adceed3b608834959f2ed079b945187bb22;hp=c2e9a4ae3f92acca36287309d416971cc3a1e574;hpb=1e034c70105f0926939acaaa27ddb46e904ae8bf;p=clitk.git diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index c2e9a4a..5ee83bd 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,16 +14,56 @@ - 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) +{ + std::istringstream parser(str); + ElementType value; + parser >> value; + assert(!parser.fail()); + return value; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +std::vector parse_string(std::string str,char delim) +{ + std::istringstream ss(str); + std::string token; + std::vector result; + while (std::getline(ss,token,delim)) { + result.push_back(parse_value(token)); + } + return result; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +std::vector > parse_file(const char* filename,char delim) +{ + std::ifstream fs(filename); + std::string line; + std::vector > result; + while (std::getline(fs,line)) { + if (line[0] != '#') //skip comments + result.push_back(parse_string(line,delim)); + } + return result; +} +//-------------------------------------------------------------------- + //-------------------------------------------------------------------- // Convert float, double ... to string @@ -36,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 @@ -142,6 +195,7 @@ std::string GetTypeAsString() } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- template void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output) @@ -164,5 +218,22 @@ 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 ); + } +} +//-------------------------------------------------------------------- + #endif /* end #define CLITKCOMMON_TXX */