diff options
23 files changed, 0 insertions, 2438 deletions
diff --git a/fastlib/src/vespa/fastlib/io/CMakeLists.txt b/fastlib/src/vespa/fastlib/io/CMakeLists.txt index 8c45940e4c1..d5dd72dbb9c 100644 --- a/fastlib/src/vespa/fastlib/io/CMakeLists.txt +++ b/fastlib/src/vespa/fastlib/io/CMakeLists.txt @@ -1,11 +1,7 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_library(fastlib_io SOURCES - fileinputstream.cpp - fileoutputstream.cpp bufferedfile.cpp - bufferedinputstream.cpp - bufferedoutputstream.cpp INSTALL lib64 DEPENDS ) diff --git a/fastlib/src/vespa/fastlib/io/bufferedinputstream.cpp b/fastlib/src/vespa/fastlib/io/bufferedinputstream.cpp deleted file mode 100644 index f536a88913e..00000000000 --- a/fastlib/src/vespa/fastlib/io/bufferedinputstream.cpp +++ /dev/null @@ -1,247 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include "bufferedinputstream.h" -#include <cstring> -#include <cstdint> - -Fast_BufferedInputStream::Fast_BufferedInputStream(Fast_InputStream &in, size_t bufferSize) - : Fast_FilterInputStream(in), - _buffer(new char[bufferSize]), - _bufferSize((_buffer != nullptr) ? bufferSize : 0), - _bufferUsed(0), - _bufferRead(0), - _nextWillFail(false) -{ -} - -Fast_BufferedInputStream::~Fast_BufferedInputStream() -{ - delete [] _buffer; -} - -ssize_t -Fast_BufferedInputStream::Available() -{ - return _in->Available() + _bufferUsed - _bufferRead; -} - -bool -Fast_BufferedInputStream::Close() -{ - return _in->Close(); -} - -ssize_t -Fast_BufferedInputStream::Skip(size_t skipNBytes) -{ - ssize_t numBytesSkipped = 0; - - if (_nextWillFail) { - _nextWillFail = false; - return -1; - } - - if (skipNBytes > _bufferUsed - _bufferRead) { - // First, skip all bytes in buffer - numBytesSkipped = _bufferUsed - _bufferRead; - _bufferUsed = _bufferRead = 0; - - // Skip rest of bytes in slave stream - ssize_t slaveSkipped = _in->Skip(skipNBytes - numBytesSkipped); - if (slaveSkipped < 0) { - if (numBytesSkipped > 0) { - _nextWillFail = true; - } else { - numBytesSkipped = slaveSkipped; - } - } else { - numBytesSkipped += slaveSkipped; - } - - } else { - // Skip all skipNBytes in buffer - _bufferRead += skipNBytes; - if (_bufferRead == _bufferUsed) { - _bufferUsed = _bufferRead = 0; - } - numBytesSkipped = skipNBytes; - } - - return numBytesSkipped; -} - -ssize_t -Fast_BufferedInputStream::Read(void *targetBuffer, size_t length) -{ - - // This function will under no circumstance read more than once from - // its slave stream, in order to prevent blocking on input. - - if (_nextWillFail) { - _nextWillFail = false; - return -1; - } - - ssize_t numBytesRead = 0; - char* to = static_cast<char*>(targetBuffer); - size_t bufferRemain = _bufferUsed - _bufferRead; - - if (length <= bufferRemain) { - memcpy(to, &_buffer[_bufferRead], length); - numBytesRead += length; - _bufferRead += length; - if (_bufferRead == _bufferUsed) { - _bufferRead = _bufferUsed = 0; - } - } else { - // Use the data currently in the buffer, then read from slave stream. - - if (bufferRemain > 0) { - memcpy(to, &_buffer[_bufferRead], bufferRemain); - numBytesRead += bufferRemain; - length -= bufferRemain; - to += bufferRemain; - } - _bufferUsed = 0; - _bufferRead = 0; - ssize_t slaveRead; - - // If remaining data to be read can fit in the buffer, put it - // there, otherwise read directly to receiver and empty the buffer. - if (length < _bufferSize) { - slaveRead = Fast_FilterInputStream::Read(_buffer, _bufferSize); - } else { - slaveRead = Fast_FilterInputStream::Read(to, length); - } - - if (slaveRead > 0) { - if (length < _bufferSize) { - // We read to buffer, so copy from buffer to receiver. - if (length < static_cast<size_t>(slaveRead)) { - memcpy(to, _buffer, length); - numBytesRead += length; - _bufferUsed = slaveRead; - _bufferRead = length; - } else { - memcpy(to, _buffer, slaveRead); - numBytesRead += slaveRead; - } - } else { - // We read directly to receiver, no need to copy. - numBytesRead += slaveRead; - } - } else if (slaveRead == 0) { - // Do nothing - } else { - // slaveRead < 0, so an error occurred while reading from the - // slave. If there was data in the buffer, report success and - // fail on next operation instead. - if (numBytesRead > 0) { - _nextWillFail = true; - } else { - numBytesRead = slaveRead; - } - } - - } // End of slave read - - return numBytesRead; -} - -ssize_t -Fast_BufferedInputStream::ReadBufferFullUntil(void *targetBuffer, size_t maxlength, char stopChar) -{ - - if (maxlength > _bufferSize) - maxlength = _bufferSize; - - // This function will under no circumstance read more than once from - // its slave stream, in order to prevent blocking on input. - - if (_nextWillFail) { - _nextWillFail = false; - return -1; - } - - uint32_t offset = 0; - ssize_t numBytesRead = 0; - char* to = static_cast<char*>(targetBuffer); - size_t bufferRemain = _bufferUsed - _bufferRead; - - // Check if we should scan for stopChar in buffer - if (bufferRemain > 0) { - for (offset = _bufferRead; offset < _bufferUsed; offset++) { - if(_buffer[offset] == stopChar) { - break; - } - } - // Found character in buffer - if (offset < _bufferUsed) { - maxlength = offset - _bufferRead + 1; - } - } - - if (maxlength <= bufferRemain) { - memcpy(to, &_buffer[_bufferRead], maxlength); - numBytesRead += maxlength; - _bufferRead += maxlength; - if (_bufferRead == _bufferUsed) { - _bufferRead = _bufferUsed = 0; - } - } else { - // Use the data currently in the buffer, then read from slave stream. - - if (bufferRemain > 0) { - memcpy(to, &_buffer[_bufferRead], bufferRemain); - numBytesRead += bufferRemain; - maxlength -= bufferRemain; - to += bufferRemain; - } - _bufferUsed = 0; - _bufferRead = 0; - ssize_t slaveRead; - - slaveRead = Fast_FilterInputStream::Read(_buffer, _bufferSize); - if (slaveRead > 0) { - for (offset = 0; offset < static_cast<uint32_t>(slaveRead); offset++) { - if(_buffer[offset] == stopChar) { - break; - } - } - - if (offset >= maxlength) { - // Discard data if character was not present - numBytesRead = -1; - } else { - // Found character in buffer - if (offset < static_cast<uint32_t>(slaveRead)) { - maxlength = offset + 1; - } - // We read to buffer, so copy from buffer to receiver. - if (maxlength < static_cast<size_t>(slaveRead)) { - memcpy(to, _buffer, maxlength); - numBytesRead += maxlength; - _bufferUsed = slaveRead; - _bufferRead = maxlength; - } else { - memcpy(to, _buffer, slaveRead); - numBytesRead += slaveRead; - } - } - } else if (slaveRead == 0) { - // Do nothing - } else { - // slaveRead < 0, so an error occurred while reading from the - // slave. If there was data in the buffer, report success and - // fail on next operation instead. - if (numBytesRead > 0) { - _nextWillFail = true; - } else { - numBytesRead = slaveRead; - } - } - } // End of slave read - - return numBytesRead; - -} diff --git a/fastlib/src/vespa/fastlib/io/bufferedinputstream.h b/fastlib/src/vespa/fastlib/io/bufferedinputstream.h deleted file mode 100644 index e52fe625b85..00000000000 --- a/fastlib/src/vespa/fastlib/io/bufferedinputstream.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#pragma once - -#include <vespa/fastlib/io/filterinputstream.h> - -class Fast_BufferedInputStream : public Fast_FilterInputStream -{ -protected: - // Buffer attributes - char *_buffer; - const size_t _bufferSize; - size_t _bufferUsed; // Amount of buffer currently holding data - size_t _bufferRead; // How far buffer has been read - bool _nextWillFail; - - -public: - Fast_BufferedInputStream(const Fast_BufferedInputStream &) = delete; - Fast_BufferedInputStream & operator = (const Fast_BufferedInputStream &) = delete; - - // Constructor - Fast_BufferedInputStream(Fast_InputStream &in, size_t bufferSize = 1024); - - // Destructor - virtual ~Fast_BufferedInputStream(); - - // Subclassed methods - ssize_t Available() override; - bool Close() override; - ssize_t Skip(size_t skipNBytes) override; - ssize_t Read(void *targetBuffer, size_t length) override; - - // Additional methods - ssize_t ReadBufferFullUntil(void *targetBuffer, size_t maxlength, char stopChar); -}; diff --git a/fastlib/src/vespa/fastlib/io/bufferedoutputstream.cpp b/fastlib/src/vespa/fastlib/io/bufferedoutputstream.cpp deleted file mode 100644 index 432e7200e66..00000000000 --- a/fastlib/src/vespa/fastlib/io/bufferedoutputstream.cpp +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/* - * Generic buffered output stream - * - * Author: Markus Bjartveit Krüger - */ - -#include "bufferedoutputstream.h" -#include <cstring> - -Fast_BufferedOutputStream::Fast_BufferedOutputStream(Fast_OutputStream &out, - size_t bufferSize) - : Fast_FilterOutputStream(out), - _buffer(new char[bufferSize]), - _bufferSize((_buffer != NULL) ? bufferSize : 0), - _bufferUsed(0), - _bufferWritten(0), - _nextWillFail(false) -{ -} - - - -Fast_BufferedOutputStream::~Fast_BufferedOutputStream(void) -{ - delete [] _buffer; -} - - - -bool Fast_BufferedOutputStream::Close(void) -{ - Flush(); - return Fast_FilterOutputStream::Close(); -} - - - -void Fast_BufferedOutputStream::Flush(void) -{ - if (_nextWillFail) - { - _nextWillFail = false; - return; - } - while (_bufferWritten < _bufferUsed) - { - ssize_t slaveWritten; - slaveWritten = Fast_FilterOutputStream::Write(&_buffer[_bufferWritten], _bufferUsed - _bufferWritten); - if (slaveWritten >= 0) - { - _bufferWritten += slaveWritten; - } - else - { - break; - } - } - _bufferUsed = 0; - _bufferWritten = 0; - - Fast_FilterOutputStream::Flush(); -} - - - -ssize_t Fast_BufferedOutputStream::Write(const void *sourceBuffer, size_t length) -{ - - // This function will under no circumstance write more than once to - // its slave stream, in order to prevent blocking on output. - - if (_nextWillFail) - { - _nextWillFail = false; - return -1; - } - ssize_t numBytesWritten = 0; - const char* from = static_cast<const char *>(sourceBuffer); - size_t bufferRemain = _bufferUsed - _bufferWritten; - - if (length <= _bufferSize - _bufferUsed) - { - memcpy(&_buffer[_bufferUsed], from, length); - numBytesWritten += length; - _bufferUsed += length; - } - else if (length <= _bufferSize - bufferRemain) - { - memmove(_buffer, &_buffer[_bufferWritten], bufferRemain); - memcpy(&_buffer[bufferRemain], from, length); - _bufferUsed = bufferRemain + length; - _bufferWritten = 0; - } - else - { - ssize_t slaveWritten; - bool writeFromBuffer = bufferRemain > 0; - - if (writeFromBuffer) - { - // Fill up buffer before write. - memcpy(&_buffer[_bufferUsed], from, _bufferSize - _bufferUsed); - from += _bufferSize - _bufferUsed; - length -= _bufferSize - _bufferUsed; - numBytesWritten += _bufferSize - _bufferUsed; - - slaveWritten = Fast_FilterOutputStream::Write(_buffer, _bufferSize); - } - else - { - slaveWritten = Fast_FilterOutputStream::Write(from, length); - } - - if (slaveWritten >= 0) - { - if (writeFromBuffer) - { - // We wrote from buffer, so shuffle remainder of buffer before - // filling it up. - memmove(_buffer, &_buffer[slaveWritten], _bufferSize - slaveWritten); - _bufferUsed = _bufferSize - slaveWritten; - } - else - { - // Buffer was empty, all data written from sender. - numBytesWritten += slaveWritten; - from += slaveWritten; - length -= slaveWritten; - _bufferUsed = 0; - } - size_t freeBuffer = _bufferSize - _bufferUsed; - size_t refill = (length < freeBuffer) ? length : freeBuffer; - memcpy(&_buffer[_bufferUsed], from, refill); - numBytesWritten += refill; - _bufferUsed += refill; - _bufferWritten = 0; - } - else - { - // slaveWritten < 0, so an error occurred while writing to the - // slave. If there was data in the buffer, report success and - // fail on next operation instead. - if (numBytesWritten > 0) - { - _nextWillFail = true; - } - else - { - numBytesWritten = slaveWritten; - } - } - - } // End of slave write - - return numBytesWritten; -} diff --git a/fastlib/src/vespa/fastlib/io/bufferedoutputstream.h b/fastlib/src/vespa/fastlib/io/bufferedoutputstream.h deleted file mode 100644 index 86cd6a41a25..00000000000 --- a/fastlib/src/vespa/fastlib/io/bufferedoutputstream.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include <vespa/fastlib/io/filteroutputstream.h> - -class Fast_BufferedOutputStream : public Fast_FilterOutputStream -{ -private: - - // Prevent use of: - Fast_BufferedOutputStream(const Fast_BufferedOutputStream &); - Fast_BufferedOutputStream & operator=(const Fast_BufferedOutputStream &); - - -protected: - - // Buffer attributes - char *_buffer; - size_t _bufferSize; - size_t _bufferUsed; // Amount of buffer currently holding data - size_t _bufferWritten; // How far buffer has been written - bool _nextWillFail; - - -public: - - // Constructor - Fast_BufferedOutputStream(Fast_OutputStream &out, size_t bufferSize = 1024); - - // Destructor - ~Fast_BufferedOutputStream(); - - // Methods - bool Close() override; - ssize_t Write(const void *sourceBuffer, size_t length) override; - void Flush() override; -}; diff --git a/fastlib/src/vespa/fastlib/io/fileinputstream.cpp b/fastlib/src/vespa/fastlib/io/fileinputstream.cpp deleted file mode 100644 index ddb6f12d467..00000000000 --- a/fastlib/src/vespa/fastlib/io/fileinputstream.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/* - * Author: Stein Hardy Danielsen - */ - -#include "fileinputstream.h" - -Fast_FileInputStream::Fast_FileInputStream(const char *fileName) - : _theFile(new FastOS_File(fileName)), - _fileOpenedOk(false) -{ - _fileOpenedOk = _theFile->OpenReadOnly(); -} - -Fast_FileInputStream::~Fast_FileInputStream() -{ - Close(); - delete _theFile; -} - -ssize_t -Fast_FileInputStream::Read(void *targetBuffer, size_t bufferSize) -{ - ssize_t retVal = -1; - if (_fileOpenedOk) { - retVal = _theFile->Read(targetBuffer, bufferSize); - } - return retVal; -}; - -bool -Fast_FileInputStream::Close() { - return _theFile->Close(); -} - -ssize_t -Fast_FileInputStream::Available() { - return 0; -} - -ssize_t -Fast_FileInputStream::Skip(size_t) { - return 0; -} diff --git a/fastlib/src/vespa/fastlib/io/fileinputstream.h b/fastlib/src/vespa/fastlib/io/fileinputstream.h deleted file mode 100644 index e3cd65ab321..00000000000 --- a/fastlib/src/vespa/fastlib/io/fileinputstream.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include "inputstream.h" -#include <vespa/fastos/file.h> - -class Fast_FileInputStream : public Fast_InputStream -{ -private: - Fast_FileInputStream(const Fast_FileInputStream&); - Fast_FileInputStream& operator=(const Fast_FileInputStream&); - -protected: - - /** Pointer to the physical file object*/ - FastOS_FileInterface *_theFile; - - /** File opened ok flag */ - bool _fileOpenedOk; - -public: - Fast_FileInputStream(const char *fileName); - ~Fast_FileInputStream(); - - // Implementation of Fast_InputStream interface - - ssize_t Read(void *targetBuffer, size_t bufferSize) override; - bool Close() override; - ssize_t Available() override; - ssize_t Skip(size_t) override; -}; diff --git a/fastlib/src/vespa/fastlib/io/fileoutputstream.cpp b/fastlib/src/vespa/fastlib/io/fileoutputstream.cpp deleted file mode 100644 index df969434d60..00000000000 --- a/fastlib/src/vespa/fastlib/io/fileoutputstream.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/* - * Author: Stein Hardy Danielsen - */ - -#include "fileoutputstream.h" -#include <vespa/fastos/file.h> - - -Fast_FileOutputStream::Fast_FileOutputStream(const char *fileName) - : _theFile(new FastOS_File(fileName)) -{ - _theFile->OpenWriteOnly(); -} - - -Fast_FileOutputStream::~Fast_FileOutputStream(void) -{ - _theFile->Close(); - delete _theFile; -} - -ssize_t -Fast_FileOutputStream::Write(const void *sourceBuffer, size_t bufferSize) { - return _theFile->CheckedWrite(sourceBuffer, bufferSize) ? - static_cast<ssize_t>(bufferSize) : - static_cast<ssize_t>(-1); -}; - -bool -Fast_FileOutputStream::Close() { - return _theFile->Close(); -} diff --git a/fastlib/src/vespa/fastlib/io/fileoutputstream.h b/fastlib/src/vespa/fastlib/io/fileoutputstream.h deleted file mode 100644 index 39de14cc6d1..00000000000 --- a/fastlib/src/vespa/fastlib/io/fileoutputstream.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include "outputstream.h" - -class FastOS_FileInterface; - -class Fast_FileOutputStream : public Fast_OutputStream -{ -private: - Fast_FileOutputStream(const Fast_FileOutputStream&); - Fast_FileOutputStream& operator=(const Fast_FileOutputStream&); - -protected: - - /** Pointer to the physical file object*/ - FastOS_FileInterface *_theFile; - -public: - Fast_FileOutputStream(const char *fileName); - ~Fast_FileOutputStream(); - - ssize_t Write(const void *sourceBuffer, size_t bufferSize) override; - - bool Close() override; - void Flush() override {} -}; diff --git a/fastlib/src/vespa/fastlib/io/filterinputstream.h b/fastlib/src/vespa/fastlib/io/filterinputstream.h deleted file mode 100644 index 41fbda89bde..00000000000 --- a/fastlib/src/vespa/fastlib/io/filterinputstream.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/* - * Generic filter input stream - * - * Author: Stein Hardy Danielsen - */ -#pragma once - -#include "inputstream.h" - -class Fast_FilterInputStream : public Fast_InputStream -{ -private: - - // Prevent use of: - Fast_FilterInputStream(void); - Fast_FilterInputStream(Fast_FilterInputStream &); - Fast_FilterInputStream &operator=(const Fast_FilterInputStream &); - - -protected: - - /** The stream to forward data to */ - Fast_InputStream *_in; - - -public: - - // Constructors - Fast_FilterInputStream(Fast_InputStream &in) : _in(&in) {} - - ~Fast_FilterInputStream() {}; - - ssize_t Available() override { return _in->Available(); } - bool Close() override { return _in->Close(); } - ssize_t Skip(size_t skipNBytes) override { return _in->Skip(skipNBytes); } - - ssize_t Read(void *targetBuffer, size_t length) override { - return _in->Read(targetBuffer, length); - } -}; diff --git a/fastlib/src/vespa/fastlib/io/filteroutputstream.h b/fastlib/src/vespa/fastlib/io/filteroutputstream.h deleted file mode 100644 index f65aa70c851..00000000000 --- a/fastlib/src/vespa/fastlib/io/filteroutputstream.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#pragma once - -#include "outputstream.h" - -class Fast_FilterOutputStream : public Fast_OutputStream -{ -private: - - // Prevent use of: - Fast_FilterOutputStream(); - Fast_FilterOutputStream(Fast_FilterOutputStream &); - Fast_FilterOutputStream &operator=(const Fast_FilterOutputStream &); - -protected: - /** The stream to forward data to */ - Fast_OutputStream *_out; -public: - Fast_FilterOutputStream(Fast_OutputStream &out) : _out(&out) {} - ~Fast_FilterOutputStream() {} - - bool Close() override { return _out->Close(); } - void Flush() override { _out->Flush(); } - - ssize_t Write(const void *sourceBuffer, size_t length) override { - return _out->Write(sourceBuffer, length); - } -}; diff --git a/fastlib/src/vespa/fastlib/io/inputstream.h b/fastlib/src/vespa/fastlib/io/inputstream.h deleted file mode 100644 index 9b45d62f923..00000000000 --- a/fastlib/src/vespa/fastlib/io/inputstream.h +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#pragma once - -#include <sys/types.h> - -class Fast_InputStream -{ -public: - virtual ~Fast_InputStream() { } - - virtual ssize_t Available() = 0; - virtual bool Close() = 0; - virtual ssize_t Read(void *targetBuffer, size_t bufferSize) = 0; - virtual ssize_t Skip(size_t skipNBytes) = 0; -}; diff --git a/fastlib/src/vespa/fastlib/io/outputstream.h b/fastlib/src/vespa/fastlib/io/outputstream.h deleted file mode 100644 index a783d8786a7..00000000000 --- a/fastlib/src/vespa/fastlib/io/outputstream.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/* - * Generic output stream interface - * - * Author Stein Hardy Danielsen - */ -#pragma once - -#include <cstdlib> - -class Fast_OutputStream -{ -public: - - virtual ~Fast_OutputStream() { } - - virtual bool Close() = 0; - virtual void Flush() = 0; - virtual ssize_t Write(const void *sourceBuffer, size_t bufferSize) = 0; -}; diff --git a/fastlib/src/vespa/fastlib/io/tests/CMakeLists.txt b/fastlib/src/vespa/fastlib/io/tests/CMakeLists.txt index 9b0daa49cba..803892d035e 100644 --- a/fastlib/src/vespa/fastlib/io/tests/CMakeLists.txt +++ b/fastlib/src/vespa/fastlib/io/tests/CMakeLists.txt @@ -1,11 +1,4 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(fastlib_bufferedstreamtest_app TEST - SOURCES - bufferedstreamtest.cpp - DEPENDS - fastlib_io -) -vespa_add_test(NAME fastlib_bufferedstreamtest_app COMMAND fastlib_bufferedstreamtest_app fastlib_bufferedstreamtest_app) vespa_add_executable(fastlib_bufferedfiletest_app TEST SOURCES bufferedfiletest.cpp diff --git a/fastlib/src/vespa/fastlib/io/tests/bufferedstreamtest.cpp b/fastlib/src/vespa/fastlib/io/tests/bufferedstreamtest.cpp deleted file mode 100644 index f9957d96288..00000000000 --- a/fastlib/src/vespa/fastlib/io/tests/bufferedstreamtest.cpp +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <cassert> -#include <ctime> -#include <iostream> -#include <cstring> - -using namespace std; - -#include <vespa/fastlib/io/fileinputstream.h> -#include <vespa/fastlib/io/fileoutputstream.h> -#include <vespa/fastlib/io/bufferedinputstream.h> -#include <vespa/fastlib/io/bufferedoutputstream.h> - -static bool ReadFile(const char *tag, Fast_InputStream &input, - char *buffer, int64_t fileSize, size_t chunkSize) -{ - char *pos; - clock_t start, ticks; - - cout << "Starting to read file (" << tag << ")..." << endl; - start = clock(); - - pos = buffer; - // Slurp file - ssize_t numRead; - while (pos - buffer < fileSize && - (numRead = input.Read(pos, chunkSize)) > 0) - pos += numRead; - if (pos - buffer != fileSize) - { - cerr << "Read " << pos - buffer << " bytes, " - << " expected " << static_cast<int>(fileSize) << " bytes.\n"; - return false; - } - - ticks = clock() - start; - cout << "Done, used " << ticks << " ticks " - << "(" << static_cast<double>(ticks) / CLOCKS_PER_SEC << " seconds)\n\n" - << ends; - - return true; -} - -static bool WriteAndReadBackFile(const char *tag, Fast_OutputStream &output, - char *buffer, int64_t fileSize, - const char *fileName, size_t chunkSize) -{ - char *pos; - clock_t start, ticks; - - cout << "Starting to write file (" << tag << ")..." << endl; - start = clock(); - - pos = buffer; - // Dump file - ssize_t numWritten; - size_t numToWrite = (fileSize < static_cast<int64_t>(chunkSize)) ? - static_cast<size_t>(fileSize) : chunkSize; - while (pos - buffer < fileSize && - (numWritten = output.Write(pos, numToWrite)) > 0) - { - pos += numWritten; - if ((fileSize - (pos - buffer)) < static_cast<int64_t>(chunkSize)) { - numToWrite = (fileSize - (pos - buffer)); - } - else { - numToWrite = chunkSize; - } - } - output.Flush(); - - if (pos - buffer != fileSize) - { - cerr << "Wrote " << pos - buffer << " bytes, " \ - << " expected " << static_cast<int>(fileSize) << " bytes.\n"; - return false; - } - - ticks = clock() - start; - cout << "Done, used " << ticks << " ticks " - << "(" << static_cast<double>(ticks) / CLOCKS_PER_SEC << " seconds)\n\n" - << ends; - - output.Close(); - - FastOS_File readBackFile; - readBackFile.OpenReadOnly(fileName); - if (readBackFile.Read(buffer, fileSize) != fileSize) - { - cerr << "Error reading data back from " << fileName << ".\n"; - return false; - } - - return true; -} - -int main(int argc, char **argv) -{ - if (argc < 2 || argc > 4) - { - cerr << "Usage: " << argv[0] << " <file> [<buffer size> [<chunk size>]]\n"; - return 1; - } - - const char *fileName = argv[1]; - unsigned long bufferSize = (argc >= 3) ? strtoul(argv[2], NULL, 10) : 1024; - unsigned long chunkSize = (argc >= 4) ? strtoul(argv[3], NULL, 10) : 1; - - FastOS_StatInfo statInfo; - if (!FastOS_File::Stat(fileName, &statInfo)) - { - cerr << "Failed to stat " << fileName << "\n"; - return 1; - } - - int64_t fileSize = statInfo._size; - - char *unbufferedData = new char[fileSize]; - assert(unbufferedData != NULL); - - char *bufferedData = new char[fileSize]; - assert(bufferedData != NULL); - - ///////////////////////////////////////////////////////////////////// - // Start of input test - - Fast_FileInputStream unbufferedInputFile(fileName); - // run unneeded functions for coverage - unbufferedInputFile.Skip(unbufferedInputFile.Available()); - - if (!ReadFile("unbuffered", unbufferedInputFile, unbufferedData, fileSize, chunkSize)) - { - cerr << "Unbuffered read failed" << endl; - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - Fast_FileInputStream slaveInputFile(fileName); - Fast_BufferedInputStream bufferedInputFile(slaveInputFile, bufferSize); - - if (!ReadFile("buffered", bufferedInputFile, bufferedData, fileSize, chunkSize)) - { - cerr << "Buffered read failed" << endl; - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - if (memcmp(unbufferedData, bufferedData, fileSize) == 0) - { - cout << "Buffered and unbuffered data equal -- success!\n"; - } - else - { - cout << "Buffered and unbuffered data differs -- error!\n"; - cout << "Contents of unbuffered data:\n"; - cout.write(unbufferedData, fileSize); - cout << "Contents of buffered data:\n"; - cout.write(bufferedData, fileSize); - - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - ///////////////////////////////////////////////////////////////////// - // Start of output test - - const char *tempFile = "bufferedstreamtest.tmp"; - - Fast_FileOutputStream unbufferedOutputFile(tempFile); - - if (!WriteAndReadBackFile("unbuffered", unbufferedOutputFile, - unbufferedData, fileSize, tempFile, chunkSize)) - { - cerr << "Unbuffered write and read back failed" << endl; - - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - Fast_FileOutputStream slaveOutputFile(tempFile); - Fast_BufferedOutputStream bufferedOutputFile(slaveOutputFile, bufferSize); - - if (!WriteAndReadBackFile("buffered", bufferedOutputFile, - bufferedData, fileSize, tempFile, chunkSize)) - { - cerr << "Buffered write and read back failed" << endl; - - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - if (memcmp(unbufferedData, bufferedData, fileSize) == 0) - { - cout << "Buffered and unbuffered data equal -- success!\n"; - } - else - { - cout << "Buffered and unbuffered data differs -- error!\n"; - cout << "Contents of unbuffered data:\n"; - cout.write(unbufferedData, fileSize); - cout << "Contents of buffered data:\n"; - cout.write(bufferedData, fileSize); - - delete [] unbufferedData; - delete [] bufferedData; - return 1; - } - - delete [] unbufferedData; - delete [] bufferedData; - return 0; -} diff --git a/fastlib/src/vespa/fastlib/util/CMakeLists.txt b/fastlib/src/vespa/fastlib/util/CMakeLists.txt index 28a4f8b3c0e..2cd7b7d7e64 100644 --- a/fastlib/src/vespa/fastlib/util/CMakeLists.txt +++ b/fastlib/src/vespa/fastlib/util/CMakeLists.txt @@ -1,7 +1,6 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_library(fastlib_util SOURCES - base64.cpp INSTALL lib64 DEPENDS ) diff --git a/fastlib/src/vespa/fastlib/util/bag.h b/fastlib/src/vespa/fastlib/util/bag.h deleted file mode 100644 index 816208fe440..00000000000 --- a/fastlib/src/vespa/fastlib/util/bag.h +++ /dev/null @@ -1,571 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -//************************************************************************** -/** - * @author Bård Kvalheim - */ -/* - * @date Creation date: 2000-03-22 - **************************************************************************/ - -#pragma once - -template<class Type> -class Fast_BagIterator; - -/** - * A Fast_Bag is a collection which can contain duplicates. The - * only way to access the elements in a bag is via the - * Fast_BagIterator. The Fast_BagIterator is also capable of - * removing the current element. - * - * The Fast_BagIterator is used to iterate over the elements in - * the bag. It can be initiated by a pointer or a reference to a - * bag. The recomended use is to init the Fast_BagIterator of the - * stack and not on the heap - to avoid new and potential memory leaks. - * - * The Fast_BagIterator can be new'ed with the empty constructor - * or initialized with a pointer or a reference to a bag. It can be - * rewind with the Start() method accepting a bag pointer or reference. - * - * The internal array are resized with the private Grow() method. - * Choose the block size carefully to avoid overhead, since the Grow - * method is costly: If an array grows or are copied the elements in - * the array are copied with the = operator. In other words, the copy - * constructor of the objects are used. - * - * The amount the bag size are double each time the bag grows. The next - * grow-amount can be set with the void SetBlocksize(int bs) - * method. - * - * Example of use: - * <pre> - * // Init. 10 is start size. It grows as needed. - * Fast_Bag<int> bag(10); - * - * // Insert 4 elements - * bag.Insert(1); - * bag.Insert(2); - * bag.Insert(3); - * bag.Insert(4); - * - * // Iterate through and delete element equal to 2. - * for(Fast_BagIterator<int> iter(bag); - * !iter.End(); - * iter.Next()) { - * if(iter.GetCurrent() == 2) { - * iter.RemoveCurrent(); - * } - * } - * </pre> - */ - -template <class Type> -class Fast_Bag -{ - friend class Fast_BagIterator<Type>; - -protected: - /** - * The current capacity of the bag - */ - int _capacity; - - /** - * The actual array - */ - Type* _array; - - /** - * The block size used when the bag is growing - */ - int _blocksize; - - /** - * The number of elements in the bag - */ - int _numElements; - -private: - /** - * Increases the bag size with _blocksize - */ - void Grow(); - -public: - - typedef Type value_type; - - /** - * Default constructor. Sets the capacity to 0 and the - * _blocksize to 1. Very inefficient. Should only be used for - * testing - */ - Fast_Bag(); - - /** - * Copy constructor. Uses the element's = operator to make the - * copy. Hence, a deep copy are only done when actual objects are - * stored in the array and they implement a proper copy constructor. - * @param source the orginal array to copy from. - */ - Fast_Bag(const Fast_Bag<Type>& source); - - /** - * Constructor setting the _capacity . The _blocksize is - * set to capacity. - * @param capacity the initial capacity of the bag - */ - Fast_Bag(const int capacity); - - /** - * Constructor setting the _capacity and the _blocksize - * @param capacity the initial _capacity of the bag - * @param blocksize the initial _blocksize of the bag */ - Fast_Bag(const int capacity, const int blocksize); - - ~Fast_Bag(); - - /** - * Assignment operator. - * @param other the orginal array to assign from - * @return reference to the newly assigned bag - */ - Fast_Bag& operator=(const Fast_Bag<Type>& other); - - /** - * Comparison operator - * @param other the right side of == - * @return true/false - */ - bool operator==(const Fast_Bag<Type>& other) const; - - - /** - * The _blocksize is the size the bag grows with when it grows - * @return the _blocksize - */ - inline int GetBlocksize() const { return _blocksize; } - - /** - * Set a new _blocksize - * @param blocksize the new _blocksize - */ - void SetBlocksize(const int blocksize); - - /** - * @return the number of elements in the bag - */ - inline int NumberOfElements() const { return _numElements; } - - /** - * Adds a new element to the bag - * @param element the element to add - */ - void Insert(const Type element); - - /** - * Removes all the elements in the bag - */ - void RemoveAllElements(); - - /** - * Removes a element for the bag. - * @param element the element to be removed. - */ - void RemoveElement(const Type element); - - /** - * Returns true iff the element is in the bag - * @param element the element to look up. - */ - bool HasElement(const Type element); -}; - -//************************************************************************** -/** - * A Fast_BagIterator. The Fast_BagIterator can delete the - * current element from the Fast_Bag. Ensures that all elements - * are visited before the end is reached. The Fast_BagIterator can - * reused with calls to Start(Fast_Bag<Type>*) or - * Start(Fast_Bag<Type>&) - * - * Example of use: - * <pre> - * // Init. 10 is start size. It grows as needed. - * Fast_Bag<int> bag(10); - * - * // Insert 4 elements - * bag.Insert(1); - * bag.Insert(2); - * bag.Insert(3); - * bag.Insert(4); - * - * // Iterate through and delete element equal to 2. - * for(Fast_BagIterator<int> iter(bag); - * !iter.End(); - * iter.Next()) { - * if(iter.GetCurrent() == 2) { - * iter.RemoveCurrent(); - * } - * } - * </pre> - */ - -template <class Type> -class Fast_BagIterator -{ - friend class Fast_Bag<Type>; - -private: - - /** - * Pointer to the Fast_Bag. - */ - Fast_Bag<Type>* _bag; - - /** - * Pointer to the array used to represent the Fast_Bag. - */ - Type* _array; - - /** - * The point in the Fast_Bag the iterator has come to. - */ - int _index; - - /** - * Flag used to indicate if the iterator has reached the "end". The - * value of GetCurrent() will still be the "last" element in - * the Fast_Bag. - */ - bool _end; - - -public: - /** - * Private copy-constructor the avoid usage. - */ - Fast_BagIterator(const Fast_BagIterator& source) : - _bag(source._bag), - _array(source._array), - _index(source._index), - _end(source._end) {} - - Fast_BagIterator &operator=(const Fast_BagIterator& source) - { - _bag = source._bag; - _array = source._array; - _index = source._index; - _end = source._end; - return *this; - } - /** - * Default constructor, the iterator if not initialized with a - * Fast_Bag - */ - Fast_BagIterator(void) : - _bag(nullptr), - _array(nullptr), - _index(0), - _end(true) - { - - } - - /** - * Contructor that inits the Fast_bag with a pointer to a Fast_Bag - * @param bag pointer to a Fast_Bag of the same Type as the iterator - */ - inline Fast_BagIterator(Fast_Bag<Type>* bag) : - _bag(bag), - _array(bag->_array), - _index(0), - _end(bag->NumberOfElements() == 0) - { - } - - - /** - * Contructor that inits the Fast_BagIterator with a reference to a Fast_Bag - * @param bag reference to a Fast_Bag of the same Type as the iterator - */ - inline Fast_BagIterator(Fast_Bag<Type>& bag) : - _bag(&bag), - _array(bag._array), - _index(0), - _end(bag.NumberOfElements() == 0) - { - } - - /** - * Destructor - nothing to do - (commented out because having it generated warning) - ~Fast_BagIterator(void){}; - */ - - /** - * Reinits (or intis) the Fast_BagIterator with another - * Fast_Bag Alows the user to reuse a Fast_BagIterator - * @param bag the Fast_Bag to init the Fast_BagIterator with - */ - inline void Start(Fast_Bag<Type>* bag) { - _bag = bag; - _array = bag->_array; - _index = 0; - if(_bag->NumberOfElements() == 0) { - _end = true; - } else { - _end = false; - } - } - - /** - * Reinits (or intis) the Fast_BagIterator with another - * Fast_Bag. Alows the user to reuse a Fast_BagIterator. - * @param bag the Fast_Bag to init the Fast_BagIterator with - */ - inline void Start(Fast_Bag<Type>& bag) { - _bag = &bag; - _array = bag._array; - _index = 0; - if(_bag->NumberOfElements() == 0) { - _end = true; - } else { - _end = false; - } - } - - - - /** - * @return the current element - */ - inline Type GetCurrent() { - return _array[_index]; - } - - /** - * Moves the Fast_BagIterator to the next element. If the - * Fast_BagIterator already are at the end the _end flag is set to - * true. - */ - inline void Next() { - - // If we are at the end and should return. - if(_end) { - return; - } - _index = _index + 1; - - if(_index == _bag->NumberOfElements()) { - // Now the return value from GetCurrent are undefined. - _end = true; - } - } - - /** - * Deletes the current element in the Fast_Bag and moves the - * Fast_BagIterator to the previous - */ - inline void RemoveCurrent() { - _bag->_numElements = _bag->_numElements - 1; - - _array[_index] = _array[_bag->NumberOfElements()]; - - _index = _index - 1; - } - - /** - * @return true if the Fast_BagIterator is at the end - */ - inline bool End() const { return _end; } - -}; - - -template <typename Type> -Fast_BagIterator<Type>& operator++(Fast_BagIterator<Type>& bi) { - bi.Next(); - return bi; -} - -template <typename Type> -Fast_BagIterator<Type> operator++(Fast_BagIterator<Type>& bi, int) { - Fast_BagIterator<Type> old = bi; - bi.Next(); - return old; -} - - - -template <class Type> -Fast_Bag<Type>::Fast_Bag() : - _capacity(1), - _array(new Type[_capacity]), - _blocksize(1), - _numElements(0) -{ - -} - - -template <class Type> -Fast_Bag<Type>::Fast_Bag(const Fast_Bag<Type>& source) : - _capacity(source._capacity), - _array(nullptr), - _blocksize(source._blocksize), - _numElements(source._numElements) -{ - // Self assignment - if(this == &source) return; - - _array = new Type[_capacity]; - - for(int i = 0; i < _capacity; i++) { - _array[i] = source._array[i]; - } -} - - -template <class Type> -Fast_Bag<Type>::Fast_Bag(const int capacity) : - _capacity(capacity), - _array(new Type[capacity]), - _blocksize(capacity), - _numElements(0) -{ - -} - - -template <class Type> -Fast_Bag<Type>::Fast_Bag(const int capacity, const int blocksize) : - _capacity(capacity), - _array(new Type[capacity]), - _blocksize(blocksize), - _numElements(0) -{ - -} - - -template <class Type> -Fast_Bag<Type>::~Fast_Bag() -{ - delete[] _array; -} - - -template<class Type> -inline Fast_Bag<Type>& Fast_Bag<Type>::operator=(const Fast_Bag<Type>& other) -{ - // Self assignment - if(this == &other) return *this; - - if(_array != nullptr) { - delete[] _array; - } - - _numElements = other._numElements; - _capacity = other._capacity; - _blocksize = other._blocksize; - _array = new Type[_capacity]; - - for(int i = 0; i < _numElements; i++) { - _array[i] = other._array[i]; - } - - return *this; -} - - -template<class Type> -inline bool Fast_Bag<Type>::operator==(const Fast_Bag<Type>& other) const -{ - if(_numElements == other._numElements && - _capacity == other._capacity && - _blocksize == other._blocksize) - { - for(int i = 0; i < _numElements; i++) { - if (!(_array[i] == other._array[i])) return false; - } - return true; - } else { - return false; - } -} - - -template <class Type> -void Fast_Bag<Type>::Grow() -{ - int newCapacity = _capacity + _blocksize; - - // Grow exponentially - _blocksize = newCapacity; - - Type* newArray = new Type[newCapacity]; - - for(int i = 0; i < _capacity; i++) { - newArray[i] = _array[i]; - } - - _capacity = newCapacity; - - delete[] _array; - _array = newArray; -} - - -template <class Type> -inline void Fast_Bag<Type>::SetBlocksize(const int blocksize) -{ - if(blocksize > 0 ) { - _blocksize = blocksize; - } -} - - -template <class Type> -inline void Fast_Bag<Type>::Insert(const Type element) -{ - if(_numElements == _capacity) Grow(); - _array[_numElements++] = element; -} - - -template <class Type> -void Fast_Bag<Type>::RemoveAllElements() -{ - delete[] _array; - _numElements = 0; - _array = new Type[_capacity]; -} - - -template <class Type> -void Fast_Bag<Type>::RemoveElement(const Type element) -{ - for(Fast_BagIterator<Type> it(this); - !it.End(); - it.Next()) { - if(it.GetCurrent() == element) { - it.RemoveCurrent(); - } - } -} - -template <class Type> -bool Fast_Bag<Type>::HasElement(const Type element) -{ - bool retVal=false; - for(Fast_BagIterator<Type> it(this); - !it.End(); - it.Next()) { - if(it.GetCurrent() == element) { - retVal=true; - break; - } - } - return retVal; -} diff --git a/fastlib/src/vespa/fastlib/util/base64.cpp b/fastlib/src/vespa/fastlib/util/base64.cpp deleted file mode 100644 index 8b9ac45e698..00000000000 --- a/fastlib/src/vespa/fastlib/util/base64.cpp +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** -***************************************************************************** -* @author Aleksander Øhrn -* @date Creation date: 2000-10-16 -* Utility functions for base-64 encoding/decoding. -*****************************************************************************/ - -#include "base64.h" -#include <cstdio> -#include <cctype> - -static const char _base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -int -Fast_Base64::Decode(const char *source, unsigned int length, char *destination) { - - unsigned int i; - - // Sanity checks. - if (!source || !destination) - return -1; - - int state = 0; - int index = 0; - char symbol; - - // Process all source symbols. - for (i = 0; i < length; ++i) { - - symbol = source[i]; - - // Skip whitespace. - if (isspace(symbol)) - continue; - - // Are we at the end? - if (symbol == '=') - break; - - // Lookup symbol in base-64 table. - if ((symbol >= 'A') && (symbol <= 'Z')) - symbol = symbol - 'A'; - else if ((symbol >= 'a') && (symbol <= 'z')) - symbol = symbol - 'a' + 26; - else if ((symbol >= '0') && (symbol <= '9')) - symbol = symbol - '0' + 52; - else if (symbol == '+') - symbol = 62; - else if (symbol == '/') - symbol = 63; - else - return -1; - - // Write stuff into the destination buffer. - switch (state) { - case 0: destination[index] = symbol << 2; - state = 1; - break; - case 1: destination[index] |= symbol >> 4; - destination[++index] = (symbol & 0x0f) << 4; - state = 2; - break; - case 2: destination[index] |= symbol >> 2; - destination[++index] = (symbol & 0x03) << 6; - state = 3; - break; - case 3: destination[index++] |= symbol; - state = 0; - break; - } - - } - - // Did we end on a byte boundary, and/or with erroneous trailing characters? - if (i < length) { - - // We've got a pad character. Skip it, and get the next symbol. - symbol = source[++i]; - - // Check state. - switch (state) { - case 0: - case 1: return -1; - case 2: for (; i < length; ++i) { - symbol = source[i]; - if (!isspace(symbol)) - break; - } - if (symbol != '=' || i == length) - return -1; - symbol = source[++i]; - [[fallthrough]]; - case 3: for (; i < length; ++i) { - symbol = source[i]; - if (symbol == '\0') - break; - if (!isspace(symbol)) - return -1; - } - if (destination[index] != '\0') - return -1; - } - } - - // We ended by seeing the end of the string. Make sure we have no partial bytes lying around. - else { - if (state != 0) - return -1; - } - - return index; - -} - - -int -Fast_Base64::Encode(const char *source, unsigned int length, char *destination) { - - unsigned int i; - - // Sanity checks. - if (!source || !destination) - return -1; - - char a, b, c, d; // Holds encoded bytes. - const char *p = source; // Points into the current location in the source buffer. - char *q = destination; // Points into the current location in the destination buffer. - unsigned int n = length / 3; // Number of 4-byte encodings. - unsigned int m = length - 3 * n; // Remainder if length is not divisible by 3. - - // Encode symbols. Three source symbols translates into four destination synbols. - for (i = 0; i < n; ++i) { - a = ((p[0] & 0xfc) >> 2); - b = ((p[0] & 0x03) << 4) | ((p[1] & 0xf0) >> 4); - c = ((p[1] & 0x0f) << 2) | ((p[2] & 0xc0) >> 6); - d = p[2] & 0x3f; - p += 3; - sprintf(q, "%c%c%c%c", - _base64[static_cast<int>(a)], - _base64[static_cast<int>(b)], - _base64[static_cast<int>(c)], - _base64[static_cast<int>(d)]); - q += 4; - } - - // Handle remaining symbols, if any. - if (m == 0) { - } - else if (m == 1) { - a = ((p[0] & 0xfc) >> 2); - b = ((p[0] & 0x03) << 4); - sprintf(q, "%c%c==", - _base64[static_cast<int>(a)], - _base64[static_cast<int>(b)]); - q += 4; - } - else { - a = ((p[0] & 0xfc) >> 2); - b = ((p[0] & 0x03) << 4) | ((p[1] & 0xf0) >> 4); - c = ((p[1] & 0x0f) << 2); - sprintf(q, "%c%c%c=", - _base64[static_cast<int>(a)], - _base64[static_cast<int>(b)], - _base64[static_cast<int>(c)]); - q += 4; - } - - return q - destination + 1; - -} diff --git a/fastlib/src/vespa/fastlib/util/base64.h b/fastlib/src/vespa/fastlib/util/base64.h deleted file mode 100644 index 4a1cb873e7b..00000000000 --- a/fastlib/src/vespa/fastlib/util/base64.h +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** -***************************************************************************** -* @author Aleksander Øhrn -* @date Creation date: 2001-10-16 -* Utility functions for base-64 encoding/decoding. -*****************************************************************************/ - -#pragma once - -/** -***************************************************************************** -* Utility functions for base-64 encoding/decoding. -* -* @class Fast_Base64 -* @author Aleksander Øhrn -* @date Creation date : 2001-10-16 -*****************************************************************************/ - -class Fast_Base64 { -public: - - /** - ***************************************************************************** - * Encodes source in base64 into destination. - * Returns the number of data bytes stored in destination, or -1 on error. - * - * @param source is the source buffer - * @param length is the length of the source string - * @param destination is the destination buffer - * @return Length of the destination string, INCLUDING the trailing '\0' byte. - * Returns -1 on error. - * @author Aleksander Øhrn - ***************************************************************************/ - static int Encode(const char *source, - unsigned int length, - char *destination); - - /** - *************************************************************************** - * Decodes the base64 encoded source into destination. - * Returns the number of data bytes stored in destination, or -1 on error. - * Note that there is no trailing '\0' byte automatically padded to the - * destination buffer. So the length returned is the same as was originally - * used for length the Encode() call. - * - * @param source is the source buffer - * @param length is the length of the source string, - * NOT including the terminating '\0' byte - * @param destination is the destination buffer, identical to the buffer - * that originally went into Encode(). - * @return Length of the destination string. - * Returns -1 on error. - * @author Aleksander Øhrn - **************************************************************************/ - static int Decode(const char *source, - unsigned int length, - char *destination); - -}; diff --git a/fastlib/src/vespa/fastlib/util/tests/CMakeLists.txt b/fastlib/src/vespa/fastlib/util/tests/CMakeLists.txt index 3ba1ff161cd..3254d9832d1 100644 --- a/fastlib/src/vespa/fastlib/util/tests/CMakeLists.txt +++ b/fastlib/src/vespa/fastlib/util/tests/CMakeLists.txt @@ -1,20 +1,4 @@ # Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -vespa_add_executable(fastlib_base64test_app TEST - SOURCES - base64test.cpp - DEPENDS - fastlib_util - fastlib_fast_testsuite -) -vespa_add_test(NAME fastlib_base64test_app NO_VALGRIND COMMAND fastlib_base64test_app fastlib_base64test_app) -vespa_add_executable(fastlib_bagtest_app TEST - SOURCES - bagtest.cpp - DEPENDS - fastlib_util - fastlib_fast_testsuite -) -vespa_add_test(NAME fastlib_bagtest_app NO_VALGRIND COMMAND fastlib_bagtest_app) vespa_add_executable(fastlib_wildcard_match_test_app TEST SOURCES wildcard_match_test.cpp diff --git a/fastlib/src/vespa/fastlib/util/tests/bagtest.cpp b/fastlib/src/vespa/fastlib/util/tests/bagtest.cpp deleted file mode 100644 index cb5e8f468b6..00000000000 --- a/fastlib/src/vespa/fastlib/util/tests/bagtest.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "bagtest.h" - - - -int BagTest::Main() { - BagTester bt; - bt.SetStream(&std::cout); - bt.Run(); - if (bt.Report() > 0) { - return 1; - } - - return 0; -} - - -FASTOS_MAIN(BagTest) diff --git a/fastlib/src/vespa/fastlib/util/tests/bagtest.h b/fastlib/src/vespa/fastlib/util/tests/bagtest.h deleted file mode 100644 index 4aec082948e..00000000000 --- a/fastlib/src/vespa/fastlib/util/tests/bagtest.h +++ /dev/null @@ -1,555 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include <vespa/fastlib/util/bag.h> -#include <vespa/fastlib/testsuite/test.h> -#include <vespa/fastos/app.h> -#include <iostream> - -/** - - */ -class Tester -{ -private: - bool _isTouched; - - int _i; - -protected: - Tester() : _isTouched(false), _i() { } - -public: - Tester(int i) : _isTouched(false), _i(i) { } - - ~Tester() { } - - bool IsTouched() const { return _isTouched; } - - void Touch() { - if(_isTouched) { _isTouched = false; } - else { _isTouched = true; } - } - - int GetI() { return _i; } - - -}; - -std::ostream& operator<<(std::ostream& s, const Tester* t) -{ - return s << static_cast<const void*>(t) << ": " << (t->IsTouched()?"X":"-"); -} - - -//Print method, not used when thing works -template <typename T> void PrintBag(Fast_Bag<T>* bag) { - for (Fast_BagIterator<T> bagIterator(bag); - !bagIterator.End(); - bagIterator.Next()) { - std::cout << bagIterator.GetCurrent() << " "; - } - std::cout << std::endl; -} -//Print method, not used when thing works -template <typename T> void PrintArray(T* array, int n) { - for(int i = 0; i < n; i++) { - std::cout << array[i] << " "; - } - std::cout << std::endl; -} - - -class BagTester : public Test -{ -private: - BagTester(const BagTester &other); - BagTester& operator=(const BagTester &other); - - Tester** _array; - Fast_Bag<Tester*>* _bag; - - int _elements; - - - void RunTest(void (BagTester::*testFunc)()) { - StartUp(); - (this->*testFunc)(); - TearDown(); - } - - -protected: - - /** Touches all elemenets in the bag */ - void TouchBag(Fast_Bag<Tester*>* bag) { - for(Fast_BagIterator<Tester*> bagIterator(bag); - !bagIterator.End(); - bagIterator.Next()) { - bagIterator.GetCurrent()->Touch(); - } - } - - - /** Generate a test bag with num tester and init cap of max */ - void InitArray(int num, int maxCapacity = 0) { - if (maxCapacity == 0) maxCapacity = num; - _bag = new Fast_Bag<Tester*>(maxCapacity); - for(int i = 0; i < num; i++) { - Tester* tester = new Tester(i); - _array[i] = tester; - _bag->Insert(tester); - } - _elements = num; - } - -public: - - BagTester(void) - : _array(NULL), - _bag(NULL), - _elements(0) - { - } - - - /** Checks if the iterator new'ed with a bagpointer works */ - void IterPtrInitTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - TouchBag(_bag); - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - /** Checks if the iterator new'ed with a bagref works */ - void IterRefInitTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - for(Fast_BagIterator<Tester*> bagIterator(*_bag); - !bagIterator.End(); - bagIterator.Next()) { - bagIterator.GetCurrent()->Touch(); - } - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - void IterPPOperTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - for(Fast_BagIterator<Tester*> bagIterator(*_bag); - !bagIterator.End(); - ++bagIterator, - bagIterator++) { - bagIterator.GetCurrent()->Touch(); - } - - for(int i = 0 ; i < num; i = i + 2) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - - /** Checks if the iterator new'ed with a bagref works */ - void IterPtrStartTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - Fast_BagIterator<Tester*> bagIterator; - - for(bagIterator.Start(*_bag); - !bagIterator.End(); - bagIterator.Next()) { - bagIterator.GetCurrent()->Touch(); - } - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - - - /** Checks if the iterator new'ed with a bagref works */ - void IterRefStartTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - Fast_BagIterator<Tester*> bagIterator; - - for(bagIterator.Start(_bag); - !bagIterator.End(); - bagIterator.Next()) { - bagIterator.GetCurrent()->Touch(); - } - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - - - - void IterStartOverTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - - int n = 0; - Fast_BagIterator<Tester*> bagIterator; - for(bagIterator.Start(_bag); - !bagIterator.End(); - bagIterator.Next()) { - if(n>4) break; - n++; - } - - for(bagIterator.Start(_bag); - !bagIterator.End(); - bagIterator.Next()) { - bagIterator.GetCurrent()->Touch(); - } - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - - - - /** Checks if the delete in iterator works */ - void DeleteEnumTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - - Tester* del = NULL; - - for(Fast_BagIterator<Tester*> bagIterator(_bag); - !bagIterator.End(); - bagIterator.Next()) { - if(bagIterator.GetCurrent()->GetI() == 5) { - del = bagIterator.GetCurrent(); - bagIterator.RemoveCurrent(); - } - } - - // <inv: del points to a tester not in the bag anymore> - - TouchBag(_bag); - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - if(_array[i] != del) { - ok = false; - } - } - } - - delete _bag; - - _test(ok); - } - - /** Checks is the RemoveElement method in bag.*/ - void RemoveTest(void) { - bool ok = true; - int num = 10; - InitArray(num, num+num); - - Tester* del = NULL; - - for(Fast_BagIterator<Tester*> bagIterator(_bag); - !bagIterator.End(); - bagIterator.Next()) { - if(bagIterator.GetCurrent()->GetI() == 5) { - del = bagIterator.GetCurrent(); - } - } - - _bag->RemoveElement(del); - - // <inv: del points to a tester not in the bag anymore> - - TouchBag(_bag); - - for(int i = 0 ; i < 10; i++) { - if(!_array[i]->IsTouched()) { - if(_array[i] != del) { - ok = false; - } - } - } - - delete _bag; - - _test(ok); - } - - - /**Test the HasElement method in bag.*/ - void HasElementTest(void) { - bool ok = true; - int num = 10; - InitArray(num, num+num); - - Tester* current= NULL; - - for(Fast_BagIterator<Tester*> bagIterator(_bag); - !bagIterator.End(); - bagIterator.Next()) { - current=bagIterator.GetCurrent(); - if(!_bag->HasElement(current)) { - ok=false; - } - } - Tester* t=new Tester(4); - if(_bag->HasElement(t) ) { - ok=false; - } - - delete t; - delete _bag; - - _test(ok); - } - - void GrowTest(void) { - bool ok = true; - int num = 10; - InitArray(num, 2); - - // <inv: now the bag has grown many times> - TouchBag(_bag); - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - void CopyConstTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - Fast_Bag<Tester*> bag(*_bag); - TouchBag(&bag); - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - void AssignTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - - Fast_Bag<Tester*> bag; - bag = *_bag; - - TouchBag(&bag); - - for(int i = 0 ; i < num; i++) { - if(!_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - - void EqualTest(void) { - bool ok = true; - int num = 10; - - InitArray(num); - Fast_Bag<Tester*> equalBag; - equalBag = *_bag; - - if(!(equalBag == *_bag)) { ok = false; } - - Tester *elem1 = new Tester(4); - equalBag.Insert(elem1); - - if(equalBag == *_bag) { ok = false; } - - equalBag.RemoveElement(elem1); - delete elem1; - delete _bag; - - _test(ok); - } - - - void RemoveAllElementsTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - _bag->RemoveAllElements(); - - TouchBag(_bag); - - for(int i = 0 ; i < num; i++) { - if(_array[i]->IsTouched()) { - ok = false; - } - } - - delete _bag; - - _test(ok); - } - - void GetBlocksizeTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - if(_bag->GetBlocksize() != num) { - ok = false; - } - - delete _bag; - - _test(ok); - } - - void SetBlocksizeTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - _bag->SetBlocksize(19); - if(_bag->GetBlocksize() != 19) { - ok = false; - } - - delete _bag; - - _test(ok); - } - - void NumberOfElementsTest(void) { - bool ok = true; - int num = 10; - InitArray(num); - if(_bag->NumberOfElements() != num) { - ok= false; - } - _bag->RemoveAllElements(); - if(_bag->NumberOfElements() != 0) { - ok = false; - } - - delete _bag; - - _test(ok); - } - - void StartUp(void) { - _array = new Tester*[1024]; - } - - void TearDown(void) { - for(int i = 0; i < _elements; i++) { - delete _array[i]; - } - delete[] _array; - } - - void Run() override { - RunTest(&BagTester::IterPtrInitTest); - RunTest(&BagTester::IterRefInitTest); - RunTest(&BagTester::IterPtrStartTest); - RunTest(&BagTester::IterRefStartTest); - RunTest(&BagTester::IterStartOverTest); - RunTest(&BagTester::IterPPOperTest); - RunTest(&BagTester::GrowTest); - RunTest(&BagTester::AssignTest); - RunTest(&BagTester::CopyConstTest); - RunTest(&BagTester::EqualTest); - RunTest(&BagTester::DeleteEnumTest); - RunTest(&BagTester::RemoveTest); - RunTest(&BagTester::HasElementTest); - RunTest(&BagTester::RemoveAllElementsTest); - RunTest(&BagTester::GetBlocksizeTest); - RunTest(&BagTester::SetBlocksizeTest); - RunTest(&BagTester::NumberOfElementsTest); - } - -}; - -class BagTest : public FastOS_Application -{ -public: - int Main() override; -}; diff --git a/fastlib/src/vespa/fastlib/util/tests/base64test.cpp b/fastlib/src/vespa/fastlib/util/tests/base64test.cpp deleted file mode 100644 index 16bb3ccd9ad..00000000000 --- a/fastlib/src/vespa/fastlib/util/tests/base64test.cpp +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <vespa/fastos/app.h> -#include <vespa/fastos/file.h> -#include <vespa/vespalib/stllike/string.h> -#include <vespa/fastlib/util/base64.h> -#include <cassert> - -class Base64Test : public FastOS_Application { -public: - int Main() override; -}; - -int -Base64Test::Main(void) { - - char buffer0[1024]; // Original text. - char buffer1[1024]; // Encoded text. - char buffer2[1024]; // Decoded text. - - sprintf(buffer0, "Hello, world! This is a test. 123."); - - int length0 = strlen(buffer0); - int length1 = Fast_Base64::Encode(buffer0, length0, buffer1); - - assert(length1 != -1); - - int length2 = Fast_Base64::Decode(buffer1, length1, buffer2); - - assert(length2 != -1); - assert(length2 == length0); - assert(0 == strncmp(buffer0, buffer2, length0)); - - printf("Original = '%.*s'\n", length0, buffer0); - printf("Encoded = '%.*s'\n", length1, buffer1); - printf("Decoded = '%.*s'\n", length2, buffer2); - - - // Big file test - vespalib::string filename("base64test"); - - if (_argc > 1) { - filename.assign(_argv[1]); - } - - FastOS_StatInfo statInfo; - int filesize = 0; - if (FastOS_File::Stat(filename.c_str(), &statInfo)) { - filesize = statInfo._size; - } else { - printf("FAILURE: Could not stat file %s\n", filename.c_str()); - exit(1); - } - - FastOS_File testFile; - if (!testFile.OpenReadOnly(filename.c_str())) { - printf ("FAILURE: Could not open file %s for reading\n", filename.c_str()); - exit(1); - } - - - auto unencoded = std::make_unique<char[]>(filesize); - auto encoded = std::make_unique<char[]>(filesize * 2); - auto decoded = std::make_unique<char[]>(filesize + 1); - testFile.ReadBuf(unencoded.get(), filesize); - - int encLen = Fast_Base64::Encode(unencoded.get(), filesize, encoded.get()); - if (encLen == -1) { - printf("FAILURE: Encoding failed\n"); - exit(1); - } - - // encLen is including the trailing '\0' byte, so subtract one - int decLen = Fast_Base64::Decode(encoded.get(), encLen - 1, decoded.get()); - if (decLen == -1) { - printf("FAILURE: Decoding failed\n"); - exit(1); - } - - if (filesize != decLen) { - printf("FAILURE: decoded length != original filesize, filesize = %d, decLen = %d\n", filesize, decLen); - exit(1); - } - char *uencP = unencoded.get(); - char *decP = decoded.get(); - for (int i = 0; i < filesize; i++) { - if (*uencP != *decP) { - printf ("FAILURE: Encode or Decode ERROR! at byte offset %d\n", i); - exit(1); - } - uencP++; - decP++; - } - printf("SUCCESS: Encode/decode OK\n"); - - return 0; - -} - -FASTOS_MAIN(Base64Test) |