Example 3:
The AutoDecoder.
L E D A's AutoDecoder can be used to
decode any stream that has been encoded by L
E D A.
#include <LEDA/core/string.h>
#include <LEDA/coding/compression.h>
using namespace leda;
typedef CoderPipe2<MD5SumCoder, HuffmanCoder> Coder;
int main()
{
string str = "Hello World";
// your
code ...
encoding_ofstream<Coder> out("foo");
out << str << "\n";
out.close();
if (out.fail()) std::cout << "error writing
foo" << "\n";
// your
friend's code ...
autodecoding_ifstream in("foo");
// autodecoding_ifstream = decoding_istream<AutoDecoder>
in >> str;
in.close();
if (in.fail()) std::cout << "decoding
error, foo corrupted" << "\n";
std::cout << "decoded string: " << str << "\n";
return 0;
}
This example shows how easy it is to
add compression to existing applications:
You include the header ``LEDA/compression.h'', which makes all classes
in the compression module available. Then you simply replace every
occurrence of ofstream by encoding_ofstream <Coder > and every
occurence of ifstream by autodecoding_ifstream.
|