ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkCSVFileReaderBase.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 
00019 #ifndef __itkCSVFileReaderBase_h
00020 #define __itkCSVFileReaderBase_h
00021 
00022 #include "itkLightProcessObject.h"
00023 #include <vcl_limits.h>
00024 #include "itkMacro.h"
00025 #include "itkSize.h"
00026 #include <fstream>
00027 
00028 namespace itk
00029 {
00063 class ITK_EXPORT CSVFileReaderBase:public LightProcessObject
00064 {
00065 public:
00067   typedef CSVFileReaderBase         Self;
00068   typedef LightProcessObject        Superclass;
00069 
00071   itkTypeMacro(Self,Superclass);
00072 
00074   itkSetStringMacro(FileName);
00075 
00079   itkSetMacro(FieldDelimiterCharacter,char);
00080 
00082   itkGetMacro(FieldDelimiterCharacter, char);
00083 
00086   itkSetMacro(UseStringDelimiterCharacter,bool);
00087 
00089   itkGetConstMacro(UseStringDelimiterCharacter, bool);
00090 
00092   itkSetMacro(StringDelimiterCharacter,char);
00093 
00095   itkGetMacro(StringDelimiterCharacter, char);
00096 
00099   itkSetMacro(HasRowHeaders,bool);
00100 
00102   itkGetConstMacro(HasRowHeaders,bool);
00103 
00106   itkSetMacro(HasColumnHeaders,bool);
00107 
00109   itkGetConstMacro(HasColumnHeaders, bool);
00110 
00114   itkBooleanMacro(HasRowHeaders);
00115   itkBooleanMacro(HasColumnHeaders);
00116   itkBooleanMacro(UseStringDelimiterCharacter);
00118 
00121   void GetDataDimension(SizeValueType & rows, SizeValueType & columns);
00122 
00125   void GetNextField(std::string & );
00126 
00134   template <class TData>
00135   TData ConvertStringToValueType(const std::string str)
00136   {
00137     TData value;
00138     std::istringstream isstream(str);
00139     isstream >> value;
00141 
00142     size_t isstreamtellg = static_cast<size_t>(isstream.tellg() );
00143 
00144     if (!isstream.fail() || isstreamtellg == str.length() )
00145       {
00146       return value;
00147       }
00148     else
00149       {
00150       return vcl_numeric_limits<TData>::quiet_NaN();
00151       }
00152   }
00153 
00159   virtual void Parse()=0;
00160 
00161 protected:
00162   std::string                  m_FileName;
00163   char                         m_FieldDelimiterCharacter;
00164   char                         m_StringDelimiterCharacter;
00165   bool                         m_UseStringDelimiterCharacter;
00166   bool                         m_HasRowHeaders;
00167   bool                         m_HasColumnHeaders;
00168   std::ifstream                m_InputStream;
00169   int                          m_EndOfColumnHeadersLine;
00170   std::string                  m_Line;
00171 
00172   CSVFileReaderBase();
00173   virtual ~CSVFileReaderBase() {}
00175   void PrintSelf(std::ostream & os, Indent indent) const;
00176 
00178   void PrepareForParsing();
00179 
00180 private:
00181   CSVFileReaderBase(const Self &);       //purposely not implemented
00182   void operator=(const Self &);          //purposely not implemented
00183 };
00184 
00185 } //end namespace itk
00186 
00187 #endif
00188