libstdc++
|
00001 // Helpers for quoted stream manipulators -*- C++ -*- 00002 00003 // Copyright (C) 2013-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/quoted_string.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{iomanip} 00028 */ 00029 00030 #ifndef _GLIBCXX_QUOTED_STRING_H 00031 #define _GLIBCXX_QUOTED_STRING_H 1 00032 00033 #pragma GCC system_header 00034 00035 #if __cplusplus < 201103L 00036 # include <bits/c++0x_warning.h> 00037 #else 00038 #include <sstream> 00039 00040 namespace std _GLIBCXX_VISIBILITY(default) 00041 { 00042 namespace __detail { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 /** 00046 * @brief Struct for delimited strings. 00047 */ 00048 template<typename _String, typename _CharT> 00049 struct _Quoted_string 00050 { 00051 static_assert(is_reference<_String>::value 00052 || is_pointer<_String>::value, 00053 "String type must be pointer or reference"); 00054 00055 _Quoted_string(_String __str, _CharT __del, _CharT __esc) 00056 : _M_string(__str), _M_delim{__del}, _M_escape{__esc} 00057 { } 00058 00059 _Quoted_string& 00060 operator=(_Quoted_string&) = delete; 00061 00062 _String _M_string; 00063 _CharT _M_delim; 00064 _CharT _M_escape; 00065 }; 00066 00067 /** 00068 * @brief Inserter for quoted strings. 00069 * 00070 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00071 * DR 2344 quoted()'s interaction with padding is unclear 00072 */ 00073 template<typename _CharT, typename _Traits> 00074 std::basic_ostream<_CharT, _Traits>& 00075 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 00076 const _Quoted_string<const _CharT*, _CharT>& __str) 00077 { 00078 std::basic_ostringstream<_CharT, _Traits> __ostr; 00079 __ostr << __str._M_delim; 00080 for (const _CharT* __c = __str._M_string; *__c; ++__c) 00081 { 00082 if (*__c == __str._M_delim || *__c == __str._M_escape) 00083 __ostr << __str._M_escape; 00084 __ostr << *__c; 00085 } 00086 __ostr << __str._M_delim; 00087 00088 return __os << __ostr.str(); 00089 } 00090 00091 /** 00092 * @brief Inserter for quoted strings. 00093 * 00094 * _GLIBCXX_RESOLVE_LIB_DEFECTS 00095 * DR 2344 quoted()'s interaction with padding is unclear 00096 */ 00097 template<typename _CharT, typename _Traits, typename _String> 00098 std::basic_ostream<_CharT, _Traits>& 00099 operator<<(std::basic_ostream<_CharT, _Traits>& __os, 00100 const _Quoted_string<_String, _CharT>& __str) 00101 { 00102 std::basic_ostringstream<_CharT, _Traits> __ostr; 00103 __ostr << __str._M_delim; 00104 for (auto& __c : __str._M_string) 00105 { 00106 if (__c == __str._M_delim || __c == __str._M_escape) 00107 __ostr << __str._M_escape; 00108 __ostr << __c; 00109 } 00110 __ostr << __str._M_delim; 00111 00112 return __os << __ostr.str(); 00113 } 00114 00115 /** 00116 * @brief Extractor for delimited strings. 00117 * The left and right delimiters can be different. 00118 */ 00119 template<typename _CharT, typename _Traits, typename _Alloc> 00120 std::basic_istream<_CharT, _Traits>& 00121 operator>>(std::basic_istream<_CharT, _Traits>& __is, 00122 const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&, 00123 _CharT>& __str) 00124 { 00125 _CharT __c; 00126 __is >> __c; 00127 if (!__is.good()) 00128 return __is; 00129 if (__c != __str._M_delim) 00130 { 00131 __is.unget(); 00132 __is >> __str._M_string; 00133 return __is; 00134 } 00135 __str._M_string.clear(); 00136 std::ios_base::fmtflags __flags 00137 = __is.flags(__is.flags() & ~std::ios_base::skipws); 00138 do 00139 { 00140 __is >> __c; 00141 if (!__is.good()) 00142 break; 00143 if (__c == __str._M_escape) 00144 { 00145 __is >> __c; 00146 if (!__is.good()) 00147 break; 00148 } 00149 else if (__c == __str._M_delim) 00150 break; 00151 __str._M_string += __c; 00152 } 00153 while (true); 00154 __is.setf(__flags); 00155 00156 return __is; 00157 } 00158 00159 _GLIBCXX_END_NAMESPACE_VERSION 00160 } // namespace __detail 00161 } // namespace std 00162 00163 #endif // C++11 00164 #endif /* _GLIBCXX_QUOTED_STRING_H */