Skip to content

Commit

Permalink
Squashed 'externals/coda-oss/' changes from 72669b0..a11c529
Browse files Browse the repository at this point in the history
a11c529 Merge pull request #281 from mdaus/xerces-parser-hang
afb64a1 Set feature flags to prevent hanging on DTDs
b070887 Merge pull request #278 from mdaus/getReturnType
33b26bd Fix return type for getter
c451ef2 Merge pull request #277 from mdaus/fix_nautical_mile_conversion
845eb13 Fixed bug in nautical miles to feet conversion
8070a11 Merge pull request #274 from mdaus/scratchReleaseTesting
d8a3c9c created two new tests cases for edge cases
8a36a71 Merge pull request #276 from mdaus/gpotts
900837f Added stdc++ explicit operator bool syntax for ScopedCopyablePtr for testing the PTR object to true fals in a boolean expression.
76ac669 Merge pull request #275 from mdaus/fix_library_call
c5860a5 Add missing include in unit test
d68f315 spacing
1bf2d73 removed unecessary code
e4fe1a8 new updated and fixed version of scratch release with generated test cases
ce63ddb Merge pull request #273 from mdaus/buffer_stream
6d73e8c Add include for memcpy
02a2776 Make inherited interface work with bytes instead of elements
3dfba21 Fix bugs when working with larger types
daa41bf Add BufferViewStream

git-subtree-dir: externals/coda-oss
git-subtree-split: a11c52941cd117ec72e0c78d2a0976c10f95378c
  • Loading branch information
JonathanMeans committed Nov 23, 2018
1 parent 8de1d66 commit 2c91d92
Show file tree
Hide file tree
Showing 10 changed files with 668 additions and 36 deletions.
53 changes: 27 additions & 26 deletions modules/c++/io/include/import/io.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* =========================================================================
* This file is part of io-c++
* This file is part of io-c++
* =========================================================================
*
*
* (C) Copyright 2004 - 2014, MDA Information Systems LLC
*
* io-c++ is free software; you can redistribute it and/or modify
Expand All @@ -14,8 +14,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*
*/
Expand All @@ -29,12 +29,12 @@
*
* This package is based on the Java io package.
* For the upcoming 0.1.1 release of the modules,
* we will support the io.File API as well.
* we will support the io.File API as well.
*
* The io package takes a more simple but still powerful
* approach to streaming. We have a built in buffering mechanism
* that can be activated simply by using the InputStream interface's
* streamTo() method, connecting it to an OutputStream.
* streamTo() method, connecting it to an OutputStream.
*
* Many of the streams are also implemented as filters, allowing them
* to pipe or stream, or sort data that is incoming, and affect its
Expand All @@ -51,26 +51,27 @@
*
*/

#include "io/BidirectionalStream.h"
#include "io/ByteStream.h"
#include "io/DataStream.h"
#include "io/DbgStream.h"
#include "io/InputStream.h"
#include "io/OutputStream.h"
#include "io/FileInputStream.h"
#include "io/FileOutputStream.h"
#include "io/Seekable.h"
#include "io/Serializable.h"
#include "io/SerializableFile.h"
#include "io/PipeStream.h"
#include "io/StandardStreams.h"
#include "io/StringStream.h"
#include "io/NullStreams.h"
#include "io/ProxyStreams.h"
#include "io/FileUtils.h"
#include "io/SerializableArray.h"
#include "io/CountingStreams.h"
#include "io/RotatingFileOutputStream.h"
#include <io/BidirectionalStream.h>
#include <io/BufferViewStream.h>
#include <io/ByteStream.h>
#include <io/DataStream.h>
#include <io/DbgStream.h>
#include <io/InputStream.h>
#include <io/OutputStream.h>
#include <io/FileInputStream.h>
#include <io/FileOutputStream.h>
#include <io/Seekable.h>
#include <io/Serializable.h>
#include <io/SerializableFile.h>
#include <io/PipeStream.h>
#include <io/StandardStreams.h>
#include <io/StringStream.h>
#include <io/NullStreams.h>
#include <io/ProxyStreams.h>
#include <io/FileUtils.h>
#include <io/SerializableArray.h>
#include <io/CountingStreams.h>
#include <io/RotatingFileOutputStream.h>

//#include "io/MMapInputStream.h"
//using namespace io;
Expand Down
215 changes: 215 additions & 0 deletions modules/c++/io/include/io/BufferViewStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/* =========================================================================
* This file is part of io-c++
* =========================================================================
*
* (C) Copyright 2004 - 2018, MDA Information Systems LLC
*
* io-c++ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; If not,
* see <http://www.gnu.org/licenses/>.
*
*/

#ifndef __IO_BUFFER_VIEW_STREAM_H__
#define __IO_BUFFER_VIEW_STREAM_H__

#include <mem/BufferView.h>
#include <sys/Conf.h>
#include <except/Error.h>
#include <except/Exception.h>
#include <io/SeekableStreams.h>
#include <string.h>

/*!
* \file
* \brief Class for streaming preallocated data, inherits from
* SeekableInputStream, SeekableOutputStream
*/
namespace io
{
/*!
* \class
* \brief Class for streaming preallocated data, inherits from
* SeekableInputStream, SeekableOutputStream
*/
template <typename T>
class BufferViewStream: public SeekableInputStream, public SeekableOutputStream
{
public:
/*!
* Default constructor
* \param bufferView The BufferView to wrap in the stream
*/
BufferViewStream(mem::BufferView<T>& bufferView) :
mBufferView(bufferView),
mPosition(0)
{
}

//! Returns current location in buffer in bytes
virtual sys::Off_T tell()
{
return mPosition * sizeof(T);
}

/*!
* Seek to given location. Throw an exception if the seek would be out of
* bounds.
* \param offset Offset to seek to
* \param whence Location to seek from
* \return new position
*/
virtual sys::Off_T seek(sys::Off_T offset, Whence whence);

/*
* \return The available bytes to read from the stream
*/
virtual sys::Off_T available()
{
return (mBufferView.size - mPosition) * sizeof(T);
}

using OutputStream::write;
using InputStream::streamTo;

/*
* Writes the bytes in data to the stream.
* \param buffer The data to write to the stream
* \param size The number of bytes to write to the stream
*/
virtual void write(const void* buffer, size_t size);

/*!
* Get a pointer to the internal buffer.
* This pointer should not be treated as valid
* after a call to the seek, write, or reset methods
* \return pointer to the internal buffer
*/
T* get()
{
return mBufferView.data;
}

//! Returns const pointer to internal buffer
const T* get() const
{
return mBufferView.data;
}

/*!
* Overload for reading into a typed buffer
* \param[out] buffer Buffer to read into
* \param numElements How many -elements- (not bytes) to read
*/
sys::SSize_T read(T* buffer, size_t numElements)
{
return InputStream::read(buffer, numElements * sizeof(T)) / sizeof(T);
}

/*!
* Overload for writing from a typed buffer
* \param buffer Buffer to write from
* \param numElements How many -elements- (not bytes) to write
*/
void write(const T* buffer, size_t numElements)
{
write(reinterpret_cast<const void*>(buffer), numElements * sizeof(T));
}

protected:
/*!
* Read up to len bytes of data from this buffer into an array
* update the mark
* \param buffer Buffer to read into
* \param len The length to read
* \return The number of bytes read
*/
virtual sys::SSize_T readImpl(void* buffer, size_t len);


private:
mem::BufferView<T>& mBufferView;
sys::Off_T mPosition;
};

template <typename T>
sys::Off_T BufferViewStream<T>::seek(sys::Off_T offset, Whence whence)
{
offset /= sizeof(T);
// Let's not change anything until we know it will be valid
sys::Off_T newPos = mPosition;
switch (whence)
{
case START:
newPos = offset;
break;
case END:
if (offset > static_cast<sys::Off_T>(mBufferView.size))
{
newPos = 0;
}
else
{
newPos = mBufferView.size - offset;
}
break;
default:
newPos += offset;
break;
}

if (newPos > static_cast<sys::Off_T>(mBufferView.size) || newPos < 0)
{
throw except::Exception(Ctxt("Attempted to seek beyond end of stream"));
}
mPosition = newPos;
return tell();
}

template <typename T>
void BufferViewStream<T>::write(const void* buffer, size_t numBytes)
{
const size_t numElements = numBytes / sizeof(T);
const sys::Size_T newPos = mPosition + numElements;
if (newPos > mBufferView.size)
{
std::ostringstream msg;
msg << "Write of size " << numBytes << " runs out of bounds.";
throw except::Exception(Ctxt(msg.str()));
}

::memcpy(mBufferView.data + mPosition, buffer, numBytes);
mPosition = newPos;
}

template <typename T>
sys::SSize_T BufferViewStream<T>::readImpl(void* buffer, size_t numBytes)
{
size_t numElements = numBytes / sizeof(T);
if (available() < static_cast<sys::Off_T>(numBytes))
{
numBytes = available();
numElements = numBytes / sizeof(T);
}
if (numBytes == 0)
{
return 0;
}

::memcpy(buffer, mBufferView.data + mPosition, numBytes);
mPosition += numElements;
return numBytes;
}
}
#endif

Loading

0 comments on commit 2c91d92

Please sign in to comment.