Flow123d  JS_before_hm-979-g397e552
csv_tokenizer.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file csv_tokenizer.cc
15  * @brief
16  */
17 
18 #include "input/csv_tokenizer.hh"
20 #include <boost/lexical_cast.hpp>
21 
22 using namespace boost;
23 using namespace Input;
24 
25 
26 CSVTokenizer::CSVTokenizer(const FilePath &fp, std::string field_separator)
27 : Tokenizer(fp, Tokenizer::Separator("\\", field_separator, "\""))
28 {}
29 
30 
31 
32 CSVTokenizer::CSVTokenizer( std::istream &in, std::string field_separator)
33 : Tokenizer(in, Tokenizer::Separator("\\", field_separator, "\""))
34 {}
35 
36 
37 unsigned int CSVTokenizer::get_n_lines()
38 {
39  unsigned int n_lines = 0; // number of lines
40  this->set_position( Tokenizer::Position() );
41  while ( !this->eof() ) {
42  this->next_line(false);
43  n_lines++;
44  }
45  if (this->line().size()==0) n_lines--; // removes last line if it is empty
46  return n_lines;
47 }
48 
49 
50 void CSVTokenizer::skip_header(unsigned int n_head_lines)
51 {
52  this->set_position( Tokenizer::Position() );
53  for (unsigned int i=0; i<n_head_lines; i++) {
54  this->next_line(false);
55  }
56 }
57 
58 
59 int CSVTokenizer::get_int_val()
60 {
61  try {
62  return lexical_cast<int>( *(*this) );
63  } catch (bad_lexical_cast &) {
64  THROW( ReaderInternalBase::ExcWrongCsvFormat() << ReaderInternalBase::EI_TokenizerMsg(this->position_msg()) );
65  }
66 }
67 
68 
69 double CSVTokenizer::get_double_val()
70 {
71  try {
72  return lexical_cast<double>( *(*this) );
73  } catch (bad_lexical_cast &) {
74  THROW( ReaderInternalBase::ExcWrongCsvFormat() << ReaderInternalBase::EI_Specification("Wrong double value")
75  << ReaderInternalBase::EI_TokenizerMsg(this->position_msg()) );
76  }
77 }
78 
79 
80 std::string CSVTokenizer::get_string_val()
81 {
82  try {
83  return *(*this);
84  } catch (bad_lexical_cast &) {
85  THROW( ReaderInternalBase::ExcWrongCsvFormat() << ReaderInternalBase::EI_TokenizerMsg(this->position_msg()) );
86  }
87 }
Abstract linear system class.
Definition: balance.hh:40
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53