31.01.2014 Views

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Assume that you want to send the file “foo” to a friend over the internet and you want<br />

to make sure that its contents do not get corrupted. <strong>The</strong>n you can easily add a checksum<br />

to your file. All you have to do is to replace the coder in the typedef-statement<br />

by CoderPipe2. <strong>The</strong> class CoderPipe2 combines the<br />

two <strong>LEDA</strong> coders MD5SumCoder (the checksummer) and HuffmanCoder into a single<br />

coder. If the pipe is used for encoding, then the MD5SumCoder is used first and the<br />

HuffmanCoder is applied to its output. In decoding mode the situation is reversed.<br />

<strong>The</strong> standard behaviour of a checksummer like MD5SumCoder is as follows: In encoding<br />

mode it reads the input stream and computes a checksum; the output data basically consists<br />

of the input data with the checksum appended. In decoding mode the checksum is<br />

stripped from the input data and verified. If the input is corrupted the failure flag of the<br />

coder is set to signal this.<br />

Suppose further that your friend has received the encoded file “foo” and wants to decode<br />

it but he does not know which combination of coders you have used for encoding. This is<br />

not a problem because <strong>LEDA</strong> provides a class called AutoDecoder which can be used to<br />

decode any stream that has been encoded by <strong>LEDA</strong>. <strong>The</strong> complete code for this extended<br />

example is depicted below:<br />

#include <br />

#include <br />

using namespace leda;<br />

typedef CoderPipe2 Coder;<br />

int main()<br />

{<br />

string str = "Hello World";<br />

// your code ...<br />

encoding_ofstream out("foo");<br />

out

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!