comparison base-n/example/basen_example.cpp @ 75:81e1c48e97f9

Rearrange and rename to Piper C++ structure
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 10 Oct 2016 16:31:09 +0100
parents
children
comparison
equal deleted inserted replaced
74:d45cfa25aaad 75:81e1c48e97f9
1 /**
2 * base-n, 1.0
3 * Copyright (C) 2012 Andrzej Zawadzki (azawadzki@gmail.com)
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 **/
23 #include <cassert>
24 #include <cstring>
25 #include <iostream>
26 #include <iterator>
27 #include <string>
28
29 #include "basen.hpp"
30
31 int main()
32 {
33 using namespace std;
34 string in = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
35 cout << in << endl;
36
37 {
38 string encoded;
39 bn::encode_b64(in.begin(), in.end(), back_inserter(encoded));
40 bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
41 cout << endl;
42 }
43 {
44 string encoded;
45 bn::encode_b32(in.begin(), in.end(), back_inserter(encoded));
46 bn::decode_b32(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
47 cout << endl;
48 }
49 {
50 string encoded;
51 bn::encode_b16(in.begin(), in.end(), back_inserter(encoded));
52 bn::decode_b16(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
53 cout << endl;
54 }
55 {
56 string encoded = "#TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n"
57 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbS@BvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n"
58 " dGhlIG1(pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n"
59 "\rdWVkIGFuZCBpbmRlZmF0aWdhY*mxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n"
60 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4";
61 bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
62 cout << endl;
63 }
64 {
65 // move the struct definition outside of main() for non-C++11 compilers
66 struct b8_custom
67 {
68 static size_t group_length()
69 {
70 return 3;
71 }
72
73 static char encode(unsigned int index)
74 {
75 const char* const dictionary = "01234567";
76 assert(index < strlen(dictionary));
77 return dictionary[index];
78 }
79
80 static char decode(char c)
81 {
82 if (c >= '0' && c <= '7') {
83 return c - '0';
84 }
85 return -1;
86 }
87 };
88 string encoded;
89 bn::impl::encode<b8_custom>(in.begin(), in.end(), back_inserter(encoded));
90 bn::impl::decode<b8_custom>(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
91 cout << endl;
92 }
93 }
94