1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-19 19:16:34 +00:00

Factors string serialisation with \n\r conversion out of the Apple II and reuses it with the Oric.

This commit is contained in:
Thomas Harte
2018-05-13 13:57:19 -04:00
parent 8f1a516a2c
commit 39c0bc6c47
6 changed files with 102 additions and 36 deletions
+46
View File
@@ -0,0 +1,46 @@
//
// StringSerialiser.cpp
// Clock Signal
//
// Created by Thomas Harte on 13/05/2018.
// Copyright © 2018 Thomas Harte. All rights reserved.
//
#include "StringSerialiser.hpp"
using namespace Utility;
StringSerialiser::StringSerialiser(const std::string &source, bool use_linefeed_only) {
if(!use_linefeed_only) {
input_string_ = source;
} else {
input_string_.reserve(source.size());
// Commute any \ns that are not immediately after \rs to \rs; remove the rest.
bool saw_carriage_return = false;
for(auto character: source) {
if(character != '\n') {
input_string_.push_back(character);
} else {
if(!saw_carriage_return) {
input_string_.push_back('\r');
}
}
saw_carriage_return = character == '\r';
}
}
}
uint8_t StringSerialiser::head() {
if(input_string_pointer_ == input_string_.size())
return '\0';
return static_cast<uint8_t>(input_string_[input_string_pointer_]);
}
bool StringSerialiser::advance() {
if(input_string_pointer_ != input_string_.size()) {
++input_string_pointer_;
return input_string_pointer_ != input_string_.size();
}
return false;
}