138fd1498Szrj // Helpers for quoted stream manipulators -*- C++ -*- 238fd1498Szrj 338fd1498Szrj // Copyright (C) 2013-2018 Free Software Foundation, Inc. 438fd1498Szrj // 538fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 638fd1498Szrj // software; you can redistribute it and/or modify it under the 738fd1498Szrj // terms of the GNU General Public License as published by the 838fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 938fd1498Szrj // any later version. 1038fd1498Szrj 1138fd1498Szrj // This library is distributed in the hope that it will be useful, 1238fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 1338fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1438fd1498Szrj // GNU General Public License for more details. 1538fd1498Szrj 1638fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 1738fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 1838fd1498Szrj // 3.1, as published by the Free Software Foundation. 1938fd1498Szrj 2038fd1498Szrj // You should have received a copy of the GNU General Public License and 2138fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 2238fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 2338fd1498Szrj // <http://www.gnu.org/licenses/>. 2438fd1498Szrj 2538fd1498Szrj /** @file bits/quoted_string.h 2638fd1498Szrj * This is an internal header file, included by other library headers. 2738fd1498Szrj * Do not attempt to use it directly. @headername{iomanip} 2838fd1498Szrj */ 2938fd1498Szrj 3038fd1498Szrj #ifndef _GLIBCXX_QUOTED_STRING_H 3138fd1498Szrj #define _GLIBCXX_QUOTED_STRING_H 1 3238fd1498Szrj 3338fd1498Szrj #pragma GCC system_header 3438fd1498Szrj 3538fd1498Szrj #if __cplusplus < 201103L 3638fd1498Szrj # include <bits/c++0x_warning.h> 3738fd1498Szrj #else 3838fd1498Szrj #include <sstream> 3938fd1498Szrj 4038fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 4138fd1498Szrj { 4238fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 4338fd1498Szrj 4438fd1498Szrj namespace __detail { 4538fd1498Szrj /** 4638fd1498Szrj * @brief Struct for delimited strings. 4738fd1498Szrj */ 4838fd1498Szrj template<typename _String, typename _CharT> 4938fd1498Szrj struct _Quoted_string 5038fd1498Szrj { 5138fd1498Szrj static_assert(is_reference<_String>::value 5238fd1498Szrj || is_pointer<_String>::value, 5338fd1498Szrj "String type must be pointer or reference"); 5438fd1498Szrj 5538fd1498Szrj _Quoted_string(_String __str, _CharT __del, _CharT __esc) 5638fd1498Szrj : _M_string(__str), _M_delim{__del}, _M_escape{__esc} 5738fd1498Szrj { } 5838fd1498Szrj 5938fd1498Szrj _Quoted_string& 6038fd1498Szrj operator=(_Quoted_string&) = delete; 6138fd1498Szrj 6238fd1498Szrj _String _M_string; 6338fd1498Szrj _CharT _M_delim; 6438fd1498Szrj _CharT _M_escape; 6538fd1498Szrj }; 6638fd1498Szrj 67*58e805e6Szrj #if __cplusplus >= 201703L 68*58e805e6Szrj template<typename _CharT, typename _Traits> 69*58e805e6Szrj struct _Quoted_string<basic_string_view<_CharT, _Traits>, _CharT> 70*58e805e6Szrj { 71*58e805e6Szrj _Quoted_string(basic_string_view<_CharT, _Traits> __str, 72*58e805e6Szrj _CharT __del, _CharT __esc) 73*58e805e6Szrj : _M_string(__str), _M_delim{__del}, _M_escape{__esc} 74*58e805e6Szrj { } 75*58e805e6Szrj 76*58e805e6Szrj _Quoted_string& 77*58e805e6Szrj operator=(_Quoted_string&) = delete; 78*58e805e6Szrj 79*58e805e6Szrj basic_string_view<_CharT, _Traits> _M_string; 80*58e805e6Szrj _CharT _M_delim; 81*58e805e6Szrj _CharT _M_escape; 82*58e805e6Szrj }; 83*58e805e6Szrj #endif // C++17 84*58e805e6Szrj 8538fd1498Szrj /** 8638fd1498Szrj * @brief Inserter for quoted strings. 8738fd1498Szrj * 8838fd1498Szrj * _GLIBCXX_RESOLVE_LIB_DEFECTS 8938fd1498Szrj * DR 2344 quoted()'s interaction with padding is unclear 9038fd1498Szrj */ 9138fd1498Szrj template<typename _CharT, typename _Traits> 9238fd1498Szrj std::basic_ostream<_CharT, _Traits>& 9338fd1498Szrj operator<<(std::basic_ostream<_CharT, _Traits>& __os, 9438fd1498Szrj const _Quoted_string<const _CharT*, _CharT>& __str) 9538fd1498Szrj { 9638fd1498Szrj std::basic_ostringstream<_CharT, _Traits> __ostr; 9738fd1498Szrj __ostr << __str._M_delim; 9838fd1498Szrj for (const _CharT* __c = __str._M_string; *__c; ++__c) 9938fd1498Szrj { 10038fd1498Szrj if (*__c == __str._M_delim || *__c == __str._M_escape) 10138fd1498Szrj __ostr << __str._M_escape; 10238fd1498Szrj __ostr << *__c; 10338fd1498Szrj } 10438fd1498Szrj __ostr << __str._M_delim; 10538fd1498Szrj 10638fd1498Szrj return __os << __ostr.str(); 10738fd1498Szrj } 10838fd1498Szrj 10938fd1498Szrj /** 11038fd1498Szrj * @brief Inserter for quoted strings. 11138fd1498Szrj * 11238fd1498Szrj * _GLIBCXX_RESOLVE_LIB_DEFECTS 11338fd1498Szrj * DR 2344 quoted()'s interaction with padding is unclear 11438fd1498Szrj */ 11538fd1498Szrj template<typename _CharT, typename _Traits, typename _String> 11638fd1498Szrj std::basic_ostream<_CharT, _Traits>& 11738fd1498Szrj operator<<(std::basic_ostream<_CharT, _Traits>& __os, 11838fd1498Szrj const _Quoted_string<_String, _CharT>& __str) 11938fd1498Szrj { 12038fd1498Szrj std::basic_ostringstream<_CharT, _Traits> __ostr; 12138fd1498Szrj __ostr << __str._M_delim; 122*58e805e6Szrj for (auto __c : __str._M_string) 12338fd1498Szrj { 12438fd1498Szrj if (__c == __str._M_delim || __c == __str._M_escape) 12538fd1498Szrj __ostr << __str._M_escape; 12638fd1498Szrj __ostr << __c; 12738fd1498Szrj } 12838fd1498Szrj __ostr << __str._M_delim; 12938fd1498Szrj 13038fd1498Szrj return __os << __ostr.str(); 13138fd1498Szrj } 13238fd1498Szrj 13338fd1498Szrj /** 13438fd1498Szrj * @brief Extractor for delimited strings. 13538fd1498Szrj * The left and right delimiters can be different. 13638fd1498Szrj */ 13738fd1498Szrj template<typename _CharT, typename _Traits, typename _Alloc> 13838fd1498Szrj std::basic_istream<_CharT, _Traits>& 13938fd1498Szrj operator>>(std::basic_istream<_CharT, _Traits>& __is, 14038fd1498Szrj const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&, 14138fd1498Szrj _CharT>& __str) 14238fd1498Szrj { 14338fd1498Szrj _CharT __c; 14438fd1498Szrj __is >> __c; 14538fd1498Szrj if (!__is.good()) 14638fd1498Szrj return __is; 14738fd1498Szrj if (__c != __str._M_delim) 14838fd1498Szrj { 14938fd1498Szrj __is.unget(); 15038fd1498Szrj __is >> __str._M_string; 15138fd1498Szrj return __is; 15238fd1498Szrj } 15338fd1498Szrj __str._M_string.clear(); 15438fd1498Szrj std::ios_base::fmtflags __flags 15538fd1498Szrj = __is.flags(__is.flags() & ~std::ios_base::skipws); 15638fd1498Szrj do 15738fd1498Szrj { 15838fd1498Szrj __is >> __c; 15938fd1498Szrj if (!__is.good()) 16038fd1498Szrj break; 16138fd1498Szrj if (__c == __str._M_escape) 16238fd1498Szrj { 16338fd1498Szrj __is >> __c; 16438fd1498Szrj if (!__is.good()) 16538fd1498Szrj break; 16638fd1498Szrj } 16738fd1498Szrj else if (__c == __str._M_delim) 16838fd1498Szrj break; 16938fd1498Szrj __str._M_string += __c; 17038fd1498Szrj } 17138fd1498Szrj while (true); 17238fd1498Szrj __is.setf(__flags); 17338fd1498Szrj 17438fd1498Szrj return __is; 17538fd1498Szrj } 17638fd1498Szrj } // namespace __detail 17738fd1498Szrj 17838fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 17938fd1498Szrj } // namespace std 18038fd1498Szrj 18138fd1498Szrj #endif // C++11 18238fd1498Szrj #endif /* _GLIBCXX_QUOTED_STRING_H */ 183