forked from boostorg/io
Convert documentation to asciidoc
This commit is contained in:
23
doc/Jamfile
Normal file
23
doc/Jamfile
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Copyright 2019 Glen Joseph Fernandes
|
||||||
|
# (glenjofe@gmail.com)
|
||||||
|
#
|
||||||
|
# Distributed under the Boost Software License, Version 1.0.
|
||||||
|
# (http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
import asciidoctor ;
|
||||||
|
|
||||||
|
html span.html : span.adoc ;
|
||||||
|
|
||||||
|
install html_ : span.html : <location>html ;
|
||||||
|
|
||||||
|
pdf span.pdf : span.adoc ;
|
||||||
|
explicit span.pdf ;
|
||||||
|
|
||||||
|
install pdf_ : span.pdf : <location>pdf ;
|
||||||
|
explicit pdf_ ;
|
||||||
|
|
||||||
|
alias boostdoc ;
|
||||||
|
explicit boostdoc ;
|
||||||
|
|
||||||
|
alias boostrelease : html_ ;
|
||||||
|
explicit boostrelease ;
|
@ -1,67 +0,0 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Boost I/O Library</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body bgcolor="white" text="black">
|
|
||||||
<table border="1" bgcolor="teal" cellpadding="2">
|
|
||||||
<tr>
|
|
||||||
<td bgcolor="white"><img src="../../../boost.png"
|
|
||||||
alt="boost.png (6897 bytes)" width="277"
|
|
||||||
height="86"></td>
|
|
||||||
<td><a href="../../../index.htm"><font face="Arial"
|
|
||||||
color="white"><big>Home</big></font></a></td>
|
|
||||||
<td><a href="../../libraries.htm"><font face="Arial"
|
|
||||||
color="white"><big>Libraries</big></font></a></td>
|
|
||||||
<td><a href="http://www.boost.org/people/people.htm"><font face="Arial"
|
|
||||||
color="white"><big>People</big></font></a></td>
|
|
||||||
<td><a href="http://www.boost.org/more/faq.htm"><font face="Arial"
|
|
||||||
color="white"><big>FAQ</big></font></a></td>
|
|
||||||
<td><a href="../../../more/index.htm"><font face="Arial"
|
|
||||||
color="white"><big>More</big></font></a></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h1>Boost Input/Output Library</h1>
|
|
||||||
|
|
||||||
<table border="1" cellpadding="5" align="center">
|
|
||||||
<tr>
|
|
||||||
<th>Header / Docs</th>
|
|
||||||
<th>Contents</th>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center">
|
|
||||||
<code><a href="../../../boost/io_fwd.hpp"><boost/io_fwd.hpp></a></code>
|
|
||||||
</td>
|
|
||||||
<td valign="top">
|
|
||||||
Forward declaration header.
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center">
|
|
||||||
<code><a href="../../../boost/io/ios_state.hpp"><boost/io/ios_state.hpp></a></code><br>
|
|
||||||
<br><a href="ios_state.html">documentation</a>
|
|
||||||
</td>
|
|
||||||
<td valign="top">
|
|
||||||
State-saving classes for various IOStream attributes.
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h2>Rationale</h2>
|
|
||||||
|
|
||||||
<p>The I/O sub-library of Boost helps segregate the large number of
|
|
||||||
Boost headers. This sub-library should contain various items to use
|
|
||||||
with/for the standard I/O library.</p>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<p>Revised: 26 Feb 2002</p>
|
|
||||||
|
|
||||||
<p>Copyright 2002 Daryle Walker. Use, modification, and distribution are
|
|
||||||
subject to the Boost Software License, Version 1.0. (See accompanying file <a
|
|
||||||
href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at <<a
|
|
||||||
href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
33
doc/io.adoc
Normal file
33
doc/io.adoc
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
////
|
||||||
|
Copyright 2019 Glen Joseph Fernandes
|
||||||
|
(glenjofe@gmail.com)
|
||||||
|
|
||||||
|
Distributed under the Boost Software License, Version 1.0.
|
||||||
|
(http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
////
|
||||||
|
|
||||||
|
# Boost.IO
|
||||||
|
Daryle Walker, Beman Dawes
|
||||||
|
:idprefix:
|
||||||
|
:source-language: cpp
|
||||||
|
:toc: left
|
||||||
|
|
||||||
|
The I/O sub-library of Boost helps segregate the large number of Boost headers.
|
||||||
|
This sub-library should contain various items to use with/for the standard I/O
|
||||||
|
library.
|
||||||
|
|
||||||
|
:leveloffset: +1
|
||||||
|
|
||||||
|
include::ios_state.adoc[]
|
||||||
|
include::quoted.adoc[]
|
||||||
|
|
||||||
|
:leveloffset: -1
|
||||||
|
|
||||||
|
## Copyright and License
|
||||||
|
|
||||||
|
* Copyright 2002 Daryle Walker
|
||||||
|
* Copyright 2002, 2006, 2007, 2009, 2010 Beman Dawes
|
||||||
|
* Copyright 2019 Glen Joseph Fernandes
|
||||||
|
|
||||||
|
Distributed under the
|
||||||
|
http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0].
|
316
doc/ios_state.adoc
Normal file
316
doc/ios_state.adoc
Normal file
@ -0,0 +1,316 @@
|
|||||||
|
////
|
||||||
|
Copyright 2019 Glen Joseph Fernandes
|
||||||
|
(glenjofe@gmail.com)
|
||||||
|
|
||||||
|
Distributed under the Boost Software License, Version 1.0.
|
||||||
|
(http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
////
|
||||||
|
|
||||||
|
# ios_state, <boost/io/ios_state.hpp>
|
||||||
|
:toc:
|
||||||
|
:toc-title:
|
||||||
|
:idprefix:
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
The header `<boost/io/ios_state.hpp>` covers saving the stream state of objects
|
||||||
|
in the {cpp} IOStreams system.
|
||||||
|
|
||||||
|
## Rationale
|
||||||
|
|
||||||
|
Sometimes a certain value has to change only for a limited scope. Saver classes
|
||||||
|
save a copy of the current state of some object (or an aspect of an object),
|
||||||
|
and reset the object's state at destruction time, undoing any change the object
|
||||||
|
may have gone through.
|
||||||
|
|
||||||
|
The saver class strategy is helpful when using I/O stream objects. Manipulator
|
||||||
|
objects can change some aspect of a stream during input or output. The state
|
||||||
|
changed by the manipulator usually sticks to its new value after the I/O
|
||||||
|
transaction. This can be a problem if manipulators are used in a function that
|
||||||
|
is not supposed to externally change a stream's state.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <ostream>
|
||||||
|
#include <ios>
|
||||||
|
|
||||||
|
void hex_my_byte(std::ostream& os, char byte)
|
||||||
|
{
|
||||||
|
os << std::hex << static_cast<unsigned>(byte);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `os` stream will retain its new hexadecimal printing mode after the call to
|
||||||
|
`hex_my_byte`. The stream's printing mode can be saved and restored with manual
|
||||||
|
calls to the stream's state inspecting and mutating member functions. The
|
||||||
|
manual method becomes unwieldy if the main functionality is complex and/or
|
||||||
|
needs to be exception safe. A saver class can implement the better
|
||||||
|
"resource acquisition is initialization" strategy.
|
||||||
|
|
||||||
|
See the example below for better code, using saver classes.
|
||||||
|
|
||||||
|
## Header Synopsis
|
||||||
|
|
||||||
|
```
|
||||||
|
namespace boost {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
class ios_flags_saver;
|
||||||
|
class ios_precision_saver;
|
||||||
|
class ios_width_saver;
|
||||||
|
class ios_base_all_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_iostate_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_exception_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_tie_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_rdbuf_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_fill_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_locale_saver;
|
||||||
|
|
||||||
|
template<class Ch, class Tr = std::char_traits<Ch> >
|
||||||
|
class basic_ios_all_saver;
|
||||||
|
|
||||||
|
typedef basic_ios_iostate_saver<char> ios_iostate_saver;
|
||||||
|
typedef basic_ios_iostate_saver<wchar_t> wios_iostate_saver;
|
||||||
|
typedef basic_ios_exception_saver<char> ios_exception_saver;
|
||||||
|
typedef basic_ios_exception_saver<wchar_t> wios_exception_saver;
|
||||||
|
typedef basic_ios_tie_saver<char> ios_tie_saver;
|
||||||
|
typedef basic_ios_tie_saver<wchar_t> wios_tie_saver;
|
||||||
|
typedef basic_ios_rdbuf_saver<char> ios_rdbuf_saver;
|
||||||
|
typedef basic_ios_rdbuf_saver<wchar_t> wios_rdbuf_saver;
|
||||||
|
typedef basic_ios_fill_saver<char> ios_fill_saver;
|
||||||
|
typedef basic_ios_fill_saver<wchar_t> wios_fill_saver;
|
||||||
|
typedef basic_ios_locale_saver<char> ios_locale_saver;
|
||||||
|
typedef basic_ios_locale_saver<wchar_t> wios_locale_saver;
|
||||||
|
typedef basic_ios_all_saver<char> ios_all_saver;
|
||||||
|
typedef basic_ios_all_saver<wchar_t> wios_all_saver;
|
||||||
|
|
||||||
|
class ios_iword_saver;
|
||||||
|
class ios_pword_saver;
|
||||||
|
class ios_all_word_saver;
|
||||||
|
|
||||||
|
} // io
|
||||||
|
} // boost
|
||||||
|
```
|
||||||
|
|
||||||
|
## Savers for Basic Standard Attributes
|
||||||
|
|
||||||
|
The basic saver classes have this format:
|
||||||
|
|
||||||
|
[subs=+quotes]
|
||||||
|
```
|
||||||
|
class saver_class {
|
||||||
|
typedef std::ios_base state_type;
|
||||||
|
typedef `implementation_defined` aspect_type;
|
||||||
|
|
||||||
|
explicit saver_class(state_type& s);
|
||||||
|
saver_class(state_type& s, const aspect_type& new_value);
|
||||||
|
~saver_class();
|
||||||
|
|
||||||
|
void restore();
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
The `state_type` is the IOStreams base class `std::ios_base`. The user would
|
||||||
|
usually place an actual input, output, or combined stream object for the
|
||||||
|
state-type parameter, and not a base class object. The first constructor takes
|
||||||
|
a stream object and saves a reference to the stream and the current value of a
|
||||||
|
particular stream attribute. The second constructor works like the first, and
|
||||||
|
uses its second argument to change the stream's attribute to the new
|
||||||
|
`aspect_type` value given. The destructor restores the stream's attribute to
|
||||||
|
the saved value. The restoration can be activated early (and often) with the
|
||||||
|
`restore` member function.
|
||||||
|
|
||||||
|
.Basic IOStreams State Saver Classes
|
||||||
|
[%header,cols=5*]
|
||||||
|
|===
|
||||||
|
|Class |Saved Attribute |Attribute Type |Reading Method |Writing Method
|
||||||
|
|`ios_flags_saver`
|
||||||
|
|Format control flags
|
||||||
|
|`std::ios_base::fmtflags`
|
||||||
|
|`flags`
|
||||||
|
|`flags`
|
||||||
|
|`ios_precision_saver`
|
||||||
|
|Number of digits to print after decimal point
|
||||||
|
|`std::streamsize`
|
||||||
|
|`precision`
|
||||||
|
|`precision`
|
||||||
|
|`ios_width_saver`
|
||||||
|
|Minimum field width for printing objects
|
||||||
|
|`std::streamsize`
|
||||||
|
|`width`
|
||||||
|
|`width`
|
||||||
|
|===
|
||||||
|
|
||||||
|
## Savers for Advanced Standard Attributes
|
||||||
|
|
||||||
|
The saver class templates have this format:
|
||||||
|
|
||||||
|
[subs=+quotes]
|
||||||
|
```
|
||||||
|
template<class Ch, class Tr>
|
||||||
|
class saver_class {
|
||||||
|
typedef std::basic_ios<Ch, Tr> state_type;
|
||||||
|
typedef `implementation-defined` aspect_type;
|
||||||
|
|
||||||
|
explicit saver_class(state_type& s);
|
||||||
|
saver_class(state_type& s, const aspect_type& new_value);
|
||||||
|
~saver_class();
|
||||||
|
|
||||||
|
void restore();
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
The `state_type` is a version of the IOStreams base class template
|
||||||
|
`std::basic_ios<Ch, Tr>`, where `Ch` is a character type and `Tr` is a
|
||||||
|
character traits class. The user would usually place an actual input,
|
||||||
|
output, or combined stream object for the state-type parameter, and not a base
|
||||||
|
class object. The first constructor takes a stream object and saves a reference
|
||||||
|
to the stream and the current value of a particular stream attribute. The
|
||||||
|
second constructor works like the first, and uses its second argument to change
|
||||||
|
the stream's attribute to the new `aspect_type` value given. The destructor
|
||||||
|
restores the stream's attribute to the saved value. The restoration can be
|
||||||
|
activated early (and often) with the `restore` member function.
|
||||||
|
|
||||||
|
.Advanced IOStreams State Saver Class Templates
|
||||||
|
[%header,cols=5*]
|
||||||
|
|===
|
||||||
|
|Class |Saved Attribute |Attribute Type |Reading Method |Writing Method
|
||||||
|
|`basic_ios_iostate_saver<Ch, Tr>`
|
||||||
|
|Failure state of the stream [1], [2]
|
||||||
|
|`std::ios_base::iostate`
|
||||||
|
|`rdstate`
|
||||||
|
|`clear`
|
||||||
|
|`basic_ios_exception_saver<Ch, Tr>`
|
||||||
|
|Which failure states trigger an exception [1]
|
||||||
|
|`std::ios_base::iostate`
|
||||||
|
|`exceptions`
|
||||||
|
|`exceptions`
|
||||||
|
|`basic_ios_tie_saver<Ch, Tr>`
|
||||||
|
|Output stream synchronized with the stream
|
||||||
|
|`std::basic_ostream<Ch, Tr>*`
|
||||||
|
|`tie`
|
||||||
|
|`tie`
|
||||||
|
|`basic_ios_rdbuf_saver<Ch, Tr>`
|
||||||
|
|Stream buffer associated with the stream [2]
|
||||||
|
|`std::basic_streambuf<Ch, Tr>*`
|
||||||
|
|`rdbuf`
|
||||||
|
|`rdbuf`
|
||||||
|
|`basic_ios_fill_saver<Ch, Tr>`
|
||||||
|
|Character used to pad oversized field widths
|
||||||
|
|`Ch`
|
||||||
|
|`fill`
|
||||||
|
|`fill`
|
||||||
|
|`basic_ios_locale_saver<Ch, Tr>`
|
||||||
|
|Locale information associated with the stream [3]
|
||||||
|
|`std::locale`
|
||||||
|
|`getloc` (from `std::ios_base`)
|
||||||
|
|`imbue` (from `std::basic_ios<Ch, Tr>`)
|
||||||
|
|===
|
||||||
|
|
||||||
|
### Notes
|
||||||
|
|
||||||
|
1. When the failure state flags and/or the failure state exception watching
|
||||||
|
flags are changed, an exception is thrown if a match occurs among the two sets
|
||||||
|
of flags. This could mean that the constructor or destructor of these class
|
||||||
|
templates may throw.
|
||||||
|
2. When the associated stream buffer is changed, the stream's failure state set
|
||||||
|
is reset to "good" if the given stream buffer's address is non-NULL, but the
|
||||||
|
"bad" failure state is set if that address is NULL. This means that a saved
|
||||||
|
failure state of "good" may be restored as "bad" if the stream is stripped of
|
||||||
|
an associated stream buffer. Worse, given a NULL stream buffer address, an
|
||||||
|
exception is thrown if the "bad" failure state is being watched. This could
|
||||||
|
mean that the constructor or destructor of these class templates may throw.
|
||||||
|
3. The saver for the locale uses the `std::basic_ios<Ch, Tr>` class to extract
|
||||||
|
their information, although it could have used the functionality in
|
||||||
|
`std::ios_base`. The problem is that the versions of the needed member
|
||||||
|
functions in `ios_base` are not polymorphically related to the ones in
|
||||||
|
`basic_ios`. The stream classes that will be used with the saver classes should
|
||||||
|
use the versions of the member functions closest to them by inheritance, which
|
||||||
|
means the ones in `basic_ios`.
|
||||||
|
|
||||||
|
## Savers for User-Defined Attributes
|
||||||
|
|
||||||
|
There are three class (templates) for combined attribute savers. The
|
||||||
|
`ios_base_all_saver` saver class combines the functionality of all the basic
|
||||||
|
attribute saver classes. It has a constructor that takes the stream to have its
|
||||||
|
state preserved. The `basic_ios_all_saver` combines the functionality of all
|
||||||
|
the advanced attribute saver class templates and the combined basic attribute
|
||||||
|
saver class. It has a constructor that takes the stream to have its state
|
||||||
|
preserved. The `ios_all_word_saver` saver class combines the saver classes that
|
||||||
|
preserve user-defined formatting information. Its constructor takes the stream
|
||||||
|
to have its attributes saved and the index of the user-defined attributes. The
|
||||||
|
destructor for each class restores the saved state. Restoration can be
|
||||||
|
activated early (and often) for a class with the restore member function.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
The code used in the rationale can be improved at two places. The printing
|
||||||
|
function could use a saver around the code that changes the formatting state.
|
||||||
|
Or the calling function can surround the call with a saver. Or both can be
|
||||||
|
done, especially if the user does not know if the printing function uses a
|
||||||
|
state saver. If the user wants a series of changes back & forth, without
|
||||||
|
surrounding each change within a separate block, the restore member function
|
||||||
|
can be called between each trial.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include <boost/io/ios_state.hpp>
|
||||||
|
#include <ios>
|
||||||
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
void new_hex_my_byte(std::ostream& os, char byte)
|
||||||
|
{
|
||||||
|
boost::io::ios_flags_saver ifs(os);
|
||||||
|
os << std::hex << static_cast<unsigned>(byte);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
{
|
||||||
|
boost::io::ios_all_saver ias(std::cout);
|
||||||
|
new_hex_my_byte(std::cout, 'A');
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
{
|
||||||
|
boost::io::ios_all_saver ias(std::cerr);
|
||||||
|
new_hex_my_byte(std::cerr, 'b');
|
||||||
|
ias.restore();
|
||||||
|
new_hex_my_byte(std::cerr, 'C');
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
### Daryle Walker
|
||||||
|
|
||||||
|
Started the library. Contributed the initial versions of the format flags,
|
||||||
|
precision, width, and user-defined format flags saver classes. Contributed the
|
||||||
|
initial versions of the success state, success state exception flags, output
|
||||||
|
stream tie, stream buffer, character fill, and locale saver class templates.
|
||||||
|
Contributed the combined attribute classes and class template. Contributed the
|
||||||
|
test file `ios_state_test.cpp`.
|
||||||
|
|
||||||
|
## History
|
||||||
|
|
||||||
|
### 28 Feb 2005
|
||||||
|
|
||||||
|
Daryle Walker added the restore member functions, based on suggestions by
|
||||||
|
Gennadiy Rozental and Rob Stewart.
|
||||||
|
|
||||||
|
### 13 Mar 2002
|
||||||
|
|
||||||
|
Daryle Walker implemented the initial version.
|
@ -1,468 +0,0 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>I/O Stream-State Saver Library</title>
|
|
||||||
</head>
|
|
||||||
<body text="black" bgcolor="white" link="blue" vlink="purple" alink="red">
|
|
||||||
<h1><img src="../../../boost.png" alt="boost.png (6897 bytes)"
|
|
||||||
align="middle" width="277" height="86">Header <<cite><a
|
|
||||||
href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>
|
|
||||||
></h1>
|
|
||||||
|
|
||||||
<p>The header <cite><a
|
|
||||||
href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite>
|
|
||||||
covers saving the stream state of objects in the C++ IOStreams
|
|
||||||
system.</p>
|
|
||||||
|
|
||||||
<h2><a name="contents">Contents</a></h2>
|
|
||||||
|
|
||||||
<ol>
|
|
||||||
<li><a href="#contents">Contents</a></li>
|
|
||||||
<li><a href="#rationale">Rationale</a></li>
|
|
||||||
<li><a href="#header">Header Synopsis</a></li>
|
|
||||||
<li><a href="#base_savers">Savers for Basic Standard Attributes</a></li>
|
|
||||||
<li><a href="#adv_savers">Savers for Advanced Standard Attributes</a></li>
|
|
||||||
<li><a href="#user_savers">Savers for User-Defined Attributes</a></li>
|
|
||||||
<li><a href="#combo_savers">Savers for Combined Attributes</a></li>
|
|
||||||
<li><a href="#example">Example</a></li>
|
|
||||||
<li><a href="#refer">References</a></li>
|
|
||||||
<li><a href="#credits">Credits</a>
|
|
||||||
<ul>
|
|
||||||
<li><a href="#contributors">Contributors</a></li>
|
|
||||||
<li><a href="#history">History</a></li>
|
|
||||||
</ul></li>
|
|
||||||
</ol>
|
|
||||||
|
|
||||||
<h2><a name="rationale">Rationale</a></h2>
|
|
||||||
|
|
||||||
<p>Sometimes a certain value has to change only for a limited scope.
|
|
||||||
Saver classes save a copy of the current state of some object (or an
|
|
||||||
aspect of an object), and reset the object's state at destruction time,
|
|
||||||
undoing any change the object may have gone through.</p>
|
|
||||||
|
|
||||||
<p>The saver class strategy is helpful when using I/O stream objects.
|
|
||||||
Manipulator objects can change some aspect of a stream during input or
|
|
||||||
output. The state changed by the manipulator usually sticks to its new
|
|
||||||
value after the I/O transaction. This can be a problem if manipulators
|
|
||||||
are used in a function that is not supposed to externally change a
|
|
||||||
stream's state.</p>
|
|
||||||
|
|
||||||
<blockquote><pre>#include <ostream>
|
|
||||||
#include <ios>
|
|
||||||
|
|
||||||
void hex_my_byte( std::ostream &os, char byte )
|
|
||||||
{
|
|
||||||
os << std::hex << static_cast<unsigned>(byte);
|
|
||||||
}
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<p>The <var>os</var> stream will retain its new hexadecimal printing
|
|
||||||
mode after the call to <code>hex_my_byte</code>. The stream's printing
|
|
||||||
mode can be saved and restored with manual calls to the stream's state
|
|
||||||
inspecting and mutating member functions. The manual method becomes
|
|
||||||
unwieldy if the main functionality is complex and/or needs to be
|
|
||||||
exception safe. A saver class can implement the better "resource
|
|
||||||
acquisition is initialization" strategy.</p>
|
|
||||||
|
|
||||||
<p>See the <a href="#example">example</a> below for better code, using
|
|
||||||
saver classes.</p>
|
|
||||||
|
|
||||||
<h2><a name="header">Header Synopsis</a></h2>
|
|
||||||
|
|
||||||
<blockquote><pre>#include <iosfwd> <i>// for std::char_traits (declaration)</i>
|
|
||||||
|
|
||||||
namespace boost
|
|
||||||
{
|
|
||||||
namespace io
|
|
||||||
{
|
|
||||||
|
|
||||||
class ios_flags_saver;
|
|
||||||
class ios_precision_saver;
|
|
||||||
class ios_width_saver;
|
|
||||||
class ios_base_all_saver;
|
|
||||||
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_iostate_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_exception_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_tie_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_rdbuf_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_fill_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_locale_saver;
|
|
||||||
template < typename Ch, class Tr = ::std::char_traits<Ch> >
|
|
||||||
class basic_ios_all_saver;
|
|
||||||
|
|
||||||
typedef basic_ios_iostate_saver<char> ios_iostate_saver;
|
|
||||||
typedef basic_ios_iostate_saver<wchar_t> wios_iostate_saver;
|
|
||||||
typedef basic_ios_exception_saver<char> ios_exception_saver;
|
|
||||||
typedef basic_ios_exception_saver<wchar_t> wios_exception_saver;
|
|
||||||
typedef basic_ios_tie_saver<char> ios_tie_saver;
|
|
||||||
typedef basic_ios_tie_saver<wchar_t> wios_tie_saver;
|
|
||||||
typedef basic_ios_rdbuf_saver<char> ios_rdbuf_saver;
|
|
||||||
typedef basic_ios_rdbuf_saver<wchar_t> wios_rdbuf_saver;
|
|
||||||
typedef basic_ios_fill_saver<char> ios_fill_saver;
|
|
||||||
typedef basic_ios_fill_saver<wchar_t> wios_fill_saver;
|
|
||||||
typedef basic_ios_locale_saver<char> ios_locale_saver;
|
|
||||||
typedef basic_ios_locale_saver<wchar_t> wios_locale_saver;
|
|
||||||
typedef basic_ios_all_saver<char> ios_all_saver;
|
|
||||||
typedef basic_ios_all_saver<wchar_t> wios_all_saver;
|
|
||||||
|
|
||||||
class ios_iword_saver;
|
|
||||||
class ios_pword_saver;
|
|
||||||
class ios_all_word_saver;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<h2><a name="base_savers">Savers for Basic Standard Attributes</a></h2>
|
|
||||||
|
|
||||||
<p>The basic saver classes have this format:</p>
|
|
||||||
|
|
||||||
<blockquote><pre>class <var>saver_class</var>
|
|
||||||
{
|
|
||||||
typedef std::ios_base state_type;
|
|
||||||
typedef <i>implementation_defined</i> aspect_type;
|
|
||||||
|
|
||||||
explicit saver_class( state_type &s );
|
|
||||||
saver_class( state_type &s, <var>aspect_type</var> const &new_value );
|
|
||||||
~saver_class();
|
|
||||||
|
|
||||||
void restore();
|
|
||||||
};
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<p>The <var>state_type</var> is the IOStreams base class
|
|
||||||
<code>std::ios_base</code>. The user would usually place an actual
|
|
||||||
input, output, or combined stream object for the state-type parameter,
|
|
||||||
and not a base class object. The first constructor takes a stream
|
|
||||||
object and saves a reference to the stream and the current value of a
|
|
||||||
particular stream attribute. The second constructor works like the
|
|
||||||
first, and uses its second argument to change the stream's attribute to
|
|
||||||
the new <var>aspect_type</var> value given. The destructor restores the
|
|
||||||
stream's attribute to the saved value. The restoration can be activated
|
|
||||||
early (and often) with the <code>restore</code> member function.</p>
|
|
||||||
|
|
||||||
<table border="1" align="center">
|
|
||||||
<caption>Basic IOStreams State Saver Classes</caption>
|
|
||||||
<tr>
|
|
||||||
<th>Class</th>
|
|
||||||
<th>Saved Attribute</th>
|
|
||||||
<th>Attribute Type</th>
|
|
||||||
<th>Reading Method</th>
|
|
||||||
<th>Writing Method</th>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::ios_flags_saver</code></td>
|
|
||||||
<td>Format control flags</td>
|
|
||||||
<td><code>std::ios_base::fmtflags</code></td>
|
|
||||||
<td><code>flags</code></td>
|
|
||||||
<td><code>flags</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::ios_precision_saver</code></td>
|
|
||||||
<td>Number of digits to print after decimal point</td>
|
|
||||||
<td><code>std::streamsize</code></td>
|
|
||||||
<td><code>precision</code></td>
|
|
||||||
<td><code>precision</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::ios_width_saver</code></td>
|
|
||||||
<td>Minimum field width for printing objects</td>
|
|
||||||
<td><code>std::streamsize</code></td>
|
|
||||||
<td><code>width</code></td>
|
|
||||||
<td><code>width</code></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h2><a name="adv_savers">Savers for Advanced Standard Attributes</a></h2>
|
|
||||||
|
|
||||||
<p>The saver class templates have this format:</p>
|
|
||||||
|
|
||||||
<blockquote><pre>template < typename Ch, class Tr >
|
|
||||||
class <var>saver_class</var>
|
|
||||||
{
|
|
||||||
typedef std::basic_ios<Ch, Tr> state_type;
|
|
||||||
typedef <i>implementation_defined</i> aspect_type;
|
|
||||||
|
|
||||||
explicit saver_class( state_type &s );
|
|
||||||
saver_class( state_type &s, <var>aspect_type</var> const &new_value );
|
|
||||||
~saver_class();
|
|
||||||
|
|
||||||
void restore();
|
|
||||||
};
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<p>The <var>state_type</var> is a version of the IOStreams base class
|
|
||||||
template <code>std::basic_ios<Ch, Tr></code>, where
|
|
||||||
<code>Ch</code> is a character type and <code>Tr</code> is a character
|
|
||||||
traits class. The user would usually place an actual input, output, or
|
|
||||||
combined stream object for the state-type parameter, and not a base
|
|
||||||
class object. The first constructor takes a stream object and saves a
|
|
||||||
reference to the stream and the current value of a particular stream
|
|
||||||
attribute. The second constructor works like the first, and uses its
|
|
||||||
second argument to change the stream's attribute to the new
|
|
||||||
<var>aspect_type</var> value given. The destructor restores the stream's
|
|
||||||
attribute to the saved value. The restoration can be activated
|
|
||||||
early (and often) with the <code>restore</code> member function.</p>
|
|
||||||
|
|
||||||
<table border="1" align="center">
|
|
||||||
<caption>Advanced IOStreams State Saver Class Templates</caption>
|
|
||||||
<tr>
|
|
||||||
<th>Class Template</th>
|
|
||||||
<th>Saved Attribute</th>
|
|
||||||
<th>Attribute Type</th>
|
|
||||||
<th>Reading Method</th>
|
|
||||||
<th>Writing Method</th>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_iostate_saver<Ch, Tr></code></td>
|
|
||||||
<td>Failure state of the stream <a href="#Note1">[1]</a>, <a href="#Note2">[2]</a></td>
|
|
||||||
<td><code>std::ios_base::iostate</code></td>
|
|
||||||
<td><code>rdstate</code></td>
|
|
||||||
<td><code>clear</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_exception_saver<Ch, Tr></code></td>
|
|
||||||
<td>Which failure states trigger an exception <a href="#Note1">[1]</a></td>
|
|
||||||
<td><code>std::ios_base::iostate</code></td>
|
|
||||||
<td><code>exceptions</code></td>
|
|
||||||
<td><code>exceptions</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_tie_saver<Ch, Tr></code></td>
|
|
||||||
<td>Output stream synchronized with the stream</td>
|
|
||||||
<td><code>std::basic_ostream<Ch, Tr> *</code></td>
|
|
||||||
<td><code>tie</code></td>
|
|
||||||
<td><code>tie</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_rdbuf_saver<Ch, Tr></code></td>
|
|
||||||
<td>Stream buffer associated with the stream <a href="#Note2">[2]</a></td>
|
|
||||||
<td><code>std::basic_streambuf<Ch, Tr> *</code></td>
|
|
||||||
<td><code>rdbuf</code></td>
|
|
||||||
<td><code>rdbuf</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_fill_saver<Ch, Tr></code></td>
|
|
||||||
<td>Character used to pad oversized field widths</td>
|
|
||||||
<td><code>Ch</code></td>
|
|
||||||
<td><code>fill</code></td>
|
|
||||||
<td><code>fill</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::basic_ios_locale_saver<Ch, Tr></code></td>
|
|
||||||
<td>Locale information associated with the stream <a href="#Note3">[3]</a></td>
|
|
||||||
<td><code>std::locale</code></td>
|
|
||||||
<td><code>getloc</code> (from <code>std::ios_base</code>)</td>
|
|
||||||
<td><code>imbue</code> (from <code>std::basic_ios<Ch, Tr></code>)</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h3>Notes</h3>
|
|
||||||
|
|
||||||
<ol>
|
|
||||||
<li>When the failure state flags and/or the failure state exception
|
|
||||||
watching flags are changed, an exception is thrown if a match
|
|
||||||
occurs among the two sets of flags. This could mean that
|
|
||||||
the <a name="Note1">constructor or destructor of these class
|
|
||||||
templates may throw</a>.</li>
|
|
||||||
<li>When the associated stream buffer is changed, the stream's
|
|
||||||
failure state set is reset to "good" if the given stream
|
|
||||||
buffer's address is non-NULL, but the "bad" failure
|
|
||||||
state is set if that address is NULL. This means that a saved
|
|
||||||
failure state of "good" may be restored as "bad"
|
|
||||||
if the stream is stripped of an associated stream buffer. Worse,
|
|
||||||
given a NULL stream buffer address, an exception is thrown if the
|
|
||||||
"bad" failure state is being watched. This could mean
|
|
||||||
that the <a name="Note2">constructor or destructor of these class
|
|
||||||
templates may throw</a>.</li>
|
|
||||||
<li>The <a name="Note3">saver for the locale uses the
|
|
||||||
<code>std::basic_ios<Ch, Tr></code> class to extract their
|
|
||||||
information</a>, although it could have used the functionality
|
|
||||||
in <code>std::ios_base</code>. The problem is that the versions
|
|
||||||
of the needed member functions in <code>ios_base</code> are not
|
|
||||||
polymorphically related to the ones in <code>basic_ios</code>.
|
|
||||||
The stream classes that will be used with the saver classes
|
|
||||||
should use the versions of the member functions closest to them
|
|
||||||
by inheritance, which means the ones in
|
|
||||||
<code>basic_ios</code>.</li>
|
|
||||||
</ol>
|
|
||||||
|
|
||||||
<h2><a name="user_savers">Savers for User-Defined Attributes</a></h2>
|
|
||||||
|
|
||||||
<p>The saver classes for user-defined formatting information have this
|
|
||||||
format:</p>
|
|
||||||
|
|
||||||
<blockquote><pre>#include <iosfwd> <i>// for std::ios_base (declaration)</i>
|
|
||||||
|
|
||||||
class <var>saver_class</var>
|
|
||||||
{
|
|
||||||
typedef std::ios_base state_type;
|
|
||||||
typedef int index_type;
|
|
||||||
typedef <i>implementation_defined</i> aspect_type;
|
|
||||||
|
|
||||||
explicit saver_class( state_type &s, index_type i );
|
|
||||||
saver_class( state_type &s, index_type i, <var>aspect_type</var> const &new_value );
|
|
||||||
~saver_class();
|
|
||||||
|
|
||||||
void restore();
|
|
||||||
};
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<p>The index <var>i</var> differentiates between specific user-defined
|
|
||||||
formatting attributes. The index can only be determined at run-time
|
|
||||||
(most likely with the class-static <code>std::ios_base::xalloc</code>
|
|
||||||
member function).</p>
|
|
||||||
|
|
||||||
<p>The <var>state_type</var> is the base class of the IOStreams system,
|
|
||||||
<code>std::ios_base</code>. The user would usually place an actual
|
|
||||||
input, output, or combined stream object for the state-type parameter,
|
|
||||||
and not a base class object. The first constructor takes a stream
|
|
||||||
object and index and saves a reference to the stream and the current
|
|
||||||
value of a particular stream attribute. The second constructor works
|
|
||||||
like the first, and uses its third argument to change the stream's
|
|
||||||
attribute to the new <var>aspect_type</var> value given. The destructor
|
|
||||||
restores the stream's attribute to the saved value. The restoration can
|
|
||||||
be activated early (and often) with the <code>restore</code> member
|
|
||||||
function.</p>
|
|
||||||
|
|
||||||
<table border="1" align="center">
|
|
||||||
<caption>IOStream User-Defined State Saver Classes</caption>
|
|
||||||
<tr>
|
|
||||||
<th>Class</th>
|
|
||||||
<th>Saved Attribute</th>
|
|
||||||
<th>Attribute Type</th>
|
|
||||||
<th>Reference Method</th>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::ios_iword_saver</code></td>
|
|
||||||
<td>Numeric user-defined format flag</td>
|
|
||||||
<td><code>long</code></td>
|
|
||||||
<td><code>iword</code></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><code>boost::io::ios_pword_saver</code></td>
|
|
||||||
<td>Pointer user-defined format flag</td>
|
|
||||||
<td><code>void *</code></td>
|
|
||||||
<td><code>pword</code></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h2><a name="combo_savers">Savers for Combined Attributes</a></h2>
|
|
||||||
|
|
||||||
<p>There are three class (templates) for combined attribute savers. The
|
|
||||||
<code>boost:io::ios_base_all_saver</code> saver class combines the
|
|
||||||
functionality of all the basic attribute saver classes. It has a
|
|
||||||
constructor that takes the stream to have its state preserved. The
|
|
||||||
<code>boost::io::basic_ios_all_saver</code> combines the functionality
|
|
||||||
of all the advanced attribute saver class templates and the combined
|
|
||||||
basic attribute saver class. It has a constructor that takes the stream
|
|
||||||
to have its state preserved. The
|
|
||||||
<code>boost::io::ios_all_word_saver</code> saver class combines the
|
|
||||||
saver classes that preserve user-defined formatting information. Its
|
|
||||||
constructor takes the stream to have its attributes saved and the index
|
|
||||||
of the user-defined attributes. The destructor for each class restores
|
|
||||||
the saved state. Restoration can be activated early (and often) for a
|
|
||||||
class with the <code>restore</code> member function.</p>
|
|
||||||
|
|
||||||
<h2><a name="example">Example</a></h2>
|
|
||||||
|
|
||||||
<p>The code used in the <a href="#rationale">rationale</a> can be
|
|
||||||
improved at two places. The printing function could use a saver around
|
|
||||||
the code that changes the formatting state. Or the calling function can
|
|
||||||
surround the call with a saver. Or both can be done, especially if the
|
|
||||||
user does not know if the printing function uses a state saver. If the
|
|
||||||
user wants a series of changes back & forth, without surrounding each
|
|
||||||
change within a separate block, the <code>restore</code> member function
|
|
||||||
can be called between each trial.</p>
|
|
||||||
|
|
||||||
<blockquote><pre>#include <boost/io/ios_state.hpp>
|
|
||||||
#include <ios>
|
|
||||||
#include <iostream>
|
|
||||||
#include <ostream>
|
|
||||||
|
|
||||||
void new_hex_my_byte( std::ostream &os, char byte )
|
|
||||||
{
|
|
||||||
boost::io::ios_flags_saver ifs( os );
|
|
||||||
|
|
||||||
os << std::hex << static_cast<unsigned>(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
using std::cout;
|
|
||||||
using std::cerr;
|
|
||||||
|
|
||||||
//...
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::io::ios_all_saver ias( cout );
|
|
||||||
|
|
||||||
new_hex_my_byte( cout, 'A' );
|
|
||||||
}
|
|
||||||
|
|
||||||
//...
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::io::ios_all_saver ias( cerr );
|
|
||||||
|
|
||||||
new_hex_my_byte( cerr, 'b' );
|
|
||||||
ias.restore();
|
|
||||||
new_hex_my_byte( cerr, 'C' );
|
|
||||||
}
|
|
||||||
|
|
||||||
//...
|
|
||||||
}
|
|
||||||
</pre></blockquote>
|
|
||||||
|
|
||||||
<h2><a name="refer">References</a></h2>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>The I/O state saver library header itself: <cite><a
|
|
||||||
href="../../../boost/io/ios_state.hpp">boost/io/ios_state.hpp</a></cite></li>
|
|
||||||
<li>Some test/example code: <cite><a
|
|
||||||
href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<h2><a name="credits">Credits</a></h2>
|
|
||||||
|
|
||||||
<h3><a name="contributors">Contributors</a></h3>
|
|
||||||
|
|
||||||
<dl>
|
|
||||||
<dt><a href="http://www.boost.org/people/daryle_walker.html">Daryle Walker</a>
|
|
||||||
<dd>Started the library. Contributed the initial versions of the
|
|
||||||
format flags, precision, width, and user-defined format flags
|
|
||||||
saver classes. Contributed the initial versions of the success
|
|
||||||
state, success state exception flags, output stream tie, stream
|
|
||||||
buffer, character fill, and locale saver class templates.
|
|
||||||
Contributed the combined attribute classes and class template.
|
|
||||||
Contributed the test file <cite><a
|
|
||||||
href="../test/ios_state_test.cpp">ios_state_test.cpp</a></cite>.
|
|
||||||
</dl>
|
|
||||||
|
|
||||||
<h3><a name="history">History</a></h3>
|
|
||||||
|
|
||||||
<dl>
|
|
||||||
<dt>28 Feb 2005, Daryle Walker
|
|
||||||
<dd>Added the <code>restore</code> member functions, based on suggestions
|
|
||||||
by Gennadiy Rozental and Rob Stewart
|
|
||||||
|
|
||||||
<dt>13 Mar 2002, Daryle Walker
|
|
||||||
<dd>Initial version
|
|
||||||
</dl>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<p>Revised: 28 February 2005</p>
|
|
||||||
|
|
||||||
<p>Copyright 2002, 2005 Daryle Walker. Use, modification, and distribution
|
|
||||||
are subject to the Boost Software License, Version 1.0. (See accompanying
|
|
||||||
file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or a copy at
|
|
||||||
<<a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>>.)</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
131
doc/quoted.adoc
Normal file
131
doc/quoted.adoc
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
////
|
||||||
|
Copyright 2019 Glen Joseph Fernandes
|
||||||
|
(glenjofe@gmail.com)
|
||||||
|
|
||||||
|
Distributed under the Boost Software License, Version 1.0.
|
||||||
|
(http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
////
|
||||||
|
|
||||||
|
# quoted, <boost/io/quoted.hpp>
|
||||||
|
:toc:
|
||||||
|
:toc-title:
|
||||||
|
:idprefix:
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
C++ Standard library stream I/O for strings that contain embedded spaces can
|
||||||
|
produce unexpected results. For example,
|
||||||
|
|
||||||
|
```
|
||||||
|
std::stringstream ss;
|
||||||
|
std::string original = "fooled you";
|
||||||
|
std::string roundtrip;
|
||||||
|
|
||||||
|
ss << original;
|
||||||
|
ss >> roundtrip;
|
||||||
|
|
||||||
|
std::cout << original; // outputs: fooled you
|
||||||
|
std::cout << roundtrip; // outputs: fooled
|
||||||
|
|
||||||
|
assert(original == roundtrip); // assert will fire
|
||||||
|
```
|
||||||
|
|
||||||
|
The Boost quoted stream I/O manipulator places delimiters, defaulted to the
|
||||||
|
double-quote ("), around strings on output, and strips off the delimiters on
|
||||||
|
input. This ensures strings with embedded spaces round-trip as desired. For
|
||||||
|
example,
|
||||||
|
|
||||||
|
```
|
||||||
|
std::stringstream ss;
|
||||||
|
std::string original = "fooled you";
|
||||||
|
std::string roundtrip;
|
||||||
|
|
||||||
|
ss << quoted(original);
|
||||||
|
ss >> quoted(roundtrip);
|
||||||
|
|
||||||
|
std::cout << quoted(original); // outputs: "fooled you"
|
||||||
|
std::cout << roundtrip; // outputs: fooled you
|
||||||
|
|
||||||
|
assert(original == roundtrip); // assert will not fire
|
||||||
|
```
|
||||||
|
|
||||||
|
If the string contains the delimiter character, on output that character will
|
||||||
|
be preceded by an escape character, as will the escape character itself:
|
||||||
|
|
||||||
|
```
|
||||||
|
std::cout << quoted("'Jack & Jill'", '&', '\''); // outputs: '&'Jack && Jill&''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Header synopsis
|
||||||
|
|
||||||
|
[subs=+quotes]
|
||||||
|
```
|
||||||
|
namespace boost {
|
||||||
|
namespace io {
|
||||||
|
|
||||||
|
template<class Char, class Traits, class Alloc>
|
||||||
|
`unspecified-type1`
|
||||||
|
quoted(const std::basic_string<Char, Traits, Alloc>& string,
|
||||||
|
Char escape='\\', Char delim='\"');
|
||||||
|
|
||||||
|
template<class Char>
|
||||||
|
`unspecified-type2`
|
||||||
|
quoted(const Char* string, Char escape='\\', Char delim='\"');
|
||||||
|
|
||||||
|
template<class Char, class Traits, class Alloc>
|
||||||
|
`unspecified-type3`
|
||||||
|
quoted(std::basic_string<Char, Traits, Alloc>& string,
|
||||||
|
Char escape='\\', Char delim='\"');
|
||||||
|
|
||||||
|
} // io
|
||||||
|
} // boost
|
||||||
|
```
|
||||||
|
|
||||||
|
*unspecified-type1*, *unspecified-type2*, and *unspecified-type3* are
|
||||||
|
implementation supplied types with implementation supplied `operator<<`:
|
||||||
|
|
||||||
|
[subs=+quotes]
|
||||||
|
```
|
||||||
|
template<class Char, class Traits>
|
||||||
|
std::basic_ostream<Char, Traits>&
|
||||||
|
operator<<(std::basic_ostream<Char, Traits>& os,
|
||||||
|
const `unspecified-typeN`& proxy);
|
||||||
|
```
|
||||||
|
|
||||||
|
Effects:: Inserts characters into `os`:
|
||||||
|
* `delim`
|
||||||
|
* Each character in `string`. If the character to be output is equal to
|
||||||
|
`escape` or `delim`, as determined by `operator==`, first output `escape`.
|
||||||
|
* `delim`
|
||||||
|
Remarks:: `string`, `escape`, and `delim` have the type and value of the
|
||||||
|
corresponding arguments of the call to the `quoted` function that constructed
|
||||||
|
`proxy`.
|
||||||
|
Returns:: `os`.
|
||||||
|
|
||||||
|
*unspecified-type3* is an implementation supplied type with an implementation
|
||||||
|
supplied `operator>>`:
|
||||||
|
|
||||||
|
[subs=+quotes]
|
||||||
|
```
|
||||||
|
template<class Char, class Traits>
|
||||||
|
std::basic_istream<Char, Traits>&
|
||||||
|
operator>>(std::basic_istream<Char, Traits>& is,
|
||||||
|
const `unspecified-type3`& proxy);
|
||||||
|
```
|
||||||
|
|
||||||
|
Effects:: Extracts characters from `os`:
|
||||||
|
* If the first character extracted is equal to `delim`, as determined by
|
||||||
|
`operator==`, then:
|
||||||
|
** Turn off the `skipws` flag.
|
||||||
|
** `string.clear()`
|
||||||
|
** Until an unescaped `delim` character is reached or `is.not_good()`, extract
|
||||||
|
characters from `os` and append them to string, except that if an escape is
|
||||||
|
reached, ignore it and append the next character to string.
|
||||||
|
** Discard the final `delim` character.
|
||||||
|
** Restore the `skipws` flag to its original value.
|
||||||
|
* Otherwise, `os >> string`.
|
||||||
|
|
||||||
|
Remarks:: `string`, `escape`, and `delim` have the type and value of the
|
||||||
|
corresponding arguments of the call to the `quoted` function that constructed
|
||||||
|
`proxy`.
|
||||||
|
Returns:: `is`.
|
@ -1,164 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
||||||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
|
|
||||||
<title>Boost "quoted" I/O manipulator</title>
|
|
||||||
<meta name="generator" content="Microsoft FrontPage 5.0" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="../../../doc/src/minimal.css" />
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<table border="0" cellpadding="5" cellspacing="0"
|
|
||||||
style="border-collapse: collapse">
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td width="277"><a href="../../../index.htm"><img
|
|
||||||
src="../../../boost.png" alt="boost.png (6897 bytes)" align="middle"
|
|
||||||
width="300" height="86" border="0" /></a></td>
|
|
||||||
<td>
|
|
||||||
<h1 align="center">"Quoted"
|
|
||||||
I/O Manipulators<br>
|
|
||||||
for Strings</h1>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<table border="1" cellpadding="5" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111">
|
|
||||||
<tr>
|
|
||||||
<td>
|
|
||||||
<p align="center"><b>"Quoted"
|
|
||||||
I/O Manipulators
|
|
||||||
for Strings are not yet accepted into Boost as public components. Thus the
|
|
||||||
header file is currently located in <boost/io/detail/quoted_manip.hpp></b></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h2>Introduction</h2>
|
|
||||||
<p>C++ Standard library stream I/O for strings that contain embedded spaces
|
|
||||||
can produce unexpected results. For example,</p>
|
|
||||||
<blockquote>
|
|
||||||
<pre>std::stringstream ss;
|
|
||||||
std::string original = "fooled you";
|
|
||||||
std::string round_trip;
|
|
||||||
|
|
||||||
ss << original;
|
|
||||||
ss >> round_trip;
|
|
||||||
|
|
||||||
std::cout << original; // outputs: fooled you
|
|
||||||
std::cout << round_trip; // outputs: fooled
|
|
||||||
|
|
||||||
assert(original == round_trip); // assert will fire</pre>
|
|
||||||
</blockquote>
|
|
||||||
<p>The Boost <code>quoted</code> stream I/O manipulator places delimiters, defaulted
|
|
||||||
to the double-quote (<code>"</code>), around strings on output, and strips off
|
|
||||||
the delimiters on input. This ensures strings with embedded spaces round-trip as
|
|
||||||
desired. For example,</p>
|
|
||||||
<blockquote>
|
|
||||||
<pre>std::stringstream ss;
|
|
||||||
std::string original = "fooled you";
|
|
||||||
std::string round_trip;
|
|
||||||
|
|
||||||
ss << quoted(original);
|
|
||||||
ss >> quoted(round_trip);
|
|
||||||
|
|
||||||
std::cout << quoted(original); // outputs: "fooled you"
|
|
||||||
std::cout << round_trip; // outputs: fooled you
|
|
||||||
|
|
||||||
assert(original == round_trip); // assert will not fire</pre>
|
|
||||||
</blockquote>
|
|
||||||
<p>If the string contains the delimiter character, on output that character will
|
|
||||||
be preceded by an escape character, as will the escape character itself:</p>
|
|
||||||
<blockquote>
|
|
||||||
<pre>std::cout << quoted("'Jack & Jill'", '&', '\''); // outputs: '&'Jack && Jill&''</pre>
|
|
||||||
</blockquote>
|
|
||||||
<h2>Header <a href="../../../boost/io/detail/quoted_manip.hpp"><boost/io/quoted_manip.hpp></a> synopsis</h2>
|
|
||||||
<pre>namespace boost
|
|
||||||
{
|
|
||||||
namespace io
|
|
||||||
{
|
|
||||||
// manipulator for const std::basic_string&
|
|
||||||
|
|
||||||
template <class Char, class Traits, class Alloc>
|
|
||||||
<b><i>unspecified-type1</i></b> quoted(const std::basic_string<Char, Traits, Alloc>& string,
|
|
||||||
Char escape='\\', Char delim='\"');
|
|
||||||
|
|
||||||
// manipulator for const C-string*
|
|
||||||
|
|
||||||
template <class Char>
|
|
||||||
<b><i>unspecified-type2</i></b> quoted(const Char* string,
|
|
||||||
Char escape='\\', Char delim='\"');
|
|
||||||
|
|
||||||
// manipulator for non-const std::basic_string&
|
|
||||||
|
|
||||||
template <class Char, class Traits, class Alloc>
|
|
||||||
<b><i>unspecified-type3</i></b> quoted(std::basic_string<Char, Traits, Alloc>& string,
|
|
||||||
Char escape='\\', Char delim='\"');
|
|
||||||
}
|
|
||||||
}</pre>
|
|
||||||
<p><i><b><code>unspecified_type1</code></b></i>, <i><b><code>unspecified_type2</code></b></i>,
|
|
||||||
and <i><b><code>unspecified_type3</code></b></i> are implementation supplied
|
|
||||||
types with implementation supplied <code>operator<<</code>:</p>
|
|
||||||
<blockquote>
|
|
||||||
<pre>template <class Char, class Traits>
|
|
||||||
std::basic_ostream<Char, Traits>&
|
|
||||||
operator<<(std::basic_ostream<Char, Traits>& os, const <i><b><code>unspecified_typeN</code></b></i>& proxy);</pre>
|
|
||||||
<p><i>Effects:</i> Inserts characters into <code>os</code>:</p>
|
|
||||||
<ul>
|
|
||||||
<li><code>delim</code>.</li>
|
|
||||||
<li>Each character in <code>string</code>. If the character to be output is
|
|
||||||
equal to <code>escape</code> or <code>delim</code>, as determined by <code>
|
|
||||||
operator==</code>, first output <code>escape</code>. </li>
|
|
||||||
<li><code>delim</code>.</li>
|
|
||||||
</ul>
|
|
||||||
<p><i>Remarks:</i> <code>string</code>, <code>escape</code>, and <code>delim</code>
|
|
||||||
have the type and value of the corresponding arguments of the call to the <code>
|
|
||||||
quoted</code> function that constructed <code>proxy</code>.</p>
|
|
||||||
<p><i>Returns:</i> <code>os</code>. </p>
|
|
||||||
</blockquote>
|
|
||||||
<p><i><b><code>unspecified_type3</code></b></i> is an implementation supplied
|
|
||||||
type with an implementation supplied <code>operator>></code>:</p>
|
|
||||||
<blockquote>
|
|
||||||
<pre>template <class Char, class Traits>
|
|
||||||
std::basic_istream<Char, Traits>&
|
|
||||||
operator>>(std::basic_istream<Char, Traits>& is, const <i><b><code>unspecified_type3</code></b></i>& proxy);</pre>
|
|
||||||
<p><i>Effects:</i> Extracts characters from <code>os</code>:</p>
|
|
||||||
<ul>
|
|
||||||
<li>If the first character extracted is equal to delim, as determined by
|
|
||||||
<code>operator==</code>, then:<ul>
|
|
||||||
<li>Turn off the <code>skipws</code> flag.</li>
|
|
||||||
<li><code>string.clear()</code></li>
|
|
||||||
<li>Until an unescaped <code>delim</code> character is reached or <code>
|
|
||||||
is.not_good()</code>, extract
|
|
||||||
characters from <code>os</code> and append them to <code>string</code>,
|
|
||||||
except that if an <code>escape</code> is reached, ignore it and append the
|
|
||||||
next character to <code>string</code>.</li>
|
|
||||||
<li>Discard the final <code>delim</code> character.</li>
|
|
||||||
<li>Restore the <code>skipws</code> flag to its original value.</li>
|
|
||||||
</ul>
|
|
||||||
</li>
|
|
||||||
<li>Otherwise, <code>os >> string</code>.</li>
|
|
||||||
</ul>
|
|
||||||
<p><i>Remarks:</i> <code>string</code>, <code>escape</code>, and <code>delim</code>
|
|
||||||
have the type and value of the corresponding arguments of the call to the <code>
|
|
||||||
quoted</code> function that constructed <code>proxy</code>.</p>
|
|
||||||
<p><i>Returns:</i> <code>is</code>. </p>
|
|
||||||
</blockquote>
|
|
||||||
<h2>Acknowledgements</h2>
|
|
||||||
<p>The <code>quoted()</code> stream manipulator emerged from discussions on the
|
|
||||||
Boost developers mailing list. Participants included Beman Dawes, Rob Stewart,
|
|
||||||
Alexander Lamaison, Eric Niebler, Vicente Botet, Andrey Semashev, Phil Richards,
|
|
||||||
and Rob Murray. Eric Niebler's suggestions provided the basis for the name and
|
|
||||||
form of the templates. </p>
|
|
||||||
<hr>
|
|
||||||
<p>Copyright Beman Dawes, 2002, 2006, 2007, 2009, 2010</p>
|
|
||||||
<p>Distributed under the Boost Software License, Version 1.0. See
|
|
||||||
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
|
|
||||||
<p>Revised
|
|
||||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->08 March 2013<!--webbot bot="Timestamp" endspan i-checksum="27284" --></p>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
19
index.html
19
index.html
@ -1,14 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
Copyright Beman Dawes, 2001
|
||||||
|
|
||||||
|
Distributed under the Boost Software License, Version 1.0.
|
||||||
|
(http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
-->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="refresh" content="0; URL=doc/index.html">
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="refresh" content="0; url=doc/html/io.html">
|
||||||
|
<title>Span</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Automatic redirection failed, please go to
|
Redirecting you to <a href="doc/html/io.html">IO</a>.
|
||||||
<a href="doc/index.html">doc/index.html</a>. <hr>
|
|
||||||
<p>Copyright Beman Dawes, 2001</p>
|
|
||||||
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
||||||
file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
|
|
||||||
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)
|
|
||||||
</p>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
"key": "io",
|
"key": "io",
|
||||||
"name": "IO State Savers",
|
"name": "IO State Savers",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Daryle Walker"
|
"Daryle Walker",
|
||||||
|
"Beman Dawes"
|
||||||
],
|
],
|
||||||
"description": "The I/O sub-library of Boost helps segregate the large number of Boost headers. This sub-library should contain various items to use with/for the standard I/O library.",
|
"description": "Utilities for using the standard I/O library.",
|
||||||
"documentation": "doc/ios_state.html",
|
|
||||||
"category": [
|
"category": [
|
||||||
"IO"
|
"IO"
|
||||||
],
|
],
|
||||||
|
Reference in New Issue
Block a user