ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkCSVFileReaderBase.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef __itkCSVFileReaderBase_h
20 #define __itkCSVFileReaderBase_h
21 
22 #include "itkLightProcessObject.h"
23 #include <vcl_limits.h>
24 #include "itkMacro.h"
25 #include "itkSize.h"
26 #include <fstream>
27 
28 namespace itk
29 {
63 class ITK_EXPORT CSVFileReaderBase:public LightProcessObject
64 {
65 public:
69 
71  itkTypeMacro(Self,Superclass);
72 
74  itkSetStringMacro(FileName);
75 
79  itkSetMacro(FieldDelimiterCharacter,char);
80 
82  itkGetMacro(FieldDelimiterCharacter, char);
83 
86  itkSetMacro(UseStringDelimiterCharacter,bool);
87 
89  itkGetConstMacro(UseStringDelimiterCharacter, bool);
90 
92  itkSetMacro(StringDelimiterCharacter,char);
93 
95  itkGetMacro(StringDelimiterCharacter, char);
96 
99  itkSetMacro(HasRowHeaders,bool);
100 
102  itkGetConstMacro(HasRowHeaders,bool);
103 
106  itkSetMacro(HasColumnHeaders,bool);
107 
109  itkGetConstMacro(HasColumnHeaders, bool);
110 
114  itkBooleanMacro(HasRowHeaders);
115  itkBooleanMacro(HasColumnHeaders);
116  itkBooleanMacro(UseStringDelimiterCharacter);
118 
121  void GetDataDimension(SizeValueType & rows, SizeValueType & columns);
122 
125  void GetNextField(std::string & );
126 
134  template <class TData>
135  TData ConvertStringToValueType(const std::string str)
136  {
137  TData value;
138  std::istringstream isstream(str);
139  isstream >> value;
141 
142  size_t isstreamtellg = static_cast<size_t>(isstream.tellg() );
143 
144  if (!isstream.fail() || isstreamtellg == str.length() )
145  {
146  return value;
147  }
148  else
149  {
150  return vcl_numeric_limits<TData>::quiet_NaN();
151  }
152  }
153 
159  virtual void Parse()=0;
160 
161 protected:
162  std::string m_FileName;
168  std::ifstream m_InputStream;
170  std::string m_Line;
171 
173  virtual ~CSVFileReaderBase() {}
175  void PrintSelf(std::ostream & os, Indent indent) const;
176 
178  void PrepareForParsing();
179 
180 private:
181  CSVFileReaderBase(const Self &); //purposely not implemented
182  void operator=(const Self &); //purposely not implemented
183 };
184 
185 } //end namespace itk
186 
187 #endif
188