C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.1 KiB
110 lines
3.1 KiB
21 years ago
|
/** @file
|
||
|
|
||
|
$Id$
|
||
|
|
||
|
$Date$
|
||
|
$Author$
|
||
|
|
||
|
@copy © Marc Wäckerlin
|
||
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
||
|
|
||
|
*/
|
||
|
|
||
21 years ago
|
#ifndef __MRW__STDEXT_HPP__
|
||
|
#define __MRW__STDEXT_HPP__
|
||
|
|
||
21 years ago
|
#include <string>
|
||
|
#include <iostream>
|
||
21 years ago
|
#include <algorithm>
|
||
21 years ago
|
|
||
21 years ago
|
namespace mrw {
|
||
21 years ago
|
|
||
21 years ago
|
/** @addtogroup StdExt
|
||
|
*/
|
||
|
//@{
|
||
|
|
||
21 years ago
|
/** @defgroup stdextFunction Useful Global Functions
|
||
21 years ago
|
|
||
21 years ago
|
- @ref getline read exactly one line from a stream
|
||
|
- @ref ifelse implements a dual <code>?:</code> operator, like:
|
||
|
<code>a ? a : b</code>
|
||
|
|
||
20 years ago
|
@section stdextGetline Read Line
|
||
21 years ago
|
|
||
21 years ago
|
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:
|
||
20 years ago
|
std::string line(mrw::getline(std::cin));
|
||
21 years ago
|
// second syntax returns the stream:
|
||
20 years ago
|
while (mrw::getline(std::cin, line))
|
||
21 years ago
|
std::cout<<"Read: "<<line<<std::endl;
|
||
|
@endcode
|
||
|
*/
|
||
|
//@{
|
||
|
|
||
21 years ago
|
/** @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>
|
||
14 years ago
|
@pre \#include <mrw/stdext.hxx>
|
||
21 years ago
|
@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;
|
||
|
}
|
||
|
|
||
21 years ago
|
/** @brief read one line from a stream
|
||
|
|
||
|
Reads one line from a stream, up to delimiter @c d.
|
||
21 years ago
|
The delimiter is not appended to the string.
|
||
21 years ago
|
|
||
|
@param is the stream to read from
|
||
|
@param d the end of line delimiter
|
||
|
@return the line read from the stream
|
||
14 years ago
|
@pre \#include <mrw/stdext.hxx>
|
||
21 years ago
|
*/
|
||
|
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.
|
||
21 years ago
|
The delimiter is not appended to the string.
|
||
21 years ago
|
|
||
|
@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
|
||
14 years ago
|
@pre \#include <mrw/stdext.hxx>
|
||
21 years ago
|
*/
|
||
|
std::istream& getline(std::istream& is, std::string& s, char d = '\n');
|
||
|
|
||
21 years ago
|
//@}
|
||
21 years ago
|
//@}
|
||
21 years ago
|
}
|
||
21 years ago
|
|
||
|
#endif
|