简体   繁体   中英

Bencode parser in C++

What is the best possible way to go about writing a Bencode Parser in C++. Whilst I am open to the suggestion of an external library which may make the task easier I think that I would learn some valuable lessons in C++ if I wrote my own parser. Keep in mind that I am still open to the suggestion :)

Thanks in advance

It's relatively straightforward, just read how it works on http://en.m.wikipedia.org/wiki/Bencode#section_1for example.

Alternatively, Google found for example this C++ library : https://github.com/kriben/bencode .

You can also get some inspiration from http://effbot.org/zone/bencode.htm , a simple Python implementation.

One of the possible ways, apart of re-implementing the wheel again, is to use libbencode library (I'm the author). It provides template-based approach for building bencode data.

The API is quite limited, but potentially it can fit your needs:

// Create an associative array of Bencode values.
bencode::dict d;

d["port wine"] = bencode::make_integer(777);
d["green"] = bencode::make_string("elephant");

// Create a new Bencode output stream
bencode::ostream os(std::cout.rdbuf());

// Put the data to the stream
os << d;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM