diff options
author | Brad King <brad.king@kitware.com> | 2005-03-29 13:20:08 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2005-03-29 13:20:08 (GMT) |
commit | 22f6d68be3e6630b21bb17de63273965de44ee45 (patch) | |
tree | 8225f2a9c0bb6fb085e482bc04f44bfb7e7a8e5d /Source/kwsys/kwsys_stl_string.hxx.in | |
parent | c7a2c80c178457b4d8a1525a6b0ccff9ca4e5afa (diff) | |
download | CMake-22f6d68be3e6630b21bb17de63273965de44ee45.zip CMake-22f6d68be3e6630b21bb17de63273965de44ee45.tar.gz CMake-22f6d68be3e6630b21bb17de63273965de44ee45.tar.bz2 |
ENH: Added istream and ostream operators for stl string in KWSys when using old streams that do not provide them.
Diffstat (limited to 'Source/kwsys/kwsys_stl_string.hxx.in')
-rw-r--r-- | Source/kwsys/kwsys_stl_string.hxx.in | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/Source/kwsys/kwsys_stl_string.hxx.in b/Source/kwsys/kwsys_stl_string.hxx.in new file mode 100644 index 0000000..7d45649 --- /dev/null +++ b/Source/kwsys/kwsys_stl_string.hxx.in @@ -0,0 +1,92 @@ +/*========================================================================= + + Program: KWSys - Kitware System Library + Module: $RCSfile$ + + Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved. + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +// This header is extra code for <@KWSYS_NAMESPACE@/stl/string>. +#if !defined(@KWSYS_NAMESPACE@_stl_string_including_hxx) +# error "The header <@KWSYS_NAMESPACE@/stl/string.hxx> may be included only by <@KWSYS_NAMESPACE@/stl/string>." +#endif + +// Provide the istream operator for the stl string if it is not +// provided by the system or another copy of kwsys. Allow user code +// to block this definition by defining the macro +// @KWSYS_NAMESPACE@_STL_STRING_NO_ISTREAM +// to avoid conflicts with other libraries. +#if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_ISTREAM && \ + !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_ISTREAM) && \ + !defined(KWSYS_STL_STRING_ISTREAM_DEFINED) +# define KWSYS_STL_STRING_ISTREAM_DEFINED +# include <ctype.h> // isspace +# include <@KWSYS_NAMESPACE@/ios/iostream> +inline @KWSYS_NAMESPACE@_ios::istream& +operator>>(@KWSYS_NAMESPACE@_ios::istream& is, + @KWSYS_NAMESPACE@_stl::string& s) +{ + // Keep track of the resulting state. + int state = @KWSYS_NAMESPACE@_ios::ios::goodbit; + + // Save the width setting and set it back to zero. + size_t n = static_cast<size_t>(is.width(0)); + + // Clear any old contents of the output string. + s.erase(); + + // Skip leading whitespace. + is.eatwhite(); + istream& okay = is; + + if(okay) + { + // Select a maximum possible length. + if(n == 0 || n >= s.max_size()) + { + n = s.max_size(); + } + + // Read until a space is found or the maximum length is reached. + bool success = false; + for(int c = is.peek(); (--n > 0 && c != EOF && !isspace(c)); c = is.peek()) + { + s += static_cast<char>(c); + success = true; + is.ignore(); + } + + // Set flags for resulting state. + if(is.peek() == EOF) { state |= @KWSYS_NAMESPACE@_ios::ios::eofbit; } + if(success) { state |= @KWSYS_NAMESPACE@_ios::ios::failbit; } + } + + // Set the final result state. + is.clear(state); + return is; +} +#endif + +// Provide the ostream operator for the stl string if it is not +// provided by the system or another copy of kwsys. Allow user code +// to block this definition by defining the macro +// @KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM +// to avoid conflicts with other libraries. +#if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_OSTREAM && \ + !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM) && \ + !defined(KWSYS_STL_STRING_OSTREAM_DEFINED) +# define KWSYS_STL_STRING_OSTREAM_DEFINED +# include <@KWSYS_NAMESPACE@/ios/iostream> +inline @KWSYS_NAMESPACE@_ios::ostream& +operator<<(@KWSYS_NAMESPACE@_ios::ostream& os, + @KWSYS_NAMESPACE@_stl::string const& s) +{ + return os << s.c_str(); +} +#endif |