GithubHelp home page GithubHelp logo

ybubnov / libbencode Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 3.0 70 KB

The Bencode library is an elementary template-based implementation of the data serialization into the Bencode format.

License: MIT License

C++ 98.71% CMake 1.29%
bencode cpp11

libbencode's Introduction

libbencode - The Bencode coding library

Build Status

The Bencode library is an elementary template-based implementation of the data serialization into the Bencode format. The Bencode format is commonly used in the BitTorrent protocol to encode the metadata of torrents.

Installation

The build mechanism of the library is based on the CMake tool. It could be either compiled from sources or installed from the distribution repository (e.g. for debian-based systems):

$ sudo apt-get install cmake

To keep the source tree clean, it would be good to create a separate folder were all compiled binaries and generated makefiles will reside:

$ mkdir build

As the directory created, all required build scripts will be generated inside by executing the following commands:

$ cd build
$ cmake ..

After successful scripts generation, the library could be compiled and optionally tested.

$ make
$ make test

By default library will be installed into the /usr/local directory. The destination path could be changed by specifying the -DCMAKE_INSTALL_PREFIX attribute on the step of makefile generation:

$ cmake -DCMAKE_INSTALL_PREFIX=/usr ..

The library consits only of the headers file, therefore the installation does not require any source code compilation:

$ make install

Optionally, the destination path could be overridden with DESTDIR environment variable:

$ make DESTDIR=/opt install

Usage

Examples

The usage is pretty straightforward. Here is an example of dictionary and a list creation:

// 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 2-elements list of Bencode values.
bencode::list l(2);

l[0] = bencode::make_string("worsening");
l[1] = bencode::make_string("retaliation");

Input-output stream

The library provides input and output classes derived from std::basic_istream and std::basic_ostream prespectively to perform the handfull values serialization.

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

// Create a new Bencode value
bencode::string s("a string");

// output: `8:a string`
os << s;

User-defined operators

To create polimorphic shared pointers to the Bencode values, the syntax of string literals could be used:

// Create a bencode string.
auto s = "6:string"_bencode;

// Create a bencode integer.
auto i = "i777e"_bencode;

// Create a bencode list.
auto l = "li1ei2ei3ee"_bencode;

// Create a bencode dictionary.
auto d = "d5:first:i1e6:second:i2ee"_bencode;

License

The Bencode library is distributed under MIT license, therefore you are free to do with code whatever you want. See the LICENSE file for full license text.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.