root/trunk/whisperlib/common/io/buffer/io_memory_stream.cc

Revision 7, 3.9 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include "common/io/buffer/io_memory_stream.h"
33
34 io::IOMemoryStream::IOMemoryStream(common::ByteOrder byte_order,
35                                    BlockSize block_size)
36     : io::InputStream(byte_order, NULL, NULL),
37       io::OutputStream(byte_order, NULL, NULL),
38       ms_(new io::MemoryStream(byte_order, block_size)),
39       localms_(true) {
40 }
41
42 io::IOMemoryStream::IOMemoryStream(io::MemoryStream* ms_to_wrap)
43     : io::InputStream(ms_to_wrap->byte_order(), NULL, NULL),
44       io::OutputStream(ms_to_wrap->byte_order(), NULL, NULL),
45       ms_(ms_to_wrap),
46       localms_(false) {
47 }
48
49 io::IOMemoryStream::~IOMemoryStream() {
50   if ( localms_ ) {
51     delete ms_;
52   }
53 }
54
55 bool io::IOMemoryStream::operator==(const io::IOMemoryStream& in) const {
56   if ( Size() != in.Size() ) {
57     return false;
58   }
59
60   io::MemoryStream& a = const_cast<io::MemoryStream&>(*ms_);
61   const int32 original_a_size = a.Size();
62   a.MarkerSet();
63
64   io::MemoryStream& b = const_cast<io::MemoryStream&>(*in.ms_);
65   const int32 original_b_size = b.Size();
66   b.MarkerSet();
67
68   const char*  pA = NULL;
69   int32        nA = 0;
70   const char*  pB = NULL;
71   int32        nB = 0;
72   bool hasMoreDataA = false;
73   bool hasMoreDataB = false;
74   bool equals = false;
75   while ( true ) {
76     if ( nA == 0 ) {
77       hasMoreDataA = a.ReadNext(&pA, &nA);
78     }
79     if ( nB == 0 ) {
80       hasMoreDataB = b.ReadNext(&pB, &nB);
81     }
82     if ( !hasMoreDataA && !hasMoreDataB ) {
83       CHECK_EQ(nA, 0);
84       CHECK_EQ(nB, 0);
85       equals = true;
86       break;
87     }
88     if ( hasMoreDataA ^ hasMoreDataB ) {
89       // we've already checked that both streams have the same size !
90       CHECK(false);
91       equals = false;
92       break;
93     }
94     CHECK(hasMoreDataA);
95     CHECK(hasMoreDataB);
96     CHECK_GT(nA, 0);
97     CHECK_GT(nB, 0);
98     int32 d = min(nA, nB);
99     if ( ::memcmp(pA, pB, d) ) {
100       equals = false;
101       break;
102     }
103     nA -= d;
104     nB -= d;
105   }
106   a.MarkerRestore();
107   b.MarkerRestore();
108
109   // streams should not be modified
110   CHECK_EQ(a.Size(), original_a_size);
111   CHECK_EQ(b.Size(), original_b_size);
112   return equals;
113 }
114
115 string io::IOMemoryStream::ToString() const {
116 #ifdef _DEBUG
117   return (strutil::StringPrintf("IOMemoryStream [#%d bytes]\n",
118                                 static_cast<int32>(ms_->Size())) +
119           ms_->DumpContent());
120 #else
121   return (strutil::StringPrintf("IOMemoryStream [#%d bytes]\n",
122                                 static_cast<int32>(ms_->Size())));
123 #endif
124 }
Note: See TracBrowser for help on using the browser.