]> Creatis software - clitk.git/blobdiff - common/clitkCommon.txx
Conversion from itk to vtk was catching exceptions for us => update itk pipeline...
[clitk.git] / common / clitkCommon.txx
index c2e9a4ae3f92acca36287309d416971cc3a1e574..553c9b5f1df523ab1efdd0cdbc3aa63f7dc9598b 100644 (file)
 ======================================================================-====*/
 #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
@@ -36,6 +76,19 @@ std::string toString(const T & t)
 }
 //--------------------------------------------------------------------
 
+
+//--------------------------------------------------------------------
+// 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>