Skip to content

Commit

Permalink
latest from coda-oss
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Apr 18, 2022
1 parent f8fb45a commit 543bf38
Show file tree
Hide file tree
Showing 24 changed files with 321 additions and 115 deletions.
4 changes: 4 additions & 0 deletions externals/coda-oss/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
```
# coda-oss Release Notes

## WIP: (Release 202?-??-??)
* Fixed a bug in `Poly2D::atY()`; imporved `flipXY()` behavior.
* Implement [std::filesystem::file_size()](https://en.cppreference.com/w/cpp/filesystem/file_size).

## (Release 2022-02-22)
* new `EnocdedString` and `EncodedStringView` to manage strings in different encodings
* XML containing UTF-8 characters can now be validated
Expand Down
44 changes: 24 additions & 20 deletions externals/coda-oss/modules/c++/io/include/io/BufferViewStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
*
*/

#ifndef __IO_BUFFER_VIEW_STREAM_H__
#define __IO_BUFFER_VIEW_STREAM_H__
#ifndef CODA_OSS_io_BufferViewStream_h_INCLUDED_
#define CODA_OSS_io_BufferViewStream_h_INCLUDED_

#include <string.h>

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

/*!
* \file
Expand All @@ -43,23 +45,25 @@ namespace io
* SeekableInputStream, SeekableOutputStream
*/
template <typename T>
class BufferViewStream: public SeekableInputStream, public SeekableOutputStream
struct BufferViewStream: public SeekableInputStream, public SeekableOutputStream
{
public:
/*!
* Default constructor
* \param bufferView The BufferView to wrap in the stream
*/
BufferViewStream(const mem::BufferView<T>& bufferView) :
mBufferView(bufferView),
mPosition(0)
mBufferView(bufferView)
{
}
BufferViewStream(const BufferViewStream&) = delete;
BufferViewStream& operator=(const BufferViewStream&) = delete;
BufferViewStream(BufferViewStream&&) = default;
BufferViewStream& operator=(BufferViewStream&&) = delete;

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

/*!
Expand All @@ -76,7 +80,7 @@ class BufferViewStream: public SeekableInputStream, public SeekableOutputStream
*/
virtual sys::Off_T available()
{
return (mBufferView.size - mPosition) * sizeof(T);
return gsl::narrow<sys::Off_T>((mBufferView.size - mPosition) * sizeof(T));
}

using OutputStream::write;
Expand Down Expand Up @@ -140,7 +144,7 @@ class BufferViewStream: public SeekableInputStream, public SeekableOutputStream

private:
const mem::BufferView<T> mBufferView;
sys::Off_T mPosition;
sys::Off_T mPosition = 0;
};

template <typename T>
Expand All @@ -155,21 +159,22 @@ sys::Off_T BufferViewStream<T>::seek(sys::Off_T offset, Whence whence)
newPos = offset;
break;
case END:
if (offset > static_cast<sys::Off_T>(mBufferView.size))
if (offset > gsl::narrow<sys::Off_T>(mBufferView.size))
{
newPos = 0;
}
else
{
newPos = mBufferView.size - offset;
newPos = gsl::narrow<sys::Off_T>(mBufferView.size - offset);
}
break;
case CURRENT:
default:
newPos += offset;
break;
}

if (newPos > static_cast<sys::Off_T>(mBufferView.size) || newPos < 0)
if (newPos > gsl::narrow<sys::Off_T>(mBufferView.size) || newPos < 0)
{
throw except::Exception(Ctxt("Attempted to seek beyond end of stream"));
}
Expand All @@ -181,7 +186,7 @@ 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;
const auto newPos = mPosition + numElements;
if (newPos > mBufferView.size)
{
std::ostringstream msg;
Expand All @@ -190,16 +195,16 @@ void BufferViewStream<T>::write(const void* buffer, size_t numBytes)
}

::memcpy(mBufferView.data + mPosition, buffer, numBytes);
mPosition = newPos;
mPosition = gsl::narrow<sys::Off_T>(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))
if (available() < gsl::narrow<sys::Off_T>(numBytes))
{
numBytes = available();
numBytes = gsl::narrow<size_t>(available());
numElements = numBytes / sizeof(T);
}
if (numBytes == 0)
Expand All @@ -209,8 +214,7 @@ sys::SSize_T BufferViewStream<T>::readImpl(void* buffer, size_t numBytes)

::memcpy(buffer, mBufferView.data + mPosition, numBytes);
mPosition += numElements;
return numBytes;
return gsl::narrow<sys::SSize_T>(numBytes);
}
}
#endif

#endif // CODA_OSS_io_BufferViewStream_h_INCLUDED_
7 changes: 7 additions & 0 deletions externals/coda-oss/modules/c++/io/include/io/InputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifndef __IO_INPUT_STREAM_H__
#define __IO_INPUT_STREAM_H__

#include "coda_oss/span.h"

#include "sys/Dbg.h"
#include "io/OutputStream.h"

Expand Down Expand Up @@ -85,6 +87,11 @@ struct InputStream
sys::SSize_T read(void* buffer,
size_t len,
bool verifyFullRead = false);
template<typename T>
sys::SSize_T read(coda_oss::span<T> buffer, bool verifyFullRead = false)
{
return read(buffer.data(), buffer.size_bytes(), verifyFullRead);
}

/*!
* Read either a buffer of len size, or up until a newline,
Expand Down
25 changes: 13 additions & 12 deletions externals/coda-oss/modules/c++/io/include/io/OutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "sys/Conf.h"
#include "coda_oss/string.h"
#include "coda_oss/cstddef.h"
#include "coda_oss/span.h"

/*!
* \file OutputStream.h
Expand Down Expand Up @@ -71,23 +72,13 @@ struct OutputStream
* Write a string
* \param str
*/
void write(std::string::const_pointer pStr, size_t length) // i.e., std::string_view
{
const void* const pStr_ = pStr;
write(pStr_, length);
}
void write(const std::string& str)
{
write(str.c_str(), str.length());
}
void write(coda_oss::u8string::const_pointer pStr, size_t length) // i.e., std::string_view
{
const void* const pStr_ = pStr;
write(pStr_, length);
write(coda_oss::span<const std::string::value_type>(str.data(), str.size()));
}
void write(const coda_oss::u8string& str)
{
write(str.c_str(), str.length());
write(coda_oss::span<const coda_oss::u8string::value_type>(str.data(), str.size()));
}

/*!
Expand All @@ -114,6 +105,16 @@ struct OutputStream
* \throw IOException
*/
virtual void write(const void* buffer, size_t len) = 0;
template<typename T>
void write(coda_oss::span<const T> buffer)
{
write(buffer.data(), buffer.size_bytes());
}
template <typename T>
void write(coda_oss::span<T> buffer)
{
write(coda_oss::span<const T>(buffer.data(), buffer.size()));
}

/*!
* Flush the stream if needed
Expand Down
9 changes: 6 additions & 3 deletions externals/coda-oss/modules/c++/io/include/io/ReadUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
*
*/

#ifndef __IO_READ_UTILS_H__
#define __IO_READ_UTILS_H__
#ifndef CODA_OSS_io_ReadUtils_h_INCLUDED_
#define CODA_OSS_io_ReadUtils_h_INCLUDED_

#include <string>
#include <vector>

#include <sys/Conf.h>
#include <coda_oss/cstddef.h> // byte
#include <sys/filesystem.h>

namespace io
{
Expand All @@ -40,6 +42,7 @@ namespace io
*/
void readFileContents(const std::string& pathname,
std::vector<sys::byte>& buffer);
void readFileContents(const sys::filesystem::path& pathname, std::vector<coda_oss::byte>& buffer);

/*!
* Reads the contents of a file into a string. The file is assumed to be a
Expand Down Expand Up @@ -67,4 +70,4 @@ std::string readFileContents(const std::string& pathname)
}
}

#endif
#endif // CODA_OSS_io_ReadUtils_h_INCLUDED_
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ struct StringStreamT final : public SeekableBidirectionalStream
{
const auto maxSize = available();
if (maxSize <= 0)
return io::InputStream::IS_END;
return ::io::InputStream::IS_END;

auto len = gsl::narrow<sys::Off_T>(len_);
if (maxSize < len)
Expand Down
16 changes: 13 additions & 3 deletions externals/coda-oss/modules/c++/io/source/ReadUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@
*/

#include <io/FileInputStream.h>
#include <coda_oss/span.h>

namespace io
{
void readFileContents(const std::string& pathname,
std::vector<sys::byte>& buffer)
template<typename TPath, typename T>
void readFileContents_(const TPath& pathname, std::vector<T>& buffer)
{
io::FileInputStream inStream(pathname);
buffer.resize(inStream.available());
if (!buffer.empty())
{
inStream.read(&buffer[0], buffer.size(), true);
inStream.read(coda_oss::span<T>(buffer.data(), buffer.size()), true);
}
}
void readFileContents(const std::string& pathname,
std::vector<sys::byte>& buffer)
{
readFileContents_(pathname, buffer);
}
void readFileContents(const sys::filesystem::path& pathname, std::vector<coda_oss::byte>& buffer)
{
readFileContents_(pathname, buffer);
}

void readFileContents(const std::string& pathname, std::string& str)
{
Expand Down
37 changes: 28 additions & 9 deletions externals/coda-oss/modules/c++/io/unittests/test_streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*
*/

#include <std/span>
#include <std/cstddef>

#include <import/io.h>
#include <mem/BufferView.h>
#include <sys/Conf.h>
Expand Down Expand Up @@ -82,15 +85,31 @@ TEST_CASE(testByteStream)
stream.write("abcdef");
TEST_ASSERT_EQ(stream.getSize(), static_cast<size_t>(24));

stream.clear();
TEST_ASSERT_EQ(stream.available(), 0);
stream.write("test");
stream.seek(0, io::Seekable::START);
TEST_ASSERT_EQ(stream.available(), 4);
sys::byte buf[255];
stream.read(buf, 4);
buf[4] = 0;
TEST_ASSERT_EQ(std::string(buf), "test");
const std::string test("test");
{
stream.clear();
TEST_ASSERT_EQ(stream.available(), 0);
stream.write(test);
stream.seek(0, io::Seekable::START);
TEST_ASSERT_EQ(stream.available(), 4);
sys::byte buf[255];
stream.read(buf, 4);
buf[4] = 0;
TEST_ASSERT_EQ(std::string(buf), test);
}
{
stream.clear();
const std::span<const std::string::value_type> test_span(test.data(), test.size());
stream.write(test_span);
stream.seek(0, io::Seekable::START);
TEST_ASSERT_EQ(stream.available(), 4);
std::byte buf[255];
stream.read(std::span<std::byte>(buf, 4));
buf[4] = std::byte(0);
const void* pBuf = buf;
auto pStrBuf = static_cast<std::string::const_pointer>(pBuf);
TEST_ASSERT_EQ(pStrBuf, test);
}
}

TEST_CASE(testProxyOutputStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ OneD<_T>
TwoD<_T>::atY(double y) const
{
OneD<_T> ret(0);
if (orderX() > 0)
if (!empty())
{
// We have no more X, but we have Y still
ret = OneD<_T>(orderX());
Expand Down Expand Up @@ -153,6 +153,11 @@ template<typename _T>
TwoD<_T>
TwoD<_T>::flipXY() const
{
if (empty())
{
return TwoD<_T>();
}

size_t oY = orderX();
size_t oX = orderY();
TwoD<_T> prime(oX, oY);
Expand Down
Loading

0 comments on commit 543bf38

Please sign in to comment.