Skip to content

Commit 34bf82c

Browse files
author
Jim Porter
committed
Add some simple usage docs
1 parent 144c8d9 commit 34bf82c

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,80 @@ $ ninja install
2626
However, since bencode.hpp is a single-file, header-only library, you can just
2727
copy `include/bencode.hpp` to your destination of choice.
2828

29+
## Usage
30+
31+
### Data types
32+
33+
Bencode has four data types: `integer`s, `string`s, `list`s, and `dict`s. These
34+
correspond to `std::int64_t`, `std::string`, `std::vector<bencode::data>`, and
35+
`std::map<std::string, bencode::data>`, respectively. Since the data types are
36+
determined at runtime, these are all stored in a variant type called `data`.
37+
38+
### Decoding
39+
40+
Decoding bencoded data is simple. Just call `decode`, which will return a `data`
41+
object that you can operate on:
42+
43+
```c++
44+
auto data = bencode::decode("i666e");
45+
auto value = boost::get<bencode::integer>(data);
46+
```
47+
48+
`decode` also has an overload that takes an iterator pair:
49+
50+
```c++
51+
auto data = bencode::decode(foo.begin(), foo.end());
52+
```
53+
54+
Finally, you pass an `std::istream` directly to `decode`. By default, this
55+
overload will set the eof bit on the stream if it reaches the end. However, you
56+
can override this behavior. This is useful if, for instance, you're reading
57+
multiple bencoded messages from a pipe:
58+
59+
```c++
60+
// Defaults to bencode::check_eof.
61+
auto data = bencode::decode(stream, bencode::no_check_eof);
62+
```
63+
64+
#### Views
65+
66+
If the buffer holding the bencoded data is stable (i.e. won't change or be
67+
destroyed until you're done working with the parsed representation), you can
68+
decode the data as a *view* on the buffer to save memory. This results in all
69+
parsed strings being nothing more than pointers pointing to slices of your
70+
buffer. Simply append `_view` to the functions/types to take advantage of this:
71+
72+
```c++
73+
std::string buf = "3:foo";
74+
auto data = bencode::decode_view(buf);
75+
auto value = boost::get<bencode::string_view>(data);
76+
```
77+
78+
### Encoding
79+
80+
Encoding data is also straightforward:
81+
82+
```c++
83+
// Encode and store the result in an std::string.
84+
auto str = bencode::encode(666);
85+
86+
// Encode and output to an std::ostream.
87+
bencode::encode(std::cout, 666);
88+
```
89+
90+
You can also construct more-complex data structures:
91+
92+
```c++
93+
bencode::encode(std::cout, bencode::dict{
94+
{"one", 1},
95+
{"two", bencode::list{1, "foo", 2},
96+
{"three", "3"}
97+
});
98+
```
99+
100+
As with encoding, you can use the `_view` types if you know the underlying
101+
memory will live until the encoding function returns.
102+
29103
## License
30104

31105
This library is licensed under the BSD 3-Clause license.

0 commit comments

Comments
 (0)