A library for working with phylogenetic and population genetic data.
v0.27.0
string_input_source.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_STRING_INPUT_SOURCE_H_
2 #define GENESIS_UTILS_IO_STRING_INPUT_SOURCE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
36 
37 #include <cstdio>
38 #include <cstring>
39 #include <string>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // String Input Source
46 // =================================================================================================
47 
58 class StringInputSource final : public BaseInputSource
59 {
60 public:
61 
62  // -------------------------------------------------------------
63  // Constructors and Rule of Five
64  // -------------------------------------------------------------
65 
69  StringInputSource( char const* str, size_t size )
70  : in_str_( str )
71  , cursor_( str )
72  , in_size_( size )
73  , rest_size_( size )
74  {}
75 
79  explicit StringInputSource( std::string const& str )
80  : in_str_( str.c_str() )
81  , cursor_( str.c_str() )
82  , in_size_( str.size() )
83  , rest_size_( str.size() )
84  {}
85 
86  StringInputSource( StringInputSource const& ) = default;
87  StringInputSource( StringInputSource&& ) = default;
88 
89  StringInputSource& operator= ( StringInputSource const& ) = default;
91 
92  ~StringInputSource() override
93  {}
94 
95  // -------------------------------------------------------------
96  // Overloaded Internal Members
97  // -------------------------------------------------------------
98 
99 private:
100 
104  size_t read_( char* buffer, size_t size ) override
105  {
106  // Don't overshoot.
107  if( size > rest_size_ ) {
108  size = rest_size_;
109  }
110 
111  // Read.
112  std::memcpy( buffer, cursor_, size );
113  cursor_ += size;
114  rest_size_ -= size;
115  return size;
116  }
117 
121  std::string source_name_() const override
122  {
123  return "input string";
124  }
125 
129  std::string source_string_() const override
130  {
131  return "string";
132  }
133 
134  // -------------------------------------------------------------
135  // Member Variables
136  // -------------------------------------------------------------
137 
138  // Original and current string position pointer.
139  char const* in_str_;
140  char const* cursor_;
141 
142  // Original and current (remaining) string size.
143  size_t in_size_;
144  size_t rest_size_;
145 
146 };
147 
148 } // namespace utils
149 } // namespace genesis
150 
151 #endif // include guard
fs.hpp
Provides functions for accessing the file system.
genesis::utils::StringInputSource::StringInputSource
StringInputSource(std::string const &str)
Construct the input source from a std::string.
Definition: string_input_source.hpp:79
base_input_source.hpp
genesis::utils::StringInputSource::StringInputSource
StringInputSource(char const *str, size_t size)
Construct the input source from a char array.
Definition: string_input_source.hpp:69
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::StringInputSource::~StringInputSource
~StringInputSource() override
Definition: string_input_source.hpp:92
genesis::utils::BaseInputSource
Abstract base class for reading byte data from input sources.
Definition: base_input_source.hpp:50
genesis::utils::StringInputSource
Input source for reading byte data from a string.
Definition: string_input_source.hpp:58
genesis::utils::StringInputSource::operator=
StringInputSource & operator=(StringInputSource const &)=default