1 /*
2  * BufferedFileBinaryInputOutputPort.h - <file binary input/output port>
3  *
4  *   Copyright (c) 2009  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: BufferedFileBinaryInputOutputPort.h 1227 2009-02-21 03:01:29Z higepon $
30  */
31 
32 #ifndef SCHEME_BUFFERED_FILE_BINARY_INPUT_OUTPUT_PORT_
33 #define SCHEME_BUFFERED_FILE_BINARY_INPUT_OUTPUT_PORT_
34 
35 #include "BinaryInputOutputPort.h"
36 
37 namespace scheme {
38 
39 class BufferedFileBinaryInputOutputPort : public BinaryInputOutputPort
40 {
41 public:
42     BufferedFileBinaryInputOutputPort(const ucs4string& file, int openFlags);
43     virtual ~BufferedFileBinaryInputOutputPort();
44 
45     // port interfaces
46     bool hasPosition() const;
47     bool hasSetPosition() const;
48     Object position() const;
49     int close();
50     int pseudoClose();
51     bool setPosition(int64_t position);
52     ucs4string toString();
53 
54     // binary port interfaces
55     int open();
56     bool isClosed() const;
57 
58     // input interfaces
59     int getU8();
60     int lookaheadU8();
61     int64_t readBytes(uint8_t* buf, int64_t reqSize, bool& isErrorOccured);
62     int64_t readSome(uint8_t** buf, bool& isErrorOccured);
63     int64_t readAll(uint8_t** buf, bool& isErrorOccured);
64 
65     // output interfaces
66     int putU8(uint8_t v);
67     int64_t putU8(uint8_t* v, int64_t size);
68     int64_t putByteVector(ByteVector* bv, int64_t start = 0);
69     int64_t putByteVector(ByteVector* bv, int64_t start, int64_t count);
70     void flush();
71     void internalFlush();
72     File* getFile();
getLastErrorMessage()73     ucs4string getLastErrorMessage()
74     {
75         return file_->getLastErrorMessage();
76     }
77 
78 protected:
79     enum {
80         BUF_SIZE = 8192,
81     };
82 
83     void forwardPosition(int64_t offset);
84     void initializeBuffer();
85     virtual int64_t writeToBuffer(uint8_t* buf, int64_t size) = 0;
86     int64_t readFromBuffer(uint8_t* dest, int64_t reqSize);
87     void fillBuffer();
isBufferDirty()88     bool isBufferDirty() { return isDirty_; }
89     void invalidateBuffer();
90 
91     File* file_;
92     ucs4string fileName_;
93     uint8_t* buffer_;
94     bool isDirty_;
95     int64_t position_;
96     bool isClosed_;
97     bool isPseudoClosed_;
98     int64_t bufferSize_;
99     int64_t bufferIndex_;
100 };
101 
102 } // namespace scheme
103 
104 #endif // SCHEME_BUFFERED_FILE_BINARY_INPUT_OUTPUT_PORT_
105