All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InputString.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_INPUTSTRING_H_
23 #define _BLAZE_UTIL_INPUTSTRING_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cctype>
31 #include <iostream>
32 #include <string>
33 #include <blaze/util/Assert.h>
34 
35 
36 namespace blaze {
37 
38 //=================================================================================================
39 //
40 // CLASS DEFINITION
41 //
42 //=================================================================================================
43 
44 //*************************************************************************************************
55 {
56  //**Friend declarations*************************************************************************
58  friend std::istream& operator>>( std::istream& is, InputString& str );
60  //**********************************************************************************************
61 
62  public:
63  //**Type definitions****************************************************************************
64  typedef std::string::size_type SizeType;
65  //**********************************************************************************************
66 
67  //**Constructors********************************************************************************
70  explicit inline InputString( const char* string="" );
71  explicit inline InputString( const std::string& string );
72  inline InputString( const InputString& s );
74  //**********************************************************************************************
75 
76  //**Destructor**********************************************************************************
77  // No explicitly declared destructor.
78  //**********************************************************************************************
79 
80  //**Assignment operators************************************************************************
83  inline InputString& operator=( const char* string );
84  inline InputString& operator=( const std::string& string );
85  // No explicitly declared copy assignment operator.
87  //**********************************************************************************************
88 
89  //**Access functions****************************************************************************
92  inline char& operator[]( SizeType index );
93  inline const char& operator[]( SizeType index ) const;
95  //**********************************************************************************************
96 
97  //**Utility functions***************************************************************************
102  inline const char* c_str() const;
103  inline const std::string& str() const;
104  inline SizeType size() const;
105  inline SizeType capacity() const;
106  inline bool empty() const;
107  inline void reserve( SizeType newSize );
109  //**********************************************************************************************
110 
111  private:
112  //**Member varibales****************************************************************************
115  std::string buffer_;
116 
117  //**********************************************************************************************
118 };
119 //*************************************************************************************************
120 
121 
122 
123 
124 //=================================================================================================
125 //
126 // CONSTRUCTORS
127 //
128 //=================================================================================================
129 
130 //*************************************************************************************************
135 inline InputString::InputString( const char* string )
136  : buffer_( string ) // Character buffer
137 {}
138 //*************************************************************************************************
139 
140 
141 //*************************************************************************************************
146 inline InputString::InputString( const std::string& string )
147  : buffer_( string ) // Character buffer
148 {}
149 //*************************************************************************************************
150 
151 
152 //*************************************************************************************************
158  : buffer_( s.buffer_ ) // Character buffer
159 {}
160 //*************************************************************************************************
161 
162 
163 
164 
165 //=================================================================================================
166 //
167 // ASSIGNMENT OPERATORS
168 //
169 //=================================================================================================
170 
171 //*************************************************************************************************
177 inline InputString& InputString::operator=( const char* string )
178 {
179  buffer_ = string;
180  return *this;
181 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
191 inline InputString& InputString::operator=( const std::string& string )
192 {
193  buffer_ = string;
194  return *this;
195 }
196 //*************************************************************************************************
197 
198 
199 
200 
201 //=================================================================================================
202 //
203 // ACCESS FUNCTIONS
204 //
205 //=================================================================================================
206 
207 //*************************************************************************************************
213 inline char& InputString::operator[]( SizeType index )
214 {
215  BLAZE_USER_ASSERT( index < size(), "Invalid access index" );
216  return buffer_[index];
217 }
218 //*************************************************************************************************
219 
220 
221 //*************************************************************************************************
227 inline const char& InputString::operator[]( SizeType index ) const
228 {
229  BLAZE_USER_ASSERT( index < size(), "Invalid access index" );
230  return buffer_[index];
231 }
232 //*************************************************************************************************
233 
234 
235 
236 
237 //=================================================================================================
238 //
239 // UTILITY FUNCTIONS
240 //
241 //=================================================================================================
242 
243 //*************************************************************************************************
248 inline const char* InputString::c_str() const
249 {
250  return buffer_.c_str();
251 }
252 //*************************************************************************************************
253 
254 
255 //*************************************************************************************************
260 inline const std::string& InputString::str() const
261 {
262  return buffer_;
263 }
264 //*************************************************************************************************
265 
266 
267 //*************************************************************************************************
273 {
274  return buffer_.size();
275 }
276 //*************************************************************************************************
277 
278 
279 //*************************************************************************************************
285 {
286  return buffer_.capacity();
287 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
296 inline bool InputString::empty() const
297 {
298  return buffer_.empty();
299 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
309 inline void InputString::reserve( SizeType newSize )
310 {
311  buffer_.reserve( newSize );
312 }
313 //*************************************************************************************************
314 
315 
316 
317 
318 //=================================================================================================
319 //
320 // GLOBAL OPERATORS
321 //
322 //=================================================================================================
323 
324 //*************************************************************************************************
327 inline bool IsFileName( const InputString& s );
328 inline std::ostream& operator<<( std::ostream& os, const InputString& str );
329 inline std::istream& operator>>( std::istream& is, InputString& str );
331 //*************************************************************************************************
332 
333 
334 //*************************************************************************************************
344 inline bool IsFileName( const InputString& s )
345 {
346  if( s.empty() ) return false;
347  else if( isalnum(s[0]) || s[0] == '.' || s[0] == '/' || s[0] == '_' ) return true;
348  else return false;
349 }
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
361 inline std::ostream& operator<<( std::ostream& os, const InputString& str )
362 {
363  return os << str.str();
364 }
365 //*************************************************************************************************
366 
367 
368 //*************************************************************************************************
379 inline std::istream& operator>>( std::istream& is, InputString& str )
380 {
381  if( !is ) return is;
382 
383  char c;
384  std::string buffer;
385  std::istream::pos_type pos( is.tellg() );
386 
387  buffer.reserve( 20 );
388 
389  // Extracting the leading quotation
390  is >> std::ws;
391  if( !is.get( c ) || c != '"' ) {
392  is.clear();
393  is.seekg( pos );
394  is.setstate( std::istream::failbit );
395  return is;
396  }
397 
398  // Extracting the input string
399  while( true )
400  {
401  if( !is.get( c ) || c == '\n' ) {
402  is.clear();
403  is.seekg( pos );
404  is.setstate( std::istream::failbit );
405  return is;
406  }
407  else if( c == '"' ) break;
408 
409  buffer.push_back( c );
410  }
411 
412  // Replacing the old string
413  swap( str.buffer_, buffer );
414 
415  return is;
416 }
417 //*************************************************************************************************
418 
419 } // namespace blaze
420 
421 #endif