kwsys_stl_string.hxx.in 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. // This header is extra code for <@KWSYS_NAMESPACE@/stl/string>.
  11. #if !defined(@KWSYS_NAMESPACE@_stl_string_including_hxx)
  12. # error "The header <@KWSYS_NAMESPACE@/stl/string.hxx> may be included only by <@KWSYS_NAMESPACE@/stl/string>."
  13. #endif
  14. // Provide the istream operator for the stl string if it is not
  15. // provided by the system or another copy of kwsys. Allow user code
  16. // to block this definition by defining the macro
  17. // @KWSYS_NAMESPACE@_STL_STRING_NO_ISTREAM
  18. // to avoid conflicts with other libraries. User code can test for
  19. // this definition by checking the macro
  20. // @KWSYS_NAMESPACE@_STL_STRING_ISTREAM_DEFINED
  21. #if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_ISTREAM && !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_ISTREAM) && !defined(KWSYS_STL_STRING_ISTREAM_DEFINED)
  22. # define KWSYS_STL_STRING_ISTREAM_DEFINED
  23. # define @KWSYS_NAMESPACE@_STL_STRING_ISTREAM_DEFINED
  24. # include <ctype.h> // isspace
  25. # include <@KWSYS_NAMESPACE@/ios/iostream>
  26. inline @KWSYS_NAMESPACE@_ios::istream&
  27. operator>>(@KWSYS_NAMESPACE@_ios::istream& is,
  28. @KWSYS_NAMESPACE@_stl::string& s)
  29. {
  30. // Keep track of the resulting state.
  31. int state = @KWSYS_NAMESPACE@_ios::ios::goodbit;
  32. // Save the width setting and set it back to zero.
  33. size_t n = static_cast<size_t>(is.width(0));
  34. // Clear any old contents of the output string.
  35. s.erase();
  36. // Skip leading whitespace.
  37. is.eatwhite();
  38. istream& okay = is;
  39. if(okay)
  40. {
  41. // Select a maximum possible length.
  42. if(n == 0 || n >= s.max_size())
  43. {
  44. n = s.max_size();
  45. }
  46. // Read until a space is found or the maximum length is reached.
  47. bool success = false;
  48. for(int c = is.peek(); (--n > 0 && c != EOF && !isspace(c)); c = is.peek())
  49. {
  50. s += static_cast<char>(c);
  51. success = true;
  52. is.ignore();
  53. }
  54. // Set flags for resulting state.
  55. if(is.peek() == EOF) { state |= @KWSYS_NAMESPACE@_ios::ios::eofbit; }
  56. if(success) { state |= @KWSYS_NAMESPACE@_ios::ios::failbit; }
  57. }
  58. // Set the final result state.
  59. is.clear(state);
  60. return is;
  61. }
  62. #endif
  63. // Provide the ostream operator for the stl string if it is not
  64. // provided by the system or another copy of kwsys. Allow user code
  65. // to block this definition by defining the macro
  66. // @KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM
  67. // to avoid conflicts with other libraries. User code can test for
  68. // this definition by checking the macro
  69. // @KWSYS_NAMESPACE@_STL_STRING_OSTREAM_DEFINED
  70. #if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_OSTREAM && !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM) && !defined(KWSYS_STL_STRING_OSTREAM_DEFINED)
  71. # define KWSYS_STL_STRING_OSTREAM_DEFINED
  72. # define @KWSYS_NAMESPACE@_STL_STRING_OSTREAM_DEFINED
  73. # include <@KWSYS_NAMESPACE@/ios/iostream>
  74. inline @KWSYS_NAMESPACE@_ios::ostream&
  75. operator<<(@KWSYS_NAMESPACE@_ios::ostream& os,
  76. @KWSYS_NAMESPACE@_stl::string const& s)
  77. {
  78. return os << s.c_str();
  79. }
  80. #endif
  81. // Provide the operator!= for the stl string and char* if it is not
  82. // provided by the system or another copy of kwsys. Allow user code
  83. // to block this definition by defining the macro
  84. // @KWSYS_NAMESPACE@_STL_STRING_NO_NEQ_CHAR
  85. // to avoid conflicts with other libraries. User code can test for
  86. // this definition by checking the macro
  87. // @KWSYS_NAMESPACE@_STL_STRING_NEQ_CHAR_DEFINED
  88. #if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_NEQ_CHAR && !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_NEQ_CHAR) && !defined(KWSYS_STL_STRING_NEQ_CHAR_DEFINED)
  89. # define KWSYS_STL_STRING_NEQ_CHAR_DEFINED
  90. # define @KWSYS_NAMESPACE@_STL_STRING_NEQ_CHAR_DEFINED
  91. inline bool operator!=(@KWSYS_NAMESPACE@_stl::string const& s, const char* c)
  92. {
  93. return !(s == c);
  94. }
  95. inline bool operator!=(const char* c, @KWSYS_NAMESPACE@_stl::string const& s)
  96. {
  97. return !(s == c);
  98. }
  99. #endif