From 22f6d68be3e6630b21bb17de63273965de44ee45 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 29 Mar 2005 08:20:08 -0500 Subject: ENH: Added istream and ostream operators for stl string in KWSys when using old streams that do not provide them. --- Source/kwsys/CMakeLists.txt | 31 ++++++++++- Source/kwsys/Configure.hxx.in | 24 ++++++--- Source/kwsys/kwsysPlatformCxxTests.cmake | 2 +- Source/kwsys/kwsysPlatformCxxTests.cxx | 24 +++++++++ Source/kwsys/kwsys_stl.h.in | 31 ----------- Source/kwsys/kwsys_stl.hxx.in | 32 +++++++++++ Source/kwsys/kwsys_stl_string.hxx.in | 92 ++++++++++++++++++++++++++++++++ bootstrap | 11 +++- 8 files changed, 205 insertions(+), 42 deletions(-) delete mode 100644 Source/kwsys/kwsys_stl.h.in create mode 100644 Source/kwsys/kwsys_stl.hxx.in create mode 100644 Source/kwsys/kwsys_stl_string.hxx.in diff --git a/Source/kwsys/CMakeLists.txt b/Source/kwsys/CMakeLists.txt index c3455f3..c2f4b66 100644 --- a/Source/kwsys/CMakeLists.txt +++ b/Source/kwsys/CMakeLists.txt @@ -175,6 +175,21 @@ ELSE(KWSYS_IOS_USE_SSTREAM) ENDIF(KWSYS_IOS_USE_STRSTREAM_H) ENDIF(KWSYS_IOS_USE_SSTREAM) +IF(KWSYS_IOS_USE_ANSI) + # ANSI streams always have string operators. + SET(KWSYS_STL_STRING_HAVE_OSTREAM 1) + SET(KWSYS_STL_STRING_HAVE_ISTREAM 1) +ELSE(KWSYS_IOS_USE_ANSI) + # There may not be string operators for old streams. + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES + -DKWSYS_STL_HAVE_STD=${KWSYS_STL_HAVE_STD}) + KWSYS_PLATFORM_CXX_TEST(KWSYS_STL_STRING_HAVE_OSTREAM + "Checking whether stl string has ostream operator<<" DIRECT) + KWSYS_PLATFORM_CXX_TEST(KWSYS_STL_STRING_HAVE_ISTREAM + "Checking whether stl string has istream operator>>" DIRECT) + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES) +ENDIF(KWSYS_IOS_USE_ANSI) + IF(UNIX) KWSYS_PLATFORM_CXX_TEST(KWSYS_STAT_HAS_ST_MTIM "Checking whether struct stat has st_mtim member" DIRECT) @@ -210,11 +225,25 @@ INCLUDE_DIRECTORIES(${KWSYS_HEADER_ROOT}) #----------------------------------------------------------------------------- # Create STL header wrappers to block warnings in the STL headers and # give standard names by which they may be included. +SET(KWSYS_STL_HEADER_EXTRA_string 1) FOREACH(header algorithm deque iterator list map numeric queue set stack string utility vector) # Configure the header wrapper. SET(KWSYS_STL_HEADER "${header}") - CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/kwsys_stl.h.in + IF(KWSYS_STL_HEADER_EXTRA_${header}) + SET(KWSYS_STL_HEADER_EXTRA + "#define ${KWSYS_NAMESPACE}_stl_${header}_including_hxx\n# include <${KWSYS_NAMESPACE}/stl/${header}.hxx>\n#undef ${KWSYS_NAMESPACE}_stl_${header}_including_hxx\n") + CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/kwsys_stl_${header}.hxx.in + ${KWSYS_HEADER_DIR}/stl/${header}.hxx + @ONLY IMMEDIATE) + IF(KWSYS_HEADER_INSTALL_DIR) + INSTALL_FILES(${KWSYS_HEADER_INSTALL_DIR}/${KWSYS_NAMESPACE}/stl + FILES ${KWSYS_HEADER_DIR}/stl/${header}.hxx) + ENDIF(KWSYS_HEADER_INSTALL_DIR) + ELSE(KWSYS_STL_HEADER_EXTRA_${header}) + SET(KWSYS_STL_HEADER_EXTRA "") + ENDIF(KWSYS_STL_HEADER_EXTRA_${header}) + CONFIGURE_FILE(${PROJECT_SOURCE_DIR}/kwsys_stl.hxx.in ${KWSYS_HEADER_DIR}/stl/${header} @ONLY IMMEDIATE) diff --git a/Source/kwsys/Configure.hxx.in b/Source/kwsys/Configure.hxx.in index 1b9df6b..a55f991 100644 --- a/Source/kwsys/Configure.hxx.in +++ b/Source/kwsys/Configure.hxx.in @@ -35,6 +35,12 @@ /* Whether STL is in std namespace. */ #define @KWSYS_NAMESPACE@_STL_HAVE_STD @KWSYS_STL_HAVE_STD@ +/* Whether the STL string has operator<< for ostream. */ +#define @KWSYS_NAMESPACE@_STL_STRING_HAVE_OSTREAM @KWSYS_STL_STRING_HAVE_OSTREAM@ + +/* Whether the STL string has operator>> for istream. */ +#define @KWSYS_NAMESPACE@_STL_STRING_HAVE_ISTREAM @KWSYS_STL_STRING_HAVE_ISTREAM@ + /* Define the stl namespace macro. */ #if @KWSYS_NAMESPACE@_STL_HAVE_STD # define @KWSYS_NAMESPACE@_stl std @@ -64,14 +70,16 @@ # define kwsys_stl @KWSYS_NAMESPACE@_stl # define kwsys_ios @KWSYS_NAMESPACE@_ios # endif -# define KWSYS_NAME_IS_KWSYS @KWSYS_NAMESPACE@_NAME_IS_KWSYS -# define KWSYS_STL_HAVE_STD @KWSYS_NAMESPACE@_STL_HAVE_STD -# define KWSYS_IOS_HAVE_STD @KWSYS_NAMESPACE@_IOS_HAVE_STD -# define KWSYS_IOS_USE_ANSI @KWSYS_NAMESPACE@_IOS_USE_ANSI -# define KWSYS_IOS_USE_SSTREAM @KWSYS_NAMESPACE@_IOS_USE_SSTREAM -# define KWSYS_IOS_USE_STRSTREAM_H @KWSYS_NAMESPACE@_IOS_USE_STRSTREAM_H -# define KWSYS_IOS_USE_STRSTREA_H @KWSYS_NAMESPACE@_IOS_USE_STRSTREA_H -# define KWSYS_STAT_HAS_ST_MTIM @KWSYS_NAMESPACE@_STAT_HAS_ST_MTIM +# define KWSYS_NAME_IS_KWSYS @KWSYS_NAMESPACE@_NAME_IS_KWSYS +# define KWSYS_STL_HAVE_STD @KWSYS_NAMESPACE@_STL_HAVE_STD +# define KWSYS_IOS_HAVE_STD @KWSYS_NAMESPACE@_IOS_HAVE_STD +# define KWSYS_IOS_USE_ANSI @KWSYS_NAMESPACE@_IOS_USE_ANSI +# define KWSYS_IOS_USE_SSTREAM @KWSYS_NAMESPACE@_IOS_USE_SSTREAM +# define KWSYS_IOS_USE_STRSTREAM_H @KWSYS_NAMESPACE@_IOS_USE_STRSTREAM_H +# define KWSYS_IOS_USE_STRSTREA_H @KWSYS_NAMESPACE@_IOS_USE_STRSTREA_H +# define KWSYS_STAT_HAS_ST_MTIM @KWSYS_NAMESPACE@_STAT_HAS_ST_MTIM +# define KWSYS_STL_STRING_HAVE_OSTREAM @KWSYS_NAMESPACE@_STL_STRING_HAVE_OSTREAM +# define KWSYS_STL_STRING_HAVE_ISTREAM @KWSYS_NAMESPACE@_STL_STRING_HAVE_ISTREAM #endif #endif diff --git a/Source/kwsys/kwsysPlatformCxxTests.cmake b/Source/kwsys/kwsysPlatformCxxTests.cmake index 1b5b98a..b3004ca 100644 --- a/Source/kwsys/kwsysPlatformCxxTests.cmake +++ b/Source/kwsys/kwsysPlatformCxxTests.cmake @@ -4,7 +4,7 @@ MACRO(KWSYS_PLATFORM_CXX_TEST var description invert) TRY_COMPILE(${var}_COMPILED ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/kwsysPlatformCxxTests.cxx - COMPILE_DEFINITIONS -DTEST_${var} + COMPILE_DEFINITIONS -DTEST_${var} ${KWSYS_PLATFORM_CXX_TEST_DEFINES} OUTPUT_VARIABLE OUTPUT) IF(${var}_COMPILED) WRITE_FILE(${CMAKE_CURRENT_BINARY_DIR}/CMakeOutput.log diff --git a/Source/kwsys/kwsysPlatformCxxTests.cxx b/Source/kwsys/kwsysPlatformCxxTests.cxx index a799c50..d8454fd 100644 --- a/Source/kwsys/kwsysPlatformCxxTests.cxx +++ b/Source/kwsys/kwsysPlatformCxxTests.cxx @@ -30,6 +30,30 @@ int main() { return 0; } int main() { return 0; } #endif +#ifdef TEST_KWSYS_STL_STRING_HAVE_OSTREAM +# if KWSYS_STL_HAVE_STD +# define kwsys_stl std +# else +# define kwsys_stl +# endif +# include +# include +void f(ostream& os, const kwsys_stl::string& s) { os << s; } +int main() { return 0; } +#endif + +#ifdef TEST_KWSYS_STL_STRING_HAVE_ISTREAM +# if KWSYS_STL_HAVE_STD +# define kwsys_stl std +# else +# define kwsys_stl +# endif +# include +# include +void f(istream& is, kwsys_stl::string& s) { is >> s; } +int main() { return 0; } +#endif + #ifdef TEST_KWSYS_STAT_HAS_ST_MTIM #include #include diff --git a/Source/kwsys/kwsys_stl.h.in b/Source/kwsys/kwsys_stl.h.in deleted file mode 100644 index 893ce46..0000000 --- a/Source/kwsys/kwsys_stl.h.in +++ /dev/null @@ -1,31 +0,0 @@ -/*========================================================================= - - 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. - -=========================================================================*/ -#ifndef @KWSYS_NAMESPACE@_stl_@KWSYS_STL_HEADER@ -#define @KWSYS_NAMESPACE@_stl_@KWSYS_STL_HEADER@ - -#include <@KWSYS_NAMESPACE@/Configure.hxx> - -#ifdef _MSC_VER -# pragma warning (push, 1) -# pragma warning (disable: 4702) -# pragma warning (disable: 4786) -#endif - -#include <@KWSYS_STL_HEADER@> - -#ifdef _MSC_VER -# pragma warning(pop) -#endif - -#endif diff --git a/Source/kwsys/kwsys_stl.hxx.in b/Source/kwsys/kwsys_stl.hxx.in new file mode 100644 index 0000000..5a58fbf --- /dev/null +++ b/Source/kwsys/kwsys_stl.hxx.in @@ -0,0 +1,32 @@ +/*========================================================================= + + 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. + +=========================================================================*/ +#ifndef @KWSYS_NAMESPACE@_stl_@KWSYS_STL_HEADER@ +#define @KWSYS_NAMESPACE@_stl_@KWSYS_STL_HEADER@ + +#include <@KWSYS_NAMESPACE@/Configure.hxx> + +#ifdef _MSC_VER +# pragma warning (push, 1) +# pragma warning (disable: 4702) +# pragma warning (disable: 4786) +#endif + +#include <@KWSYS_STL_HEADER@> + +#ifdef _MSC_VER +# pragma warning(pop) +#endif + +@KWSYS_STL_HEADER_EXTRA@ +#endif 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 // 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(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(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 diff --git a/bootstrap b/bootstrap index a0026c4..4449e52 100755 --- a/bootstrap +++ b/bootstrap @@ -200,6 +200,8 @@ cmake_kwsys_config_replace_string () s/@KWSYS_IOS_USE_STRSTREAM_H@/${KWSYS_IOS_USE_STRSTREAM_H}/g; s/@KWSYS_IOS_USE_STRSTREA_H@/${KWSYS_IOS_USE_STRSTREA_H}/g; s/@KWSYS_STL_HAVE_STD@/${KWSYS_STL_HAVE_STD}/g; + s/@KWSYS_STL_STRING_HAVE_ISTREAM@/${KWSYS_STL_STRING_HAVE_ISTREAM}/g; + s/@KWSYS_STL_STRING_HAVE_OSTREAM@/${KWSYS_STL_STRING_HAVE_OSTREAM}/g; s/@KWSYS_STAT_HAS_ST_MTIM@/${KWSYS_STAT_HAS_ST_MTIM}/g;}" >> "${OUTFILE}.tmp" if [ -f "${OUTFILE}.tmp" ]; then if diff "${OUTFILE}" "${OUTFILE}.tmp" > /dev/null 2> /dev/null ; then @@ -708,6 +710,10 @@ KWSYS_IOS_USE_ANSI=0 KWSYS_STL_HAVE_STD=0 KWSYS_STAT_HAS_ST_MTIM=0 +# Hardcode these kwsys features. They work on all known UNIX compilers anyway. +KWSYS_STL_STRING_HAVE_ISTREAM=1 +KWSYS_STL_STRING_HAVE_OSTREAM=1 + if cmake_try_run "${cmake_cxx_compiler}" \ "${cmake_cxx_flags} -DTEST_KWSYS_STL_HAVE_STD" \ "${cmake_source_dir}/Source/kwsys/kwsysPlatformCxxTests.cxx" >> cmake_bootstrap.log 2>&1; then @@ -868,7 +874,10 @@ for a in ${KWSYS_IOS_FILES}; do "${cmake_bootstrap_dir}/cmsys/ios/${a}" KWSYS_NAMESPACE cmsys done -cmake_replace_string "${cmake_source_dir}/Source/kwsys/kwsys_stl.h.in" \ +cmake_replace_string "${cmake_source_dir}/Source/kwsys/kwsys_stl.hxx.in" \ + "${cmake_bootstrap_dir}/cmsys/stl/stl.hxx.in" KWSYS_STL_HEADER_EXTRA "" + +cmake_replace_string "${cmake_bootstrap_dir}/cmsys/stl/stl.hxx.in" \ "${cmake_bootstrap_dir}/cmsys/stl/stl.h.in" KWSYS_NAMESPACE cmsys for a in string vector map; do -- cgit v0.12