]> Creatis software - clitk.git/commitdiff
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
authorSimon Rit <simon.rit@creatis.insa-lyon.fr>
Thu, 8 Dec 2011 10:03:15 +0000 (11:03 +0100)
committerSimon Rit <simon.rit@creatis.insa-lyon.fr>
Thu, 8 Dec 2011 10:03:15 +0000 (11:03 +0100)
common/CMakeLists.txt
common/clitkGateAsciiImageIO.cxx
common/clitkGateAsciiImageIO.h
common/clitkIO.cxx

index 380ce41d5c40f3afd9d6224af5202b86cc7d0b63..6026b72e304d4a21e9a3df3d966f90779b161d4a 100644 (file)
@@ -13,6 +13,8 @@ SET(clitkCommon_SRC
   clitkImageCommon.cxx
   clitkTransformUtilities.cxx
   clitkIO.cxx
+  clitkGateAsciiImageIO.cxx
+  clitkGateAsciiImageIOFactory.cxx
   clitkVoxImageIO.cxx  
   clitkVoxImageIOFactory.cxx
   clitkVfImageIO.cxx  
@@ -36,14 +38,6 @@ SET(clitkCommon_SRC
   vvImageWriter.cxx
 )
 
-IF(UNIX)
-  SET(clitkCommon_SRC
-    ${clitkCommon_SRC}
-    clitkGateAsciiImageIO.cxx
-    clitkGateAsciiImageIOFactory.cxx
-  )
-ENDIF(UNIX)
-
 ### Declare clitkCommon library
 ADD_LIBRARY(clitkCommon STATIC ${clitkCommon_SRC})
 
index 744f413f55e7dcc800e3b7a6676b97ef3da9026c..dd47ef46b61d6b670eead939d387869c41a2273f 100644 (file)
@@ -17,7 +17,6 @@
 ===========================================================================**/
 
 // std include
-#include <regex.h>
 #include <cstdio>
 #include <sstream>
 #include <iostream>
@@ -116,7 +115,7 @@ bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
        if (ferror(handle)) return false;
        if (fread(&item,1,1,handle) != 1) return false;
 
-       if (item=='\n' or feof(handle)) {
+       if (item=='\n' || feof(handle)) {
            line = stream.str();
            return true;
        }
@@ -125,11 +124,6 @@ bool clitk::GateAsciiImageIO::ReadLine(FILE* handle, std::string& line)
     }
 }
 
-std::string ExtractMatch(const std::string& base, const regmatch_t& match) 
-{
-    return base.substr(match.rm_so,match.rm_eo-match.rm_so);
-}
-
 template <typename T>
 T ConvertFromString(const std::string& value)
 {
@@ -140,57 +134,53 @@ T ConvertFromString(const std::string& value)
     return converted;
 }
 
+bool
+clitk::GateAsciiImageIO::FindRegularExpressionNextLine(itksys::RegularExpression &reg, std::string &s, FILE* handle)
+{
+  std::string line;
+  if(!ReadLine(handle,line))  return false;
+  if(!reg.compile(s.c_str())) return false;
+  return reg.find(line.c_str());
+}
+
 //--------------------------------------------------------------------
 // Read Image Header
 bool clitk::GateAsciiImageIO::ReadHeader(FILE* handle, GateAsciiHeader& header)
 {
     assert(handle);
-    std::string line;
-
-    regex_t re_comment;
-    regex_t re_matrix_size;
-    regex_t re_resol;
-    regex_t re_voxel_size;
-    regex_t re_nb_value;
-    regmatch_t matches[4];
-
-    { // build regex
-       assert(regcomp(&re_comment,"^#.+$",REG_EXTENDED|REG_NEWLINE) == 0);
-       assert(regcomp(&re_matrix_size,"^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
-       assert(regcomp(&re_resol,"^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
-       assert(regcomp(&re_voxel_size,"^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",REG_EXTENDED|REG_NEWLINE) == 0);
-       assert(regcomp(&re_nb_value,"^# +nbVal *= +([0-9]+)$",REG_EXTENDED|REG_NEWLINE) == 0);
-    }
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
+  std::string regexstr[6] =
+    {"^#.+$",
+     "^# +Matrix *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
+     "^# +Resol *= +\\(([0-9]+),([0-9]+),([0-9]+)\\)$",
+     "^# +Voxel *Size *= +\\(([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*),([0-9]+\\.?[0-9]*)\\)$",
+     "^# +nbVal *= +([0-9]+)$"};
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_matrix_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.matrix_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
-    header.matrix_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
-    header.matrix_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
+  itksys::RegularExpression regex;
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_resol,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.resolution[0] = ConvertFromString<int>(ExtractMatch(line,matches[1]));
-    header.resolution[1] = ConvertFromString<int>(ExtractMatch(line,matches[2]));
-    header.resolution[2] = ConvertFromString<int>(ExtractMatch(line,matches[3]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_voxel_size,line.c_str(),4,matches,0) == REG_NOMATCH) return false;
-    header.voxel_size[0] = ConvertFromString<double>(ExtractMatch(line,matches[1]));
-    header.voxel_size[1] = ConvertFromString<double>(ExtractMatch(line,matches[2]));
-    header.voxel_size[2] = ConvertFromString<double>(ExtractMatch(line,matches[3]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[1], handle)) return false;
+  header.matrix_size[0] = ConvertFromString<double>(regex.match(1));
+  header.matrix_size[1] = ConvertFromString<double>(regex.match(2));
+  header.matrix_size[2] = ConvertFromString<double>(regex.match(3));
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_nb_value,line.c_str(),2,matches,0) == REG_NOMATCH) return false;
-    header.nb_value = ConvertFromString<int>(ExtractMatch(line,matches[1]));
+  if(!FindRegularExpressionNextLine(regex, regexstr[2], handle)) return false;
+  header.resolution[0] = ConvertFromString<int>(regex.match(1));
+  header.resolution[1] = ConvertFromString<int>(regex.match(2));
+  header.resolution[2] = ConvertFromString<int>(regex.match(3));
 
-    if (!ReadLine(handle,line)) return false;
-    if (regexec(&re_comment,line.c_str(),1,matches,0) == REG_NOMATCH) return false;
+  if(!FindRegularExpressionNextLine(regex, regexstr[3], handle)) return false;
+  header.voxel_size[0] = ConvertFromString<double>(regex.match(1));
+  header.voxel_size[1] = ConvertFromString<double>(regex.match(2));
+  header.voxel_size[2] = ConvertFromString<double>(regex.match(3));
 
-    return true;
+  if(!FindRegularExpressionNextLine(regex, regexstr[4], handle)) return false;
+  header.nb_value = ConvertFromString<int>(regex.match(1));
+
+  if(!FindRegularExpressionNextLine(regex, regexstr[0], handle)) return false;
+
+  return true;
 }
 
 //--------------------------------------------------------------------
index d0abbea0b91922c47e73b48a3768eec279266ec4..ddcd00fbb1d9f28ff3c5108fe8b53174d3db380a 100644 (file)
@@ -19,7 +19,8 @@
 #define CLITKGATEASCIIIMAGEIO_H
 
 // itk include
-#include "itkImageIOBase.h"
+#include <itkImageIOBase.h>
+#include <itksys/RegularExpression.hxx>
 
 #if defined (_MSC_VER) && (_MSC_VER < 1600)
 //SR: taken from
@@ -70,9 +71,9 @@ namespace clitk {
            virtual bool SupportsDimension(unsigned long dim);
 
        protected:
-
-           static bool ReadHeader(FILE* handle, GateAsciiHeader& header);
+            static bool ReadHeader(FILE* handle, GateAsciiHeader& header);
            static bool ReadLine(FILE* handle, std::string& line);
+            static bool FindRegularExpressionNextLine(itksys::RegularExpression &reg, std::string &s, FILE* handle);
 
     }; // end class GateAsciiImageIO
 } // end namespace
index 2126a5a4d79110eb1abf96422480ef5587c1f8f8..1acd352808ba3ff8f01a8beaa6cd0e2db2ddc66e 100644 (file)
@@ -38,9 +38,7 @@
 // Register factories
 void clitk::RegisterClitkFactories()
 {
-#ifdef unix
   clitk::GateAsciiImageIOFactory::RegisterOneFactory();
-#endif
   clitk::DicomRTDoseIOFactory::RegisterOneFactory();
 #if ITK_VERSION_MAJOR <= 3
   itk::ImageIOFactory::RegisterBuiltInFactories();