src/StreamInputSource.h
branchv_0
changeset 2 3ab78bf63467
child 15 177321664baf
equal deleted inserted replaced
1:9aed8c2ea97d 2:3ab78bf63467
       
     1 /**
       
     2  * Relational pipes
       
     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 GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <istream>
       
    21 
       
    22 #include <xercesc/sax/InputSource.hpp>
       
    23 #include <xercesc/util/PlatformUtils.hpp>
       
    24 #include <xercesc/util/BinInputStream.hpp>
       
    25 
       
    26 namespace relpipe {
       
    27 namespace in {
       
    28 namespace xml {
       
    29 
       
    30 class StreamInputSource : public xercesc::InputSource {
       
    31 private:
       
    32 	std::istream& input;
       
    33 
       
    34 	class StreamBinInputStream : public xercesc::BinInputStream {
       
    35 	private:
       
    36 		std::istream& input;
       
    37 	public:
       
    38 
       
    39 		StreamBinInputStream(std::istream& input) : BinInputStream(), input(input) {
       
    40 		}
       
    41 
       
    42 		XMLFilePos curPos() const override {
       
    43 			return input.tellg();
       
    44 		}
       
    45 
       
    46 		const XMLCh* getContentType() const override {
       
    47 			return nullptr;
       
    48 		}
       
    49 
       
    50 		XMLSize_t readBytes(XMLByte * const toFill, const XMLSize_t maxToRead) override {
       
    51 			input.read((char*) toFill, maxToRead);
       
    52 			return input.gcount();
       
    53 		}
       
    54 	};
       
    55 
       
    56 public:
       
    57 
       
    58 	StreamInputSource(std::istream& input, xercesc::MemoryManager * const manager = xercesc::XMLPlatformUtils::fgMemoryManager) : InputSource(manager), input(input) {
       
    59 	}
       
    60 
       
    61 	virtual ~StreamInputSource() {
       
    62 	}
       
    63 
       
    64 	xercesc::BinInputStream* makeStream() const override {
       
    65 		// TODO: avoid multiple calls, input can be used only once 
       
    66 		return new StreamBinInputStream(input);
       
    67 	}
       
    68 };
       
    69 
       
    70 }
       
    71 }
       
    72 }