| 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 |
#ifndef __COMMON_IO_INPUT_STREAM_H__ |
|---|
| 33 |
#define __COMMON_IO_INPUT_STREAM_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
|
|---|
| 37 |
#include <whisperlib/common/base/types.h> |
|---|
| 38 |
#include <whisperlib/common/base/system.h> |
|---|
| 39 |
#include <whisperlib/common/base/log.h> |
|---|
| 40 |
#include <whisperlib/common/base/strutil.h> |
|---|
| 41 |
|
|---|
| 42 |
#include <whisperlib/common/io/stream_base.h> |
|---|
| 43 |
#include <whisperlib/common/io/iomarker.h> |
|---|
| 44 |
#include <whisperlib/common/io/seeker.h> |
|---|
| 45 |
|
|---|
| 46 |
namespace io { |
|---|
| 47 |
|
|---|
| 48 |
class InputStream : public StreamBase { |
|---|
| 49 |
public: |
|---|
| 50 |
InputStream() : StreamBase() { |
|---|
| 51 |
} |
|---|
| 52 |
InputStream(common::ByteOrder byte_order, |
|---|
| 53 |
IoMarker* marker, Seeker* seeker) |
|---|
| 54 |
: StreamBase(byte_order, marker, seeker) { |
|---|
| 55 |
} |
|---|
| 56 |
virtual ~InputStream() { |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
// Reads "len" bytes of data into the given "buffer". |
|---|
| 60 |
// Returns the number of bytes read. Can be less than "len" if less bytes |
|---|
| 61 |
// are available. |
|---|
| 62 |
virtual int32 Read(void* buffer, int32 len) = 0; |
|---|
| 63 |
|
|---|
| 64 |
// Same as read, but w/ strings |
|---|
| 65 |
// If len == -1 read to the end of input, else read len bytes. Returns |
|---|
| 66 |
// anyway the number of read bytes |
|---|
| 67 |
virtual int32 ReadString(string* s, int32 len = -1) { |
|---|
| 68 |
if ( len == -1 ) |
|---|
| 69 |
len = Readable(); |
|---|
| 70 |
string tmp; |
|---|
| 71 |
tmp.reserve(len); |
|---|
| 72 |
const int32 cb = Read(&tmp[0], len); |
|---|
| 73 |
s->assign(tmp.c_str(), cb); |
|---|
| 74 |
return cb; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
// 'Peeks' len bytes from the stream at the current position. On return the |
|---|
| 78 |
// read pointer is left unchanged. |
|---|
| 79 |
// Returns the number of bytes read or 0 in not available or operation not |
|---|
| 80 |
// supported. |
|---|
| 81 |
virtual int32 Peek(void* buffer, int32 len) = 0; |
|---|
| 82 |
|
|---|
| 83 |
// Passes over "len" bytes. (Advances the read head by the "len" number of |
|---|
| 84 |
// bytes). |
|---|
| 85 |
// Returns the number of bytes skipped. Can be less than "len" if less |
|---|
| 86 |
// bytes are available. |
|---|
| 87 |
virtual int64 Skip(int64 len) = 0; |
|---|
| 88 |
|
|---|
| 89 |
// Returns how many bytes are readable in the stream until EOS |
|---|
| 90 |
virtual int64 Readable() const = 0; |
|---|
| 91 |
|
|---|
| 92 |
// Returns (Readable() == 0) (Usually much faster) |
|---|
| 93 |
virtual bool IsEos() const = 0; |
|---|
| 94 |
|
|---|
| 95 |
// Sets the read marker in the current stream possition. |
|---|
| 96 |
virtual void MarkerSet() = 0; |
|---|
| 97 |
|
|---|
| 98 |
// Restores the read position of the associated stream to the position |
|---|
| 99 |
// previously marked by set. |
|---|
| 100 |
virtual void MarkerRestore() = 0; |
|---|
| 101 |
|
|---|
| 102 |
// Removes the last read mark for the underlying stream |
|---|
| 103 |
virtual void MarkerClear() = 0; |
|---|
| 104 |
|
|---|
| 105 |
// !! NOTE !! |
|---|
| 106 |
// -- This is amazingly expensive !! Use it only for debugging -- |
|---|
| 107 |
virtual string ToString() const { |
|---|
| 108 |
#ifdef _DEBUG |
|---|
| 109 |
InputStream& in = const_cast<InputStream&>(*this); |
|---|
| 110 |
const int64 size = in.Readable(); |
|---|
| 111 |
in.MarkerSet(); |
|---|
| 112 |
string buffer; |
|---|
| 113 |
in.ReadString(&buffer); |
|---|
| 114 |
in.MarkerRestore(); |
|---|
| 115 |
DCHECK_EQ(in.Readable(), size); |
|---|
| 116 |
|
|---|
| 117 |
return (strutil::StringPrintf("InputStream [#%lld bytes]:\n", |
|---|
| 118 |
static_cast<long long int>(size)) + |
|---|
| 119 |
strutil::PrintableDataBuffer(buffer.data(), buffer.size())); |
|---|
| 120 |
#else |
|---|
| 121 |
return (strutil::StringPrintf("InputStream [#%lld bytes]:\n", |
|---|
| 122 |
static_cast<long long int>(Readable()))); |
|---|
| 123 |
#endif |
|---|
| 124 |
} |
|---|
| 125 |
virtual string PeekString() const { |
|---|
| 126 |
string s; |
|---|
| 127 |
InputStream& in = const_cast<InputStream&>(*this); |
|---|
| 128 |
const int64 size = in.Readable(); |
|---|
| 129 |
in.MarkerSet(); |
|---|
| 130 |
in.ReadString(&s); |
|---|
| 131 |
in.MarkerRestore(); |
|---|
| 132 |
CHECK_EQ(in.Readable(), size); |
|---|
| 133 |
return s; |
|---|
| 134 |
} |
|---|
| 135 |
private: |
|---|
| 136 |
DISALLOW_EVIL_CONSTRUCTORS(InputStream); |
|---|
| 137 |
}; |
|---|
| 138 |
inline std::ostream& operator<<(std::ostream& os, |
|---|
| 139 |
const InputStream& in) { |
|---|
| 140 |
return os << in.ToString(); |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
#endif // __COMMON_IO_INPUT_STREAM_H__ |
|---|