InputString.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_INPUTSTRING_H_
36 #define _BLAZE_UTIL_INPUTSTRING_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cctype>
44 #include <iostream>
45 #include <string>
46 #include <blaze/util/Assert.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS DEFINITION
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
68 {
69  //**Friend declarations*************************************************************************
71  friend std::istream& operator>>( std::istream& is, InputString& str );
73  //**********************************************************************************************
74 
75  public:
76  //**Type definitions****************************************************************************
77  typedef std::string::size_type SizeType;
78  //**********************************************************************************************
79 
80  //**Constructors********************************************************************************
83  explicit inline InputString( const char* string="" );
84  explicit inline InputString( const std::string& string );
85  inline InputString( const InputString& s );
87  //**********************************************************************************************
88 
89  //**Destructor**********************************************************************************
90  // No explicitly declared destructor.
91  //**********************************************************************************************
92 
93  //**Assignment operators************************************************************************
96  inline InputString& operator=( const char* string );
97  inline InputString& operator=( const std::string& string );
98  // No explicitly declared copy assignment operator.
100  //**********************************************************************************************
101 
102  //**Access functions****************************************************************************
105  inline char& operator[]( SizeType index );
106  inline const char& operator[]( SizeType index ) const;
108  //**********************************************************************************************
109 
110  //**Utility functions***************************************************************************
115  inline const char* c_str() const;
116  inline const std::string& str() const;
117  inline SizeType size() const;
118  inline SizeType capacity() const;
119  inline bool empty() const;
120  inline void reserve( SizeType newSize );
122  //**********************************************************************************************
123 
124  private:
125  //**Member varibales****************************************************************************
128  std::string buffer_;
129 
130  //**********************************************************************************************
131 };
132 //*************************************************************************************************
133 
134 
135 
136 
137 //=================================================================================================
138 //
139 // CONSTRUCTORS
140 //
141 //=================================================================================================
142 
143 //*************************************************************************************************
148 inline InputString::InputString( const char* string )
149  : buffer_( string ) // Character buffer
150 {}
151 //*************************************************************************************************
152 
153 
154 //*************************************************************************************************
159 inline InputString::InputString( const std::string& string )
160  : buffer_( string ) // Character buffer
161 {}
162 //*************************************************************************************************
163 
164 
165 //*************************************************************************************************
171  : buffer_( s.buffer_ ) // Character buffer
172 {}
173 //*************************************************************************************************
174 
175 
176 
177 
178 //=================================================================================================
179 //
180 // ASSIGNMENT OPERATORS
181 //
182 //=================================================================================================
183 
184 //*************************************************************************************************
190 inline InputString& InputString::operator=( const char* string )
191 {
192  buffer_ = string;
193  return *this;
194 }
195 //*************************************************************************************************
196 
197 
198 //*************************************************************************************************
204 inline InputString& InputString::operator=( const std::string& string )
205 {
206  buffer_ = string;
207  return *this;
208 }
209 //*************************************************************************************************
210 
211 
212 
213 
214 //=================================================================================================
215 //
216 // ACCESS FUNCTIONS
217 //
218 //=================================================================================================
219 
220 //*************************************************************************************************
226 inline char& InputString::operator[]( SizeType index )
227 {
228  BLAZE_USER_ASSERT( index < size(), "Invalid access index" );
229  return buffer_[index];
230 }
231 //*************************************************************************************************
232 
233 
234 //*************************************************************************************************
240 inline const char& InputString::operator[]( SizeType index ) const
241 {
242  BLAZE_USER_ASSERT( index < size(), "Invalid access index" );
243  return buffer_[index];
244 }
245 //*************************************************************************************************
246 
247 
248 
249 
250 //=================================================================================================
251 //
252 // UTILITY FUNCTIONS
253 //
254 //=================================================================================================
255 
256 //*************************************************************************************************
261 inline const char* InputString::c_str() const
262 {
263  return buffer_.c_str();
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
273 inline const std::string& InputString::str() const
274 {
275  return buffer_;
276 }
277 //*************************************************************************************************
278 
279 
280 //*************************************************************************************************
286 {
287  return buffer_.size();
288 }
289 //*************************************************************************************************
290 
291 
292 //*************************************************************************************************
298 {
299  return buffer_.capacity();
300 }
301 //*************************************************************************************************
302 
303 
304 //*************************************************************************************************
309 inline bool InputString::empty() const
310 {
311  return buffer_.empty();
312 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
322 inline void InputString::reserve( SizeType newSize )
323 {
324  buffer_.reserve( newSize );
325 }
326 //*************************************************************************************************
327 
328 
329 
330 
331 //=================================================================================================
332 //
333 // GLOBAL OPERATORS
334 //
335 //=================================================================================================
336 
337 //*************************************************************************************************
340 inline bool IsFileName( const InputString& s );
341 inline std::ostream& operator<<( std::ostream& os, const InputString& str );
342 inline std::istream& operator>>( std::istream& is, InputString& str );
344 //*************************************************************************************************
345 
346 
347 //*************************************************************************************************
357 inline bool IsFileName( const InputString& s )
358 {
359  if( s.empty() ) return false;
360  else if( isalnum(s[0]) || s[0] == '.' || s[0] == '/' || s[0] == '_' ) return true;
361  else return false;
362 }
363 //*************************************************************************************************
364 
365 
366 //*************************************************************************************************
374 inline std::ostream& operator<<( std::ostream& os, const InputString& str )
375 {
376  return os << str.str();
377 }
378 //*************************************************************************************************
379 
380 
381 //*************************************************************************************************
392 inline std::istream& operator>>( std::istream& is, InputString& str )
393 {
394  if( !is ) return is;
395 
396  char c;
397  std::string buffer;
398  std::istream::pos_type pos( is.tellg() );
399 
400  buffer.reserve( 20 );
401 
402  // Extracting the leading quotation
403  is >> std::ws;
404  if( !is.get( c ) || c != '"' ) {
405  is.clear();
406  is.seekg( pos );
407  is.setstate( std::istream::failbit );
408  return is;
409  }
410 
411  // Extracting the input string
412  while( true )
413  {
414  if( !is.get( c ) || c == '\n' ) {
415  is.clear();
416  is.seekg( pos );
417  is.setstate( std::istream::failbit );
418  return is;
419  }
420  else if( c == '"' ) break;
421 
422  buffer.push_back( c );
423  }
424 
425  // Replacing the old string
426  swap( str.buffer_, buffer );
427 
428  return is;
429 }
430 //*************************************************************************************************
431 
432 } // namespace blaze
433 
434 #endif
bool IsFileName(const InputString &s)
Tests for a valid file name.
Definition: InputString.h:357
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
SizeType size() const
Returns the size of the string.
Definition: InputString.h:285
char & operator[](SizeType index)
Direct access to the characters of the string.
Definition: InputString.h:226
const std::string & str() const
Conversion to a std::string.
Definition: InputString.h:273
std::string buffer_
The character buffer.
Definition: InputString.h:128
Implementation of a string wrapper.The InputString class is a wrapper class for the purpose to read i...
Definition: InputString.h:67
std::istream & operator>>(std::istream &is, InputString &str)
Global input operator for the InputString class.
Definition: InputString.h:392
const char * c_str() const
Conversion to a constant character array.
Definition: InputString.h:261
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
std::string::size_type SizeType
Size type of the InputString.
Definition: InputString.h:77
InputString(const char *string="")
The default constructor for InputString.
Definition: InputString.h:148
InputString & operator=(const char *string)
Assignment operator for C-style character strings.
Definition: InputString.h:190
Header file for run time assertion macros.
std::ostream & operator<<(std::ostream &os, const Matrix< MT, SO > &m)
Global output operator for dense and sparse matrices.
Definition: Matrix.h:76
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b) noexcept
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:258
void reserve(SizeType newSize)
Reserves at least size characters within the string.
Definition: InputString.h:322
SizeType capacity() const
Returns the maximum capacity of the string.
Definition: InputString.h:297
bool empty() const
Returns if the string is empty.
Definition: InputString.h:309