From 38f6f02c763251f6360106f74862b63d16241012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Wed, 13 Oct 2004 11:18:33 +0000 Subject: [PATCH] getline reads a whole line from a stream --- mrw/stdext.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ mrw/stdext.hpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 mrw/stdext.cpp diff --git a/mrw/stdext.cpp b/mrw/stdext.cpp new file mode 100644 index 0000000..2936dd9 --- /dev/null +++ b/mrw/stdext.cpp @@ -0,0 +1,44 @@ +/** @file + + $Id$ + + $Date$ + $Author$ + + @copy © Marc Wäckerlin + @license LGPL, see file COPYING + + $Log$ + Revision 1.1 2004/10/13 11:18:33 marc + getline reads a whole line from a stream + + +*/ + +#include + +std::string mrw::getline(std::istream& is, char d) { + char buf[255]; + char c; + std::string s; + do { + if (is && is.get(buf, 255, d)) { + s += buf; + if (is.get(c) && c!=d) s+=c; + } + } while (is.good() && !is.eof() && c!=d); + return s; +} + +std::istream& mrw::getline(std::istream& is, std::string& s, char d) { + char buf[255]; + char c; + s.clear(); + do { + if (is && is.get(buf, 255, d)) { + s += buf; + if (is.get(c) && c!=d) s+=c; + } + } while (is.good() && !is.eof() && c!=d); + return is; +} diff --git a/mrw/stdext.hpp b/mrw/stdext.hpp index e37cf57..867353c 100644 --- a/mrw/stdext.hpp +++ b/mrw/stdext.hpp @@ -9,20 +9,72 @@ @license LGPL, see file COPYING $Log$ + Revision 1.2 2004/10/13 11:18:33 marc + getline reads a whole line from a stream + Revision 1.1 2004/10/11 18:30:16 marc *** empty log message *** */ +#include +#include + namespace mrw { - + + /** @brief get the lower of two values + + Get the lower of two values. + If both values are equal, @c a is returned. + + @param a first value to compare + @param b second value to compare + @return the lower of both values + @pre #include + @pre @c T must support bool operator>(conbst T&, constT&) + */ template const T& min(const T& a, const T& b) { return a > b ? b : a; } + /** @brief get the higher of two values + + Get the higher of two values. + If both values are equal, @c a is returned. + + @param a first value to compare + @param b second value to compare + @return the higher of both values + @pre #include + @pre @c T must support bool operator<(conbst T&, constT&) + */ template const T& max(const T& a, const T& b) { return a < b ? b : a; } + /** @brief read one line from a stream + + Reads one line from a stream, up to delimiter @c d. + + @param is the stream to read from + @param d the end of line delimiter + @return the line read from the stream + @pre #include + */ + 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. + + @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 + @pre #include + */ + std::istream& getline(std::istream& is, std::string& s, char d = '\n'); + }