Advanced Strategic Command
streamencoding.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.org
3  Copyright (C) 1994-2010 Martin Bickel
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 #include "streamencoding.h"
22 #include "basestrm.h"
23 
24 
26 {
27  const int maxsize = 100000;
28  Uint8 buffer[maxsize];
29 
30  int size;
31 
32  {
33  tnfilestream stream ( "asc2_dlg.zip", tnstream::reading );
34  size = stream.readdata(buffer, maxsize, false );
35  }
36 
37  ASCString encodedData;
38 
39  {
40  ASCIIEncodingStream stream;
41  stream.writedata(buffer, size);
42  encodedData = stream.getResult();
43  }
44 
45  {
46  Uint8 buffer2[maxsize];
47 
48  ASCIIDecodingStream stream ( encodedData );
49 
50  int size2 = stream.readdata( buffer2, size, true);
51  assertOrThrow( size2 == size );
52 
53  for ( int i = 0; i < size; ++i )
54  if ( buffer[i] != buffer2[i] )
55  throw ASCmsgException( "ASCII Encoding/Decoding failed at offset " + ASCString::toString(i));
56  }
57 }
58 
60 {
61  const int maxsize = 100000;
62  char buffer[maxsize];
63 
64  int size;
65 
66  {
67  tnfilestream stream ( "asc2_dlg.zip", tnstream::reading );
68  size = stream.readdata(buffer, maxsize, false );
69  }
70 
71  ASCString encodedData;
72 
73  {
74  ASCIIEncodingStream outerStream;
75  StreamCompressionFilter stream( &outerStream );
76  stream.writedata(buffer, size);
77  stream.close();
78 
79  encodedData = outerStream.getResult();
80  }
81 
82  {
83  char buffer2[maxsize];
84 
85  ASCIIDecodingStream outerStream ( encodedData );
86  StreamDecompressionFilter stream( &outerStream );
87 
88  int size2 = stream.readdata( buffer2, size, true);
89  assertOrThrow( size2 == size );
90 
91  for ( int i = 0; i < size; ++i )
92  if ( buffer[i] != buffer2[i] )
93  throw ASCmsgException( "ASCII Encoding/Decoding failed at offset " + ASCString::toString(i));
94  }
95 
96 }
97 
99 {
102 }
ASCString getResult()
Definition: basestrm.cpp:1786
Compresses data transparently into another stream (using bzip2 compression)
Definition: basestrm.h:600
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1753
int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1698
#define assertOrThrow(expr)
Definition: errors.h:69
Reading data from an ASCII encoded String.
Definition: basestrm.h:577
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
Deompresses data from a compressed stream (using bzip2 compression)
Definition: basestrm.h:616
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
virtual int readdata(void *buf, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1898
void testStreamEncoding()
The interface for the various streams that ASC offers, like file and memory streams.
virtual int readdata(void *buffer, int size, bool excpt=true)
Reads data from the stream.
Definition: basestrm.cpp:1827
void testStreamEncoding2()
Data written to this stream will be encoded similar to base64 and can be retrieved as a string...
Definition: basestrm.h:557
void testStreamEncoding1()
virtual void writedata(const void *buf, int size)
writes data to the stream
Definition: basestrm.cpp:1864