1 /*
2  * ByteArrayBinaryInputPort.h -
3  *
4  *   Copyright (c) 2008  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  $Id: ByteArrayBinaryInputPort.h 261 2008-07-25 06:16:44Z higepon $
30  */
31 
32 #ifndef SCHEME_BYTE_ARRAY_BINARY_INPUT_PORT_
33 #define SCHEME_BYTE_ARRAY_BINARY_INPUT_PORT_
34 
35 #include <stdio.h> // EOF
36 #include "BinaryInputPort.h"
37 
38 namespace scheme {
39 
40 class ByteArrayBinaryInputPort : public BinaryInputPort
41 {
42 public:
43     ByteArrayBinaryInputPort(const uint8_t* buf, int64_t size);
44     ~ByteArrayBinaryInputPort();
45 
46     // profiler tells that this should be inlined
getU8()47     inline int getU8()
48     {
49         if (index_ >= size_) return EOF;
50         return buf_[index_++];
51     }
52 
lookaheadU8()53     inline int lookaheadU8()
54     {
55         if (index_ >= size_) return EOF;
56         return buf_[index_];
57     }
58 
59     ucs4string toString();
60     int64_t readBytes(uint8_t* buf, int64_t reqSize, bool& isErrorOccured);
61     int64_t readSome(uint8_t** buf, bool& isErrorOccured);
62     int64_t readAll(uint8_t** buf, bool& isErrorOccured);
63     int open();
64     int close();
65     bool isClosed() const;
66     int pseudoClose();
hasPosition()67     bool hasPosition() const { return true; }
hasSetPosition()68     bool hasSetPosition() const { return true; }
69     Object position() const;
70     File* getFile();
setPosition(int64_t position)71     bool setPosition(int64_t position)
72     {
73         if (size_ == 0 && position == 0) {
74             return true;
75         } else if (position > size_) {
76             return false;
77         } else {
78             index_ = position;
79             return true;
80         }
81     }
getLastErrorMessage()82     ucs4string getLastErrorMessage()
83     {
84         return UC("");
85     }
86 
87 
88 private:
89     const uint8_t* const buf_;
90     const int64_t size_;
91     int64_t index_;
92     bool isClosed_;
93     bool isPseudoClosed_;
94 };
95 
96 } // namespace scheme
97 
98 #endif // SCHEME_BYTE_ARRAY_BINARY_INPUT_PORT_
99