dune-grid  2.1.1
b64enc.hh
Go to the documentation of this file.
00001 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002 // vi: set et ts=8 sw=4 sts=4:
00003 
00004 #ifndef DUNE_GRID_IO_FILE_VTK_B64ENC_HH
00005 #define DUNE_GRID_IO_FILE_VTK_B64ENC_HH
00006 
00007 #include <assert.h>
00008 
00009 namespace Dune {
00010 
00019 const char base64table[] =
00020 {
00021     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
00022     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
00023     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
00024     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
00025     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
00026 };
00027 
00029 struct b64txt
00030 {
00031     typedef unsigned char size_type;
00032     size_type size;
00033     char txt[3];
00034     int read(const char* t, size_type s)
00035     {
00036         size = s>=3?3:s;
00037         txt[2] = s>0?t[0]:0;
00038         txt[1] = s>1?t[1]:0;
00039         txt[0] = s>2?t[2]:0;
00040         return size;
00041     }
00042     void put(const char c)
00043     {
00044         assert (size < 3);
00045         txt[2-size++] = c;
00046     }
00047 };
00048 
00050 struct b64data
00051 {
00052     typedef unsigned char size_type;
00053     size_type size;
00054     unsigned A : 6;
00055     unsigned B : 6;
00056     unsigned C : 6;
00057     unsigned D : 6;
00058     void write(char* t)
00059     {
00060         t[3] = size>2?base64table[A]:'=';
00061         t[2] = size>1?base64table[B]:'=';
00062         t[1] = size>0?base64table[C]:'=';
00063         t[0] = size>0?base64table[D]:'=';
00064         size = 0;
00065     }
00066 };
00067 
00069 union b64chunk
00070 {
00071     b64txt txt;
00072     b64data data; 
00073 };
00074 
00077 } // namespace Dune
00078 
00079 #endif // DUNE_GRID_IO_FILE_VTK_B64ENC_HH