annotate base-n/example/basen_example.cpp @ 93:fbce91785d35

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