1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_data_stream.cpp
28 #include "vogl_core.h"
29 #include "vogl_data_stream.h"
30 #include "vogl_file_utils.h"
34 data_stream::data_stream()
37 m_opened(false), m_error(false), m_got_cr(false)
41 data_stream::data_stream(const char *pName, uint attribs)
44 m_attribs(static_cast<uint16>(attribs)),
45 m_opened(false), m_error(false), m_got_cr(false)
49 uint64_t data_stream::read64(void *pBuf, uint64_t len)
51 uint64_t total_bytes_read = 0;
53 while (total_bytes_read < len)
55 uint bytes_to_read = static_cast<uint>(math::minimum<uint64_t>(0x7FFFFFFFU, len - total_bytes_read));
57 if (read(static_cast<uint8 *>(pBuf) + total_bytes_read, bytes_to_read) != bytes_to_read)
60 total_bytes_read += bytes_to_read;
63 return total_bytes_read;
66 uint64_t data_stream::skip(uint64_t len)
68 uint64_t total_bytes_read = 0;
70 const uint cBufSize = 1024;
75 const uint64_t bytes_to_read = math::minimum<uint64_t>(sizeof(buf), len);
76 const uint64_t bytes_read = read(buf, static_cast<uint>(bytes_to_read));
77 total_bytes_read += bytes_read;
79 if (bytes_read != bytes_to_read)
85 return total_bytes_read;
88 bool data_stream::read_line(dynamic_string &str)
94 const int c = read_byte();
96 const bool prev_got_cr = m_got_cr;
106 // ignores DOS EOF, assumes it's at the very end of the file
107 else if ((26 == c) || (!c))
122 str.append_char(static_cast<char>(c));
128 bool data_stream::printf(const char *p, ...)
134 buf.format_args(p, args);
137 return write(buf.get_ptr(), buf.get_len() * sizeof(char)) == buf.get_len() * sizeof(char);
140 bool data_stream::puts(const char *p)
142 uint len = vogl_strlen(p);
143 return write(p, len * sizeof(char)) == len * sizeof(char);
146 bool data_stream::puts(const dynamic_string &str)
149 return write(str.get_ptr(), str.get_len()) == str.get_len();
154 bool data_stream::read_array(vector<uint8> &buf)
156 if (buf.size() < get_remaining())
158 //if (get_remaining() > 1024U*1024U*1024U)
159 if (get_remaining() > static_cast<uint64_t>(cINT32_MAX))
162 if (!buf.try_resize(static_cast<uint>(get_remaining())))
166 if (!get_remaining())
172 return read(&buf[0], buf.size()) == buf.size();
175 bool data_stream::write_array(const vector<uint8> &buf)
178 return write(&buf[0], buf.size()) == buf.size();
182 bool data_stream::write_file_data(const char *pFilename)
185 if (!file_utils::get_file_size(pFilename, file_size))
191 FILE *pFile = vogl_fopen(pFilename, "rb");
195 uint8_vec buf(64 * 1024);
197 uint64_t bytes_remaining = file_size;
198 while (bytes_remaining)
200 uint n = static_cast<uint>(math::minimum<uint64_t>(buf.size(), bytes_remaining));
202 if (vogl_fread(buf.get_ptr(), 1, n, pFile) != n)
208 if (write(buf.get_ptr(), n) != n)
214 bytes_remaining -= n;