From 7fb3f08a03ea03edb40f5e73d59381c5a92935fb Mon Sep 17 00:00:00 2001 From: schaerer Date: Thu, 1 Jul 2010 16:15:20 +0000 Subject: [PATCH] move parsing utilities to clitkCommon as we are using them more and more --- common/clitkCommon.h | 2 ++ common/clitkCommon.txx | 38 +++++++++++++++++++++++++++++++++++ common/clitkDicomRT_Contour.h | 27 ------------------------- vv/vvMeshReader.cxx | 29 ++++---------------------- 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/common/clitkCommon.h b/common/clitkCommon.h index 3f6e72d..410c976 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -29,6 +29,8 @@ #include #include +#include + //-------------------------------------------------------------------- namespace clitk { diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index c2e9a4a..d7c9140 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -25,6 +25,44 @@ * -------------------------------------------------*/ +//------------------------------------------------------- +// 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 template diff --git a/common/clitkDicomRT_Contour.h b/common/clitkDicomRT_Contour.h index b599769..85ebe49 100644 --- a/common/clitkDicomRT_Contour.h +++ b/common/clitkDicomRT_Contour.h @@ -51,33 +51,6 @@ namespace clitk { }; //-------------------------------------------------------------------- - //-------------------------------------------------------------------- - template - ElementType parse_value(std::string str) - { - std::istringstream parser(str); - ElementType value; - parser >> value; - if (parser.fail()) { - DD(str); - DD(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 (getline(ss,token,delim)) - { - result.push_back(parse_value(token)); - } - return result; - } - //-------------------------------------------------------------------- } // end namespace clitk diff --git a/vv/vvMeshReader.cxx b/vv/vvMeshReader.cxx index d5100b1..152a9a3 100644 --- a/vv/vvMeshReader.cxx +++ b/vv/vvMeshReader.cxx @@ -35,6 +35,7 @@ #include "clitkCommon.h" #include "vvMeshReader.h" #include "vvProgressDialog.h" +#include vvMeshReader::vvMeshReader() : vtk_mode(false) @@ -76,28 +77,6 @@ void vvMeshReader::run() } } -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 (getline(ss,token,delim)) { - result.push_back(parse_value(token)); - } - return result; -} - std::vector > vvMeshReader::GetROINames() { assert(filename!=""); @@ -137,7 +116,7 @@ std::vector vvMeshReader::readSelectedContours() ss >> roi_number; if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end()) { //Only read selected ROIs vvMesh::Pointer current_roi=vvMesh::New(); - std::vector rgb=parse_string(i->GetEntryValue(0x3006,0x002a),'\\'); + std::vector rgb=clitk::parse_string(i->GetEntryValue(0x3006,0x002a),'\\'); assert(rgb.size()==3); current_roi->r=rgb[0]/255; current_roi->g=rgb[1]/255; @@ -148,8 +127,8 @@ std::vector vvMeshReader::readSelectedContours() for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { //loop over 2D contours std::string contour_type=j->GetEntryValue(0x3006,0x0042); if (contour_type=="CLOSED_PLANAR ") { - int point_number=parse_value(j->GetEntryValue(0x3006,0x0046)); - std::vector points=parse_string(j->GetEntryValue(0x3006,0x0050),'\\'); + int point_number=clitk::parse_value(j->GetEntryValue(0x3006,0x0046)); + std::vector points=clitk::parse_string(j->GetEntryValue(0x3006,0x0050),'\\'); assert(points.size() == static_cast(point_number)*3); if (z0 == -1) //First contour z0=points[2]; -- 2.47.1