2004-10-11 18:30:16 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
$Date$
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
@copy © Marc Wäckerlin
|
|
|
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2005-02-18 15:47:23 +00:00
|
|
|
#ifndef __MRW__STDEXT_HPP__
|
|
|
|
#define __MRW__STDEXT_HPP__
|
|
|
|
|
2004-10-13 11:18:33 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2004-12-20 07:40:36 +00:00
|
|
|
#include <algorithm>
|
2004-10-13 11:18:33 +00:00
|
|
|
|
2004-10-11 18:30:16 +00:00
|
|
|
namespace mrw {
|
2004-10-13 11:18:33 +00:00
|
|
|
|
2004-12-20 07:40:36 +00:00
|
|
|
/** @addtogroup StdExt
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
2005-02-18 15:47:23 +00:00
|
|
|
/** @defgroup stdextFunction Useful Global Functions
|
2004-12-20 07:40:36 +00:00
|
|
|
|
2005-02-18 15:47:23 +00:00
|
|
|
- @ref getline read exactly one line from a stream
|
|
|
|
- @ref ifelse implements a dual <code>?:</code> operator, like:
|
|
|
|
<code>a ? a : b</code>
|
|
|
|
|
2005-11-29 12:42:01 +00:00
|
|
|
@section stdextGetline Read Line
|
2005-02-18 15:47:23 +00:00
|
|
|
|
2004-12-20 07:40:36 +00:00
|
|
|
The global functions mrw::getline read exactly one line from a
|
|
|
|
stream, without the need of a buffer. It is therefore guaranteed
|
|
|
|
that a whole line is read, regardless of the length of the
|
|
|
|
line. The result is returned as std::string.
|
|
|
|
|
|
|
|
@code
|
|
|
|
// first syntax returns a string:
|
2005-11-29 12:42:01 +00:00
|
|
|
std::string line(mrw::getline(std::cin));
|
2004-12-20 07:40:36 +00:00
|
|
|
// second syntax returns the stream:
|
2005-11-29 12:42:01 +00:00
|
|
|
while (mrw::getline(std::cin, line))
|
2004-12-20 07:40:36 +00:00
|
|
|
std::cout<<"Read: "<<line<<std::endl;
|
|
|
|
@endcode
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
2005-02-18 15:47:23 +00:00
|
|
|
/** @brief Dual <code>?:</code> operation:
|
|
|
|
<code>a ? a : b</code>
|
|
|
|
|
|
|
|
If the first parameter is not @c 0, then it is returned,
|
|
|
|
otherwise the second parameter is returned. It is equivalent to
|
|
|
|
the construct: <code>a ? a : b</code>, but without
|
|
|
|
the side effect of duplicated evaluation of parameter
|
|
|
|
<code>a</code>.
|
|
|
|
|
|
|
|
Older gcc compiler implemented the non standard extension of
|
|
|
|
<code>a?:b</code>, which was exactly the same.
|
|
|
|
|
|
|
|
@param a criteria that is checked and returned if not @c 0
|
|
|
|
@param b alternative value to be returned if @c a is @c 0
|
|
|
|
@return <code>a ? a : b</code>
|
2005-11-29 12:42:01 +00:00
|
|
|
@pre \#include <mrw/stdext.hpp>
|
2005-02-18 15:47:23 +00:00
|
|
|
@pre @c T must be convertible to @c bool
|
|
|
|
*/
|
|
|
|
template<typename T> const T& ifelse(const T& a, const T& b) {
|
|
|
|
return a?a:b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Dual <code>?:</code> operation:
|
|
|
|
<code>a ? a : b</code>
|
|
|
|
|
|
|
|
@copydoc ifelse(const T& a, const T& b) */
|
|
|
|
template<typename T> const T* ifelse(const T* a, const T* b) {
|
|
|
|
return a?a:b;
|
|
|
|
}
|
|
|
|
|
2004-10-13 11:18:33 +00:00
|
|
|
/** @brief read one line from a stream
|
|
|
|
|
|
|
|
Reads one line from a stream, up to delimiter @c d.
|
2004-12-20 07:40:36 +00:00
|
|
|
The delimiter is not appended to the string.
|
2004-10-13 11:18:33 +00:00
|
|
|
|
|
|
|
@param is the stream to read from
|
|
|
|
@param d the end of line delimiter
|
|
|
|
@return the line read from the stream
|
2005-11-29 12:42:01 +00:00
|
|
|
@pre \#include <mrw/stdext.hpp>
|
2004-10-13 11:18:33 +00:00
|
|
|
*/
|
|
|
|
std::string getline(std::istream& is, char d = '\n');
|
|
|
|
|
|
|
|
/** @brief read one line from a stream
|
|
|
|
|
|
|
|
Reads one line from a stream, up to delimiter @c d.
|
2004-12-20 07:40:36 +00:00
|
|
|
The delimiter is not appended to the string.
|
2004-10-13 11:18:33 +00:00
|
|
|
|
|
|
|
@param is the stream to read from
|
|
|
|
@param s the string to place the line in
|
|
|
|
@param d the end of line delimiter
|
|
|
|
@return @c s: the line read from the stream
|
|
|
|
@return the stream after extraction of line
|
2005-11-29 12:42:01 +00:00
|
|
|
@pre \#include <mrw/stdext.hpp>
|
2004-10-13 11:18:33 +00:00
|
|
|
*/
|
|
|
|
std::istream& getline(std::istream& is, std::string& s, char d = '\n');
|
|
|
|
|
2004-12-17 16:27:28 +00:00
|
|
|
//@}
|
2004-12-20 07:40:36 +00:00
|
|
|
//@}
|
2004-10-11 18:30:16 +00:00
|
|
|
}
|
2005-02-18 15:47:23 +00:00
|
|
|
|
|
|
|
#endif
|