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
- 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 <david.sarrut@creatis.insa-lyon.fr>
- * @date 18 May 2006
- *
- -------------------------------------------------*/
+
+//-------------------------------------------------------
+// Utility functions for text file parsing (author: joel schaerer)
+
+//--------------------------------------------------------------------
+template<class ElementType>
+ElementType parse_value(std::string str)
+{
+ std::istringstream parser(str);
+ ElementType value;
+ parser >> value;
+ assert(!parser.fail());
+ return value;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ElementType>
+std::vector<ElementType> parse_string(std::string str,char delim)
+{
+ std::istringstream ss(str);
+ std::string token;
+ std::vector<ElementType> result;
+ while (std::getline(ss,token,delim)) {
+ result.push_back(parse_value<ElementType>(token));
+ }
+ return result;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ElementType>
+std::vector<std::vector<ElementType> > parse_file(const char* filename,char delim)
+{
+ std::ifstream fs(filename);
+ std::string line;
+ std::vector<std::vector<ElementType> > result;
+ while (std::getline(fs,line)) {
+ if (line[0] != '#') //skip comments
+ result.push_back(parse_string<ElementType>(line,delim));
+ }
+ return result;
+}
+//--------------------------------------------------------------------
+
//--------------------------------------------------------------------
// Convert float, double ... to string
}
//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+// http://www.codeguru.com/forum/showthread.php?t=231054
+template <class T>
+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<class T>
}
//--------------------------------------------------------------------
+
//--------------------------------------------------------------------
template<class ImageType>
void CloneImage(const typename ImageType::Pointer & input, typename ImageType::Pointer & output)
}
//--------------------------------------------------------------------
+
+//--------------------------------------------------------------------
+// http://stackoverflow.com/questions/771453/copy-map-values-to-vector-in-stl
+template <typename M, typename V>
+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 <typename M, typename V>
+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<class T>
+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 */