Flow123d  DF_patch_fe_data_tables-dc76198
reader_to_storage.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 reader_to_storage.cc
15  * @brief
16  */
17 
18 #include <cstdint>
19 #include <limits>
20 #include <boost/iostreams/device/file.hpp>
21 #include <boost/iostreams/filtering_stream.hpp>
22 
23 #include "reader_to_storage.hh"
24 #include "input/path_json.hh"
25 #include "input/path_yaml.hh"
26 #include "input/input_type.hh"
27 #include "input/accessors.hh"
28 #include "input/reader_internal.hh"
29 #include <stddef.h> // for NULL
30 
31 #include <ostream> // for operator<<
32 #include <set> // for set, _Rb_tree_...
33 #include <typeinfo> // for type_info
34 #include <utility> // for pair
35 #include <vector> // for vector
36 #include "system/asserts.hh" // for Assert, ASSERT
37 #include "system/file_path.hh" // for FilePath, File...
38 #include "system/logger.hh" // for operator<<
39 
40 
41 namespace Input {
42 using namespace std;
43 using namespace internal;
44 
45 
46 
47 /********************************************
48  * Implementation of public part of ReaderToStorage
49  */
50 
52 : storage_(nullptr),
53  root_type_(nullptr)
54 {}
55 
56 
57 
60 {
61  std::string fname = in_file;
62  std::string extension = fname.substr(fname.find_last_of(".") + 1);
64  if (extension == "con") {
66  } else if (extension == "yaml") {
68  } else {
69  THROW(ExcInputMessage() << EI_Message("Invalid extension of file " + fname + ".\nMust be 'con' or 'yaml'."));
70  }
71 
72  try {
73  std::ifstream in;
74  in_file.open_stream(in);
75 
76  // finish of root_type ensures finish of whole IST
77  root_type.finish();
78 
79  read_stream(in, root_type, format);
80  } catch (Input::Exception &e ) {
81  e << Input::ReaderInternalBase::EI_File(in_file); throw;
82  }
83 }
84 
85 
86 
89 {
90  // finish of root_type ensures finish of whole IST
91  root_type.finish();
92 
93  try {
94  istringstream is(str);
95  read_stream(is, root_type, format);
96  } catch (ReaderInternalBase::ExcNotJSONFormat &e) {
97  e << ReaderInternalBase::EI_File("STRING: "+str); throw;
98  }
99 }
100 
101 
102 
104 {
105  return storage_;
106 }
107 
108 
109 
110 void ReaderToStorage::read_stream(istream &in, const Type::TypeBase &root_type, FileFormat format)
111 {
112  ASSERT(storage_==nullptr).error();
113 
114  PathBase * root_path;
116  root_path = new PathJSON(in);
117  } else {
118  root_path = new PathYAML(in);
119  }
120 
121  // guarantee to delete root_path on function return even on exception
122  std::unique_ptr<PathBase> root_path_ptr(root_path);
123 
124  root_type_ = &root_type;
125  try {
126  ReaderInternal ri;
127  storage_ = ri.read_storage(*root_path_ptr, root_type_);
128  } catch (ReaderInternalBase::ExcInputError &e) {
130  e << ReaderInternalBase::EI_Format("JSON");
131  } else {
132  e << ReaderInternalBase::EI_Format("YAML");
133  }
134  throw;
135  }
136 
137  ASSERT_PTR(storage_).error();
138 }
139 
140 
141 
142 
143 
144 
145 /********************************************88
146  * Implementation
147  */
148 
149 template <class T>
151 {
152  ASSERT_PTR(storage_).error();
153 
154  Address addr(storage_, root_type_);
155  // try to create an iterator just to check type
156  Iterator<T>( *root_type_, addr, 0);
157 
158  auto tmp_root_type = static_cast<const typename T::InputType &>(*root_type_);
159  return T( addr, tmp_root_type );
160 }
161 
162 
163 template ::Input::Record ReaderToStorage::get_root_interface<::Input::Record>() const;
164 template ::Input::Array ReaderToStorage::get_root_interface<::Input::Array>() const;
165 template ::Input::AbstractRecord ReaderToStorage::get_root_interface<::Input::AbstractRecord>() const;
166 template ::Input::Tuple ReaderToStorage::get_root_interface<::Input::Tuple>() const;
167 //template ReaderToStorage::get_root_interface<::Input::>()->::Input::AbstractRecord const;
168 
169 
170 } // namespace Input
Definitions of ASSERTS.
#define ASSERT(expr)
Definition: asserts.hh:351
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
void open_stream(Stream &stream) const
Definition: file_path.cc:211
Base of exceptions due to user input.
Base abstract class used by ReaderToStorage class to iterate over the input tree.
Definition: path_base.hh:41
Class used by ReaderToStorage class to iterate over the JSON tree provided by json_spirit library.
Definition: path_json.hh:46
Class used by ReaderToStorage class to iterate over the YAML tree provided by yaml-cpp library.
Definition: path_yaml.hh:46
Creates storage of IST defined in JSON or YAML file.
StorageBase * read_storage(PathBase &p, const Type::TypeBase *type)
Create storage of given type.
Reader for (slightly) modified input files.
ReaderToStorage()
Default constructor.
StorageBase * storage_
Storage of the read and checked input data.
void read_stream(istream &in, const Type::TypeBase &root_type, FileFormat format)
This method actually reads the given stream in.
T get_root_interface() const
Returns the root accessor.
StorageBase * get_storage()
Getter for root of the storage tree.
const Type::TypeBase * root_type_
Root of the declaration tree of the data in the storage.
Base class for nodes of a data storage tree.
Definition: storage.hh:68
Base of classes for declaring structure of the input data.
Definition: type_base.hh:92
virtual FinishStatus finish(FinishStatus finish_type=FinishStatus::regular_)
Finish method. Finalize construction of "Lazy types": Record, Selection, Abstract and generic type.
Definition: type_base.cc:216
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
manipulators::Array< T, Delim > format(T const &deduce, Delim delim=", ")
Definition: logger.hh:325
Abstract linear system class.
Definition: balance.hh:40
FileFormat
Possible formats of input files.