src/types/UnsignedIntegerDataTypeReader.h
branchv_0
changeset 46 12c329f5524f
parent 45 24a506eb97b5
equal deleted inserted replaced
45:24a506eb97b5 46:12c329f5524f
       
     1 /**
       
     2  * Relational pipes (library)
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the:
       
     7  *  - GNU Lesser General Public License as published by the Free Software Foundation;
       
     8  *    version 3 of the License or (at your option)
       
     9  *  - GNU General Public License as published by the Free Software Foundation;
       
    10  *    version 2 of the License.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful,
       
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    15  * GNU General Public License for more details.
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License
       
    18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    19  */
       
    20 #pragma once
       
    21 
       
    22 #include <string>
       
    23 #include <iostream>
       
    24 #include <cassert>
       
    25 #include <limits>
       
    26 
       
    27 #include <relpipe/protocol/constants.h>
       
    28 
       
    29 #include "../../include/relpipe/reader/RelpipeReaderException.h"
       
    30 #include "../../include/relpipe/reader/typedefs.h"
       
    31 #include "../DataTypeReader.h"
       
    32 
       
    33 namespace relpipe {
       
    34 namespace reader {
       
    35 namespace types {
       
    36 
       
    37 using namespace relpipe::protocol;
       
    38 using namespace relpipe::reader;
       
    39 
       
    40 /**
       
    41  * Unsigned variable-length integer.
       
    42  * ULEB128
       
    43  */
       
    44 class IntegerDataTypeReader : public DataTypeReader<integer_t> { // FIXME: relpipe::common::type::UnsignedInteger
       
    45 private:
       
    46 
       
    47 	octet_t readNextOctet(std::istream &input) {
       
    48 		octet_t value = input.get();
       
    49 		if (input.good()) return value;
       
    50 		else throw RelpipeReaderException(L"Unable to read next octet of the integer.");
       
    51 	}
       
    52 
       
    53 public:
       
    54 
       
    55 	IntegerDataTypeReader() : DataTypeReader<integer_t>(TypeId::INTEGER, DATA_TYPE_CODE_INTEGER) {
       
    56 	}
       
    57 
       
    58 	integer_t readValue(std::istream &input) override {
       
    59 		integer_t value = 0;
       
    60 		integer_t shift = 0;
       
    61 		octet_t octet;
       
    62 		do {
       
    63 			octet = readNextOctet(input);
       
    64 			value += integer_t(octet & 0x7F) << shift;
       
    65 			shift += 7;
       
    66 		} while (octet >= 128);
       
    67 		return value;
       
    68 	}
       
    69 
       
    70 	string_t toString(const integer_t &value) override {
       
    71 		return std::to_wstring(value);
       
    72 	}
       
    73 
       
    74 };
       
    75 
       
    76 }
       
    77 }
       
    78 }