annotate ext/base-n/example/basen_example.cpp @ 150:bf8e3e7dd7de

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