kwsys_stl_string.hxx.in 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License 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. # if defined(__WATCOMC__)
  27. namespace @KWSYS_NAMESPACE@
  28. {
  29. struct ios_istream_hack: public kwsys_ios::istream
  30. { void eatwhite() { this->@KWSYS_NAMESPACE@_ios::istream::eatwhite(); } };
  31. }
  32. # endif
  33. inline @KWSYS_NAMESPACE@_ios::istream&
  34. operator>>(@KWSYS_NAMESPACE@_ios::istream& is,
  35. @KWSYS_NAMESPACE@_stl::string& s)
  36. {
  37. // Keep track of the resulting state.
  38. int state = @KWSYS_NAMESPACE@_ios::ios::goodbit;
  39. // Save the width setting and set it back to zero.
  40. size_t n = static_cast<size_t>(is.width(0));
  41. // Clear any old contents of the output string.
  42. s.erase();
  43. // Skip leading whitespace.
  44. #if defined(__WATCOMC__)
  45. static_cast<@KWSYS_NAMESPACE@::ios_istream_hack&>(is).eatwhite();
  46. #else
  47. is.eatwhite();
  48. #endif
  49. @KWSYS_NAMESPACE@_ios::istream& okay = is;
  50. if(okay)
  51. {
  52. // Select a maximum possible length.
  53. if(n == 0 || n >= s.max_size())
  54. {
  55. n = s.max_size();
  56. }
  57. // Read until a space is found or the maximum length is reached.
  58. bool success = false;
  59. for(int c = is.peek(); (--n > 0 && c != EOF && !isspace(c)); c = is.peek())
  60. {
  61. s += static_cast<char>(c);
  62. success = true;
  63. is.ignore();
  64. }
  65. // Set flags for resulting state.
  66. if(is.peek() == EOF) { state |= @KWSYS_NAMESPACE@_ios::ios::eofbit; }
  67. if(!success) { state |= @KWSYS_NAMESPACE@_ios::ios::failbit; }
  68. }
  69. // Set the final result state.
  70. is.clear(state);
  71. return is;
  72. }
  73. #endif
  74. // Provide the ostream operator for the stl string if it is not
  75. // provided by the system or another copy of kwsys. Allow user code
  76. // to block this definition by defining the macro
  77. // @KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM
  78. // to avoid conflicts with other libraries. User code can test for
  79. // this definition by checking the macro
  80. // @KWSYS_NAMESPACE@_STL_STRING_OSTREAM_DEFINED
  81. #if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_OSTREAM && !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_OSTREAM) && !defined(KWSYS_STL_STRING_OSTREAM_DEFINED)
  82. # define KWSYS_STL_STRING_OSTREAM_DEFINED
  83. # define @KWSYS_NAMESPACE@_STL_STRING_OSTREAM_DEFINED
  84. # include <@KWSYS_NAMESPACE@/ios/iostream>
  85. inline @KWSYS_NAMESPACE@_ios::ostream&
  86. operator<<(@KWSYS_NAMESPACE@_ios::ostream& os,
  87. @KWSYS_NAMESPACE@_stl::string const& s)
  88. {
  89. return os << s.c_str();
  90. }
  91. #endif
  92. // Provide the operator!= for the stl string and char* if it is not
  93. // provided by the system or another copy of kwsys. Allow user code
  94. // to block this definition by defining the macro
  95. // @KWSYS_NAMESPACE@_STL_STRING_NO_NEQ_CHAR
  96. // to avoid conflicts with other libraries. User code can test for
  97. // this definition by checking the macro
  98. // @KWSYS_NAMESPACE@_STL_STRING_NEQ_CHAR_DEFINED
  99. #if !@KWSYS_NAMESPACE@_STL_STRING_HAVE_NEQ_CHAR && !defined(@KWSYS_NAMESPACE@_STL_STRING_NO_NEQ_CHAR) && !defined(KWSYS_STL_STRING_NEQ_CHAR_DEFINED)
  100. # define KWSYS_STL_STRING_NEQ_CHAR_DEFINED
  101. # define @KWSYS_NAMESPACE@_STL_STRING_NEQ_CHAR_DEFINED
  102. inline bool operator!=(@KWSYS_NAMESPACE@_stl::string const& s, const char* c)
  103. {
  104. return !(s == c);
  105. }
  106. inline bool operator!=(const char* c, @KWSYS_NAMESPACE@_stl::string const& s)
  107. {
  108. return !(s == c);
  109. }
  110. #endif