# HG changeset patch # User František Kučera # Date 1531608321 -7200 # Node ID 03750aff86196e3b25df1bb82a978ee13af8a388 # Parent 01dd90eeedbbb87d7ae40f439dbd8f799d511d17 writer only writes + refactoring diff -r 01dd90eeedbb -r 03750aff8619 include/RelpipeWriterException.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/RelpipeWriterException.h Sun Jul 15 00:45:21 2018 +0200 @@ -0,0 +1,26 @@ +#pragma once + +#include + +using namespace std; + +namespace relpipe { +namespace writer { + +class RelpipeWriterException { +private: + wstring message; +public: + + RelpipeWriterException(wstring message) : + message(message) { + } + + wstring getMessge() { + return message; + } + +}; + +} +} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 nbproject/configurations.xml --- a/nbproject/configurations.xml Sat Jul 14 23:24:10 2018 +0200 +++ b/nbproject/configurations.xml Sun Jul 15 00:45:21 2018 +0200 @@ -4,8 +4,13 @@ + src/BooleanDataTypeWriter.h include/DataTypeWriter.h include/DataTypeWriterBase.h + src/DataTypeWriterCatalog.h + src/IntegerDataTypeWriter.h + include/RelpipeWriterException.h + src/StringDataTypeWriter.h src/format.h include/typedefs.h @@ -45,12 +50,22 @@ + + + + + + + + + + @@ -78,12 +93,22 @@ + + + + + + + + + + diff -r 01dd90eeedbb -r 03750aff8619 src/BooleanDataType.h --- a/src/BooleanDataType.h Sat Jul 14 23:24:10 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -#pragma once - -#include -#include - -#include "DataType.h" -#include "RelpipeException.h" - -using namespace std; - -namespace rp_prototype { - -class BooleanDataType : public DataType { -private: - const wstring TRUE = L"true"; - const wstring FALSE = L"false"; -public: - - BooleanDataType() : DataType(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) { - } - - bool readValue(istream &input) override { - auto value = input.get(); // TODO: check failbit - if (value == 0) return false; - else if (value == 1) return true; - else throw RelpipeException(L"Unable to convert the octet to boolean", EXIT_CODE_DATA_ERROR); - } - - void writeValue(ostream &output, const bool &value) override { - output.put(value ? 1 : 0); - } - - bool toValue(const wstring &stringValue) override { - if (stringValue == TRUE) return true; - else if (stringValue == FALSE) return false; - else throw RelpipeException(L"Unable to convert the string to boolean", EXIT_CODE_DATA_ERROR); - } - - wstring toString(const bool &value) override { - return value ? TRUE : FALSE; - } - -}; - -} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/BooleanDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/BooleanDataTypeWriter.h Sun Jul 15 00:45:21 2018 +0200 @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include "../include/DataTypeWriter.h" +#include "../include/RelpipeWriterException.h" +#include "format.h" + +namespace relpipe { +namespace writer { + +class BooleanDataTypeWriter : public DataTypeWriter { +private: + const string_t TRUE = L"true"; + const string_t FALSE = L"false"; +public: + + BooleanDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_BOOLEAN, DATA_TYPE_CODE_BOOLEAN) { + } + + void writeValue(std::ostream &output, const boolean_t &value) override { + output.put(value ? 1 : 0); + } + + bool toValue(const string_t &stringValue) override { + if (stringValue == TRUE) return true; + else if (stringValue == FALSE) return false; + else throw RelpipeWriterException(L"Unable to convert the string to boolean"); + } + +}; + +} +} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/DataTypeCatalog.h --- a/src/DataTypeCatalog.h Sat Jul 14 23:24:10 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,47 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "common.h" -#include "DataType.h" -#include "BooleanDataType.h" -#include "IntegerDataType.h" -#include "StringDataType.h" - -using namespace std; - -namespace rp_prototype { - -class DataTypeCatalog { -private: - BooleanDataType booleanType; - IntegerDataType integerType; - StringDataType stringType; - vector types = {&booleanType, &integerType, &stringType}; -public: - - integer_t toTypeId(const wstring typeCode) { - for (DataTypeBase* dataType : types) if (dataType->supports(typeCode)) return dataType->getTypeId(); - throw RelpipeException(L"Unsupported data type: " + typeCode, EXIT_CODE_DATA_ERROR); - } - - wstring toTypeCode(const integer_t typeId) { - for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->getTypeCode(); - throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR); - } - - void writeString(ostream &output, const wstring &stringValue, const integer_t typeId) { - for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->writeString(output, stringValue); - throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR); - } - - wstring readString(istream &input, const integer_t typeId) { - for (DataTypeBase* dataType : types) if (dataType->supports(typeId)) return dataType->readString(input); - throw RelpipeException(L"Unsupported data type: " + typeId, EXIT_CODE_DATA_ERROR); - } - -}; - -} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/DataTypeWriterCatalog.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/DataTypeWriterCatalog.h Sun Jul 15 00:45:21 2018 +0200 @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +#include "../include/typedefs.h" +#include "../include/DataTypeWriterBase.h" +#include "BooleanDataTypeWriter.h" +#include "IntegerDataTypeWriter.h" +#include "StringDataTypeWriter.h" + +namespace relpipe { +namespace writer { + +class DataTypeCatalog { +private: + BooleanDataTypeWriter booleanWriter; + IntegerDataTypeWriter integerWriter; + StringDataTypeWriter stringWriter; + vector writers = {&booleanWriter, &integerWriter, &stringWriter}; +public: + + integer_t toTypeId(const string_t typeCode) { + for (DataTypeWriterBase* writer : writers) if (writer->supports(typeCode)) return writer->getTypeId(); + throw RelpipeWriterException(L"Unsupported data type: " + typeCode); + } + + string_t toTypeCode(const integer_t typeId) { + for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode(); + throw RelpipeWriterException(L"Unsupported data type: " + typeId); + } + + void writeString(std::ostream &output, const string_t &stringValue, const integer_t typeId) { + for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue); + throw RelpipeWriterException(L"Unsupported data type: " + typeId); + } + +}; + +} +} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/IntegerDataType.h --- a/src/IntegerDataType.h Sat Jul 14 23:24:10 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "common.h" -#include "DataType.h" -#include "RelpipeException.h" - -using namespace std; - -namespace rp_prototype { - -/** - * The prototype does not have various integer and other numeric data types, - * it just works with one type of integer. - * But this integer has variable length -- smaller values occupy only one byte, bigger ones, more bytes 1,2,4,8 + first byte (contains length signalization). - * In the real implementation of relational pipes, there will be DataTypes for particular numeric types. - * - * TODO: support also big endian architectures. - * TODO: throw exception if a value was stored in bigger type than needed (while reading – there should be only one supported way how to encode a single value) - * - * Example of encoded values: - * ------------------------------------------------------------------------------------------------- - * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do printf '%20s = ' $n; dist/Debug/GNU-Linux/rp-prototype write integer $n | hd | head -n 1; done - * 0 = 00000000 00 |.| - * 1 = 00000000 01 |.| - * 10 = 00000000 0a |.| - * 250 = 00000000 fa |.| - * 251 = 00000000 fb fb |..| - * 252 = 00000000 fb fc |..| - * 65535 = 00000000 fc ff ff |...| - * 65536 = 00000000 fd 00 00 01 00 |.....| - * 4294967295 = 00000000 fd ff ff ff ff |.....| - * 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| - * 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| - * ------------------------------------------------------------------------------------------------- - * - * Example of decoded values: - * ------------------------------------------------------------------------------------------------- - * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do dist/Debug/GNU-Linux/rp-prototype write integer $n | dist/Debug/GNU-Linux/rp-prototype read integer; done; - * 0 - * 1 - * 10 - * 250 - * 251 - * 252 - * 65535 - * 65536 - * 4294967295 - * 4294967296 - * 18446744073709551615 - * ------------------------------------------------------------------------------------------------- - * - * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets - * - */ -class IntegerDataType : public DataType { -private: - static const uint8_t INTEGER_TYPE_UINT8 = 251; - static const uint8_t INTEGER_TYPE_UINT16 = 252; - static const uint8_t INTEGER_TYPE_UINT32 = 253; - static const uint8_t INTEGER_TYPE_UINT64 = 254; - static const uint8_t INTEGER_TYPE_RESERVED = 255; - - template integer_t read(istream &input) { - T value = 0; - input.read(reinterpret_cast (&value), sizeof (value)); - return value; - } - - template void write(ostream &output, const integer_t &value) { - assert(sizeof (T) <= sizeof (value)); - output.write(reinterpret_cast (&value), sizeof (T)); - } - - template void write(ostream &output, const uint8_t type, const integer_t &value) { - write(output, type); - write(output, value); - } - - template bool fits(const integer_t &value) { - return value <= numeric_limits::max(); - } - -public: - - IntegerDataType() : DataType(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) { - } - - integer_t readValue(istream &input) override { - uint8_t first; - input.read(reinterpret_cast (&first), sizeof (first)); - if (input.good()) { - - if (first < INTEGER_TYPE_UINT8) return first; - else if (first == INTEGER_TYPE_UINT8) return read(input); - else if (first == INTEGER_TYPE_UINT16) return read(input); - else if (first == INTEGER_TYPE_UINT32) return read(input); - else if (first == INTEGER_TYPE_UINT64) return read(input); - else throw RelpipeException(L"Error while parsing integer type: unsupported type", EXIT_CODE_DATA_ERROR); - } else { - throw RelpipeException(L"Error while reading integer from the stream.", EXIT_CODE_DATA_ERROR); - } - } - - void writeValue(ostream &output, const integer_t &value) override { - // output << value; // by zapsalo číslo jako ASII text - - if (value < INTEGER_TYPE_UINT8) write(output, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT8, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT16, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT32, value); - else if (fits(value)) write(output, INTEGER_TYPE_UINT64, value); - else throw RelpipeException(L"Error while writing integer type: value too long", EXIT_CODE_DATA_ERROR); - } - - integer_t toValue(const wstring &stringValue) override { - // throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number - return stoul(stringValue); - } - - wstring toString(const integer_t &value) override { - return to_wstring(value); - } - -}; - -} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/IntegerDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/IntegerDataTypeWriter.h Sun Jul 15 00:45:21 2018 +0200 @@ -0,0 +1,104 @@ +#pragma once + +#include +#include +#include +#include + +#include "../include/DataTypeWriter.h" +#include "format.h" + +namespace relpipe { +namespace writer { + +/** + * The prototype does not have various integer and other numeric data types, + * it just works with one type of integer. + * But this integer has variable length -- smaller values occupy only one byte, bigger ones, more bytes 1,2,4,8 + first byte (contains length signalization). + * In the real implementation of relational pipes, there will be DataTypes for particular numeric types. + * + * TODO: support also big endian architectures. + * TODO: throw exception if a value was stored in bigger type than needed (while reading – there should be only one supported way how to encode a single value) + * + * Example of encoded values: + * ------------------------------------------------------------------------------------------------- + * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do printf '%20s = ' $n; dist/Debug/GNU-Linux/rp-prototype write integer $n | hd | head -n 1; done + * 0 = 00000000 00 |.| + * 1 = 00000000 01 |.| + * 10 = 00000000 0a |.| + * 250 = 00000000 fa |.| + * 251 = 00000000 fb fb |..| + * 252 = 00000000 fb fc |..| + * 65535 = 00000000 fc ff ff |...| + * 65536 = 00000000 fd 00 00 01 00 |.....| + * 4294967295 = 00000000 fd ff ff ff ff |.....| + * 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| + * 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| + * ------------------------------------------------------------------------------------------------- + * + * Example of decoded values: + * ------------------------------------------------------------------------------------------------- + * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do dist/Debug/GNU-Linux/rp-prototype write integer $n | dist/Debug/GNU-Linux/rp-prototype read integer; done; + * 0 + * 1 + * 10 + * 250 + * 251 + * 252 + * 65535 + * 65536 + * 4294967295 + * 4294967296 + * 18446744073709551615 + * ------------------------------------------------------------------------------------------------- + * + * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets + * + */ +class IntegerDataTypeWriter : public DataTypeWriter { +private: + static const uint8_t INTEGER_TYPE_UINT8 = 251; + static const uint8_t INTEGER_TYPE_UINT16 = 252; + static const uint8_t INTEGER_TYPE_UINT32 = 253; + static const uint8_t INTEGER_TYPE_UINT64 = 254; + static const uint8_t INTEGER_TYPE_RESERVED = 255; + + template void write(std::ostream &output, const integer_t &value) { + assert(sizeof (T) <= sizeof (value)); + output.write(reinterpret_cast (&value), sizeof (T)); + } + + template void write(std::ostream &output, const uint8_t type, const integer_t &value) { + write(output, type); + write(output, value); + } + + template bool fits(const integer_t &value) { + return value <= numeric_limits::max(); + } + +public: + + IntegerDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_INTEGER, DATA_TYPE_CODE_INTEGER) { + } + + void writeValue(std::ostream &output, const integer_t &value) override { + // output << value; // by zapsalo číslo jako ASII text + + if (value < INTEGER_TYPE_UINT8) write(output, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT8, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT16, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT32, value); + else if (fits(value)) write(output, INTEGER_TYPE_UINT64, value); + else throw RelpipeWriterException(L"Error while writing integer type: value too long"); + } + + integer_t toValue(const string_t &stringValue) override { + // throws „terminate called after throwing an instance of 'std::invalid_argument'“ SIGABRT, core dumped on invalid number + return stoul(stringValue); + } + +}; + +} +} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/StringDataType.h --- a/src/StringDataType.h Sat Jul 14 23:24:10 2018 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include "DataType.h" -#include "RelpipeException.h" -#include "IntegerDataType.h" - -using namespace std; - -namespace rp_prototype { - -/** - * The prototype does not recognize any encoding, - * it just works with c++ strings in encoding default to given platform. - * In the real implementation of relational pipes, there will be DataTypes for particular encodings. - */ -class StringDataType : public DataType { -private: - IntegerDataType integerType; - wstring_convert> convertor; // TODO: support also other encodings. -public: - - StringDataType() : DataType(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) { - } - - wstring readValue(istream &input) override { - integer_t length = integerType.readValue(input); - // TODO: check maximum length of single field - // if (length > 4000) throw RelpipeException("data too long", EXIT_CODE_DATA_ERROR); - vector buf(length); - input.read(buf.data(), length); - if (input.good()) { - return convertor.from_bytes(string(buf.data(), length)); - } else { - throw RelpipeException(L"Error while reading string from the stream.", EXIT_CODE_DATA_ERROR); - } - } - - void writeValue(ostream &output, const wstring &value) override { - string s = convertor.to_bytes(value); - integerType.writeValue(output, s.length()); - output << s.c_str(); - } - - wstring toValue(const wstring &stringValue) override { - return stringValue; - } - - wstring toString(const wstring &value) override { - return value; - } - -}; - -} \ No newline at end of file diff -r 01dd90eeedbb -r 03750aff8619 src/StringDataTypeWriter.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/StringDataTypeWriter.h Sun Jul 15 00:45:21 2018 +0200 @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "../include/DataTypeWriter.h" +#include "format.h" + +namespace relpipe { +namespace writer { + +/** + * The prototype does not recognize any encoding, + * it just works with c++ strings in encoding default to given platform. + * In the real implementation of relational pipes, there will be DataTypes for particular encodings. + */ +class StringDataTypeWriter : public DataTypeWriter { +private: + IntegerDataTypeWriter integerType; + std::wstring_convert> convertor; // TODO: support also other encodings. +public: + + StringDataTypeWriter() : DataTypeWriter(DATA_TYPE_ID_STRING, DATA_TYPE_CODE_STRING) { + } + + void writeValue(std::ostream &output, const string_t &value) override { + std::string s = convertor.to_bytes(value); + integerType.writeValue(output, s.length()); + output << s.c_str(); + } + + wstring toValue(const string_t &stringValue) override { + return stringValue; + } + +}; + +} +} \ No newline at end of file