annotate json/base-n/example/basen_example.cpp @ 5:6e8607ebad03

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