<commit_before>//
// Copyright (c) 2016, <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

#include "LobStreamer.hpp"

//////////////////////////////////////////////////////////////////
db::oracle::lob::Streamer::Streamer(const LocatorPtr l):
     locator(l),
     info({ErrorType::undefined, ""}),
     sizeb(8192),
     buffer(BufferVec(sizeb))
{
     return;
}

bool db::oracle::lob::Streamer::operator<<(std::istream& i) {

     clear_info();
     if (!i) {

	  info = {ErrorType::iostream, "input stream error"};
	  return false;
     }

     if (i.eof()) {

	  info = {ErrorType::iostream, "eof already yet"};
	  return false;
     }

     i.seekg(0, std::istream::end);
     auto ssz = i.tellg();
     i.seekg(0, std::istream::beg);
     offset of = 1;
     bool first = true;
     ReturnInfo ri;

     while (i) {

	  i.read(&buffer[0], sizeb);
	  if (i.good()) {
	       if (first) {
		    if (i.gcount() == ssz) {

			 ri = locator->write(of, &buffer[0], i.gcount());
			 ssz -= i.gcount();

		    } else {
			 ri = locator->write_first(of, ssz,
						   &buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }
		    first = false;

	       } else {
		    if (i.gcount() == ssz) {

			 ri = locator->write_last(nullptr, i.gcount());
			 ssz -= i.gcount();

		    } else {

			 ri = locator->write_next(&buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }
	       }

	       if (!ri) {

		    info = {ErrorType::oci, ri.string()};
		    return false;

	       } else {
		    if (! ri.needData()) return true;
	       }
	       continue;

	  } else {
	       if (i.eof()) {
		    if (i.gcount() == 0) {
			 if (first) {

			      return true;

			 } else {

			      info = {ErrorType::oci, ri.string()};
			      return false;
			 }
		    }

		    if (first) {

			 ri = locator->write(of, &buffer[0], i.gcount());
			 ssz -= i.gcount();
			 first = false;

		    } else {

			 ri = locator->write_last(&buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }

		    if (!ri) {

			 info = {ErrorType::oci, ri.string()};
			 return false;
		    }
		    return true;
	       }
	  }
	  break;
     }
     info = {ErrorType::iostream, "input stream error"};
     return false;
}

bool db::oracle::lob::Streamer::operator>>(std::ostream& o) {

     clear_info();
     if (!o) {

	  info = {ErrorType::iostream, "input stream error"};
	  return false;
     }

     offset of = 1;
     auto ri = locator->read(of, &buffer[0], sizeb);
     while (ri) {

	  auto truesize = locator->get_size();
	  if (truesize == 0) return true;
	  o.write(&buffer[0], truesize);
	  if (o) {

	       of += truesize;
	       ri = locator->read(of, &buffer[0], sizeb);
	       continue;
	  }
	  info = {ErrorType::iostream, "input stream error"};
	  return false;
     }
     info = {ErrorType::oci, ri.string()};
     return false;
}

void db::oracle::lob::Streamer::clear_info() {

     info = {ErrorType::undefined, ""};
}

db::oracle::lob::Streamer::ErrorInfo
db::oracle::lob::Streamer::getErrorInfo() const {<commit_msg>fix lob::Streamer::operator>><commit_after>//
// Copyright (c) 2016, <NAME> <<EMAIL>>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

#include "LobStreamer.hpp"

//////////////////////////////////////////////////////////////////
db::oracle::lob::Streamer::Streamer(const LocatorPtr l):
     locator(l),
     info({ErrorType::undefined, ""}),
     sizeb(8192),
     buffer(BufferVec(sizeb))
{
     return;
}

bool db::oracle::lob::Streamer::operator<<(std::istream& i) {

     clear_info();
     if (!i) {

	  info = {ErrorType::iostream, "input stream error"};
	  return false;
     }

     if (i.eof()) {

	  info = {ErrorType::iostream, "eof already yet"};
	  return false;
     }

     i.seekg(0, std::istream::end);
     auto ssz = i.tellg();
     i.seekg(0, std::istream::beg);
     offset of = 1;
     bool first = true;
     ReturnInfo ri;

     while (i) {

	  i.read(&buffer[0], sizeb);
	  if (i.good()) {
	       if (first) {
		    if (i.gcount() == ssz) {

			 ri = locator->write_once(of, &buffer[0], i.gcount());
			 ssz -= i.gcount();

		    } else {
			 ri = locator->write_first(of, ssz,
						   &buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }
		    first = false;

	       } else {
		    if (i.gcount() == ssz) {

			 ri = locator->write_last(nullptr, i.gcount());
			 ssz -= i.gcount();

		    } else {

			 ri = locator->write_next(&buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }
	       }

	       if (!ri) {

		    info = {ErrorType::oci, ri.string()};
		    return false;

	       } else {
		    if (! ri.needData()) return true;
	       }
	       continue;

	  } else {
	       if (i.eof()) {
		    if (i.gcount() == 0) {
			 if (first) {

			      return true;

			 } else {

			      info = {ErrorType::oci, ri.string()};
			      return false;
			 }
		    }

		    if (first) {

			 ri = locator->write_once(of, &buffer[0], i.gcount());
			 ssz -= i.gcount();
			 first = false;

		    } else {

			 ri = locator->write_last(&buffer[0], i.gcount());
			 ssz -= i.gcount();
		    }

		    if (!ri) {

			 info = {ErrorType::oci, ri.string()};
			 return false;
		    }
		    return true;
	       }
	  }
	  break;
     }
     info = {ErrorType::iostream, "input stream error"};
     return false;
}

bool db::oracle::lob::Streamer::operator>>(std::ostream& o) {

     clear_info();
     if (!o.good()) {

	  info = {ErrorType::iostream, "output stream error"};
	  return false;
     }

     offset of = 1;
     ReturnInfo ri;
     ri = locator->read_first(of, &buffer[0], sizeb);
     if (!ri) {

	  info = {ErrorType::oci, ri.string()};
	  return false;
     }

     o.write(&buffer[0], locator->bytes());
     if (!o.good()) {

	  info = {ErrorType::iostream, "output stream error"};
	  return false;
     }

     while (ri.needData()) {

	  ri = locator->read_next(&buffer[0], sizeb);
	  if (ri) {

	       o.write(&buffer[0], locator->bytes());
	       if (!o.good()) {

		    info = {ErrorType::iostream, "output stream error"};
		    return false;
	       }
	  } else break;
     }

     if(ri) return true;
     info = {ErrorType::oci, ri.string()};
     return false;
}

void db::oracle::lob::Streamer::clear_info() {

     info = {ErrorType::undefined, ""};
}

db::oracle::lob::Streamer::ErrorInfo
db::oracle::lob::Streamer::getErrorInfo() const {
{'fraction_non_alphanumeric': 0.20060195635816402, 'fraction_numerical': 0.008126410835214447, 'mean_word_length': 2.072584373555247, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7277975', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2263, 'n_words': 519}