From 68d41d3b214856f8d321b4dbdffc0cd4c7333c36 Mon Sep 17 00:00:00 2001 From: Krzysztof Dermont Date: Mon, 23 Jun 2014 23:19:55 +0200 Subject: More work on streams --- src/common/resources/inputstreambuffer.cpp | 129 +++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/common/resources/inputstreambuffer.cpp (limited to 'src/common/resources/inputstreambuffer.cpp') diff --git a/src/common/resources/inputstreambuffer.cpp b/src/common/resources/inputstreambuffer.cpp new file mode 100644 index 0000000..b8710e9 --- /dev/null +++ b/src/common/resources/inputstreambuffer.cpp @@ -0,0 +1,129 @@ +// * This file is part of the COLOBOT source code +// * Copyright (C) 2014 Polish Portal of Colobot (PPC) +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see http://www.gnu.org/licenses/. + +#include "common/resources/inputstreambuffer.h" + +#include +#include + +CInputStreamBuffer::CInputStreamBuffer(size_t buffer_size) : m_buffer_size(buffer_size) +{ + if (buffer_size <= 0) + { + throw std::runtime_error("File buffer must be larger then 0 bytes"); + } + + m_buffer = new char[buffer_size]; + m_file = nullptr; +} + + +CInputStreamBuffer::~CInputStreamBuffer() +{ + close(); + delete m_buffer; +} + + +void CInputStreamBuffer::open(const std::string &filename) +{ + if (PHYSFS_isInit()) + m_file = PHYSFS_openRead(filename.c_str()); +} + + +void CInputStreamBuffer::close() +{ + if (is_open()) + PHYSFS_close(m_file); +} + + +bool CInputStreamBuffer::is_open() +{ + return m_file; +} + + +size_t CInputStreamBuffer::size() +{ + return PHYSFS_fileLength(m_file); +} + + +std::streambuf::int_type CInputStreamBuffer::underflow() +{ + if (gptr() < egptr()) + return traits_type::to_int_type(*gptr()); + + if (PHYSFS_eof(m_file)) + return traits_type::eof(); + + PHYSFS_sint64 read_count = PHYSFS_read(m_file, m_buffer, sizeof(char), m_buffer_size); + if (read_count <= 0) + return traits_type::eof(); + + setg(m_buffer, m_buffer, m_buffer + read_count); + + return traits_type::to_int_type(*gptr()); +} + + +std::streampos CInputStreamBuffer::seekpos(std::streampos sp, std::ios_base::openmode which) +{ + return seekoff(off_type(sp), std::ios_base::beg, which); +} + + +std::streampos CInputStreamBuffer::seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which) +{ + /* A bit of explanation: + We are reading file by m_buffer_size parts so our 3 internal pointers will be + * eback (not used here) - start of block + * gptr - position of read cursor in block + * egtpr - end of block + off argument is relative to way */ + + std::streamoff new_position; + + switch (way) + { + case std::ios_base::beg: + new_position = off; + break; + + case std::ios_base::cur: + // tell will give cursor at begining of block so we have to add where in block we currently are + new_position = off + static_cast(PHYSFS_tell(m_file)) - static_cast (egptr() - gptr()); + break; + + case std::ios_base::end: + new_position = off + static_cast(PHYSFS_fileLength(m_file)); + break; + + default: + break; + } + + if (PHYSFS_seek(m_file, new_position)) + { + setg(m_buffer, m_buffer, m_buffer); // reset buffer + + return pos_type(new_position); + } + + return pos_type(off_type(-1)); +} -- cgit v1.2.3-1-g7c22 From 6a1ceba8c0914691ebc873fd666e7cde1cffdb64 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 7 Sep 2014 19:26:06 +0200 Subject: Some cleaning up --- src/common/resources/inputstreambuffer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/common/resources/inputstreambuffer.cpp') diff --git a/src/common/resources/inputstreambuffer.cpp b/src/common/resources/inputstreambuffer.cpp index b8710e9..cfa06fb 100644 --- a/src/common/resources/inputstreambuffer.cpp +++ b/src/common/resources/inputstreambuffer.cpp @@ -68,14 +68,14 @@ std::streambuf::int_type CInputStreamBuffer::underflow() { if (gptr() < egptr()) return traits_type::to_int_type(*gptr()); - + if (PHYSFS_eof(m_file)) return traits_type::eof(); - + PHYSFS_sint64 read_count = PHYSFS_read(m_file, m_buffer, sizeof(char), m_buffer_size); if (read_count <= 0) return traits_type::eof(); - + setg(m_buffer, m_buffer, m_buffer + read_count); return traits_type::to_int_type(*gptr()); @@ -98,7 +98,7 @@ std::streampos CInputStreamBuffer::seekoff(std::streamoff off, std::ios_base::se off argument is relative to way */ std::streamoff new_position; - + switch (way) { case std::ios_base::beg: -- cgit v1.2.3-1-g7c22 From 47ea8a1175488564c11e09f71447cb19515c6e6c Mon Sep 17 00:00:00 2001 From: krzys-h Date: Wed, 15 Oct 2014 21:14:34 +0200 Subject: Updated some more license headers --- src/common/resources/inputstreambuffer.cpp | 33 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src/common/resources/inputstreambuffer.cpp') diff --git a/src/common/resources/inputstreambuffer.cpp b/src/common/resources/inputstreambuffer.cpp index cfa06fb..7059d60 100644 --- a/src/common/resources/inputstreambuffer.cpp +++ b/src/common/resources/inputstreambuffer.cpp @@ -1,18 +1,21 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2014 Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsiteс.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ #include "common/resources/inputstreambuffer.h" -- cgit v1.2.3-1-g7c22 From ca4f1e85d2812ad715e21be96413efe155b58a84 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 26 Oct 2014 18:30:56 +0100 Subject: Support for %lvl% in all commands Except for TerrainInitTextures (I'm not sure what it does but it does something weird) --- src/common/resources/inputstreambuffer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/common/resources/inputstreambuffer.cpp') diff --git a/src/common/resources/inputstreambuffer.cpp b/src/common/resources/inputstreambuffer.cpp index 7059d60..9ac1fec 100644 --- a/src/common/resources/inputstreambuffer.cpp +++ b/src/common/resources/inputstreambuffer.cpp @@ -19,6 +19,8 @@ #include "common/resources/inputstreambuffer.h" +#include "common/resources/resourcemanager.h" + #include #include @@ -44,7 +46,7 @@ CInputStreamBuffer::~CInputStreamBuffer() void CInputStreamBuffer::open(const std::string &filename) { if (PHYSFS_isInit()) - m_file = PHYSFS_openRead(filename.c_str()); + m_file = PHYSFS_openRead(CResourceManager::CleanPath(filename).c_str()); } -- cgit v1.2.3-1-g7c22