Debian Codegraph @

src/tools/file/input.cpp

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29
30 #include <string.h>
31 #include <iostream>
32
33 #include "file_internal.h"
34 #include "input.h"
35
36 namespace libmwaw_tools
37 {
38 //
39 // InputStream
40 //
41 InputStream::~InputStream()
42 {
43 }
44
45 unsigned char InputStream::readU8()
46 {
47 unsigned long nRead;
48 unsigned char const *data = read(1, nRead);
49 if (!data || nRead != 1) return 0;
50 return data[0];
51 }
52 unsigned short InputStream::readU16()
53 {
54 unsigned long nRead;
55 unsigned char const *data = read(2, nRead);
56 if (!data || nRead != 2) return 0;
57 return static_cast<unsigned short>((data[0]<<8)+data[1]);
58 }
59 unsigned int InputStream::readU32()
60 {
61 unsigned long nRead;
62 unsigned char const *data = read(4, nRead);
63 if (!data || nRead != 4) return 0;
64 return static_cast<unsigned int>((data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]);
65 }
66 char InputStream::read8()
67 {
68 unsigned long nRead;
69 unsigned char const *data = read(1, nRead);
70 if (!data || nRead != 1) return 0;
71 return static_cast<char>(data[0]);
72 }
73 short InputStream::read16()
74 {
75 unsigned long nRead;
76 unsigned char const *data = read(2, nRead);
77 if (!data || nRead != 2) return 0;
78 return static_cast<short>((data[0]<<8)+data[1]);
79 }
80 int InputStream::read32()
81 {
82 unsigned long nRead;
83 unsigned char const *data = read(4, nRead);
84 if (!data || nRead != 4) return 0;
85 return static_cast<int>((data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]);
86 }
87
88 int InputStream::seek(long _offset, SeekType seekType)
89 {
90 long sz = length();
91 if (seekType == SK_CUR)
92 m_offset += _offset;
93 else if (seekType == SK_SET)
94 m_offset = _offset;
95 else if (seekType == SK_END)
96 m_offset = sz+_offset;
97
98 if (m_offset < 0) {
99 m_offset = 0;
100 return 1;
101 }
102 if (m_offset > sz) {
103 m_offset = sz;
104 return 1;
105 }
106 return 0;
107 }
108
109 //
110 // StringStream
111 //
112 StringStream::StringStream(const unsigned char *data, const unsigned long dataSize)
113 : InputStream()
114 , m_buffer(dataSize)
115 {
116 memcpy(&m_buffer[0], data, dataSize);
117 }
118
119 const unsigned char *StringStream::read(unsigned long numBytes, unsigned long &numBytesRead)
120 {
121 numBytesRead = 0;
122
123 if (numBytes == 0)
124 return 0;
125
126 unsigned long numBytesToRead;
127
128 if ((static_cast<unsigned long>(m_offset)+numBytes) < m_buffer.size())
129 numBytesToRead = numBytes;
130 else
131 numBytesToRead = static_cast<unsigned long>(static_cast<long>(m_buffer.size()) - m_offset);
132
133 numBytesRead = numBytesToRead; // about as paranoid as we can be..
134
135 if (numBytesToRead == 0)
136 return 0;
137
138 long oldOffset = m_offset;
139 m_offset += numBytesToRead;
140
141 return &m_buffer[size_t(oldOffset)];
142 }
143 //
144 // FileStream
145 //
146 FileStream::FileStream(char const *path)
147 : InputStream()
148 , m_file(0)
149 , m_isOk(true)
150 , m_buffer()
151 , m_bufferPos(0)
152 {
153 m_file = fopen(path,"r");
154 if (m_file)
155 return;
156 #ifdef DEBUG
157 std::cerr << "FileStream:FileStream can not open " << path << "\n";
158 #endif
159 m_isOk = false;
160 }
161
162 FileStream::~FileStream()
163 {
164 if (!m_isOk) return;
165 if (m_file) fclose(m_file);
166 }
167
168 unsigned char const *FileStream::read(unsigned long numBytes, unsigned long &numBytesRead)
169 {
170 numBytesRead = 0;
171 if (!m_isOk || !m_file)
172 return 0;
173 long lastPos = m_offset+long(numBytes);
174 long fileLength = length();
175 if (lastPos > fileLength) lastPos = fileLength;
176 // check if the buffer is or not ok
177 auto bufSize = long(m_buffer.size());
178 if (m_offset < m_bufferPos || lastPos > m_bufferPos+bufSize) {
179 auto numToRead = size_t(numBytes);
180 if (numToRead < 4096 && m_offset+4096 <= fileLength)
181 numToRead = 4096;
182 else if (numToRead < 4096)
183 numToRead = size_t(fileLength-m_offset);
184 if (numToRead==0)
185 return 0;
186 // try to read numToRead bytes
187 m_bufferPos = m_offset;
188 m_buffer.resize(size_t(numToRead));
189
190 #if 0
191 MWAW_DEBUG_MSG(("FileStream::read called with %ld[%ld]: read %ld bytes\n", m_offset, numBytes, numToRead));
192 #endif
193 if (fseek(m_file, m_offset, SEEK_SET)==-1) {
194 MWAW_DEBUG_MSG(("FileStream::read get error when doing a seek\n"));
195 m_buffer.clear();
196 return 0;
197 }
198 size_t nRead = fread(&(m_buffer[0]), 1, numToRead, m_file);
199 if (nRead != numToRead) {
200 MWAW_DEBUG_MSG(("FileStream::read get error when reading data\n"));
201 m_buffer.resize(size_t(nRead));
202 }
203 }
204
205 bufSize = long(m_buffer.size());
206 if (!bufSize)
207 return 0;
208 numBytesRead = static_cast<unsigned long>(m_bufferPos + bufSize - m_offset);
209 if (numBytesRead > numBytes)
210 numBytesRead = numBytes;
211 unsigned char const *res = &(m_buffer[size_t(m_offset-m_bufferPos)]);
212 m_offset += long(numBytesRead);
213 return res;
214 }
215
216 long FileStream::length()
217 {
218 if (!m_isOk || !m_file) return 0;
219 if (fseek(m_file, 0, SEEK_END)==-1) {
220 MWAW_DEBUG_MSG(("FileStream::length get error when reading data\n"));
221 return 0;
222 }
223 return long(ftell(m_file));
224 }
225 }
226 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: