2013-07-10 09:26:01 -04:00
|
|
|
/*******************************************************************************
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2012.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of Sortix.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
linebuffer.cpp
|
|
|
|
Provides a simple queue-like line buffering for terminals.
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
*******************************************************************************/
|
2012-02-09 17:54:24 -05:00
|
|
|
|
2012-03-21 19:52:29 -04:00
|
|
|
#include <sortix/kernel/platform.h>
|
2012-09-22 14:38:34 -04:00
|
|
|
#include <string.h>
|
2012-02-09 17:54:24 -05:00
|
|
|
#include "linebuffer.h"
|
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
static size_t OffsetIndex(size_t offset, size_t index, size_t length)
|
|
|
|
{
|
|
|
|
// TODO: Possible overflow here.
|
|
|
|
return (offset + index) % length;
|
|
|
|
}
|
|
|
|
|
|
|
|
LineBuffer::LineBuffer()
|
|
|
|
{
|
|
|
|
buffer = NULL;
|
|
|
|
bufferlength = 0;
|
|
|
|
bufferoffset = 0;
|
|
|
|
buffercommitted = 0;
|
|
|
|
bufferfrozen = 0;
|
|
|
|
bufferused = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LineBuffer::~LineBuffer()
|
|
|
|
{
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LineBuffer::Push(uint32_t unicode)
|
|
|
|
{
|
|
|
|
// Check if we need to allocate or resize the circular queue.
|
|
|
|
if ( bufferused == bufferlength )
|
|
|
|
{
|
|
|
|
size_t newbufferlength = (bufferlength) ? bufferlength * 2 : 32UL;
|
|
|
|
uint32_t* newbuffer = new uint32_t[newbufferlength];
|
|
|
|
if ( !newbuffer ) { return false; }
|
|
|
|
size_t elemsize = sizeof(*buffer);
|
|
|
|
size_t leadingavai = bufferlength-bufferoffset;
|
|
|
|
size_t leading = (leadingavai < bufferused) ? leadingavai : bufferused;
|
|
|
|
size_t trailing = bufferused - leading;
|
2012-09-22 14:38:34 -04:00
|
|
|
memcpy(newbuffer, buffer + bufferoffset, leading * elemsize);
|
|
|
|
memcpy(newbuffer + leading, buffer, trailing * elemsize);
|
2012-02-09 17:54:24 -05:00
|
|
|
delete[] buffer;
|
|
|
|
buffer = newbuffer;
|
|
|
|
bufferlength = newbufferlength;
|
|
|
|
bufferoffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t index = OffsetIndex(bufferoffset, bufferused++, bufferlength);
|
|
|
|
buffer[index] = unicode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LineBuffer::Pop()
|
|
|
|
{
|
|
|
|
if ( !CanPop() ) { return 0; }
|
|
|
|
uint32_t result = Peek();
|
|
|
|
bufferoffset = (bufferoffset+1) % bufferlength;
|
|
|
|
buffercommitted--;
|
|
|
|
bufferfrozen--;
|
|
|
|
bufferused--;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LineBuffer::Peek() const
|
|
|
|
{
|
|
|
|
if ( !CanPop() ) { return 0; }
|
|
|
|
size_t index = OffsetIndex(bufferoffset, 0, bufferlength);
|
|
|
|
return buffer[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LineBuffer::Backspace()
|
|
|
|
{
|
|
|
|
if ( !CanBackspace() ) { return 0; }
|
|
|
|
size_t index = OffsetIndex(bufferoffset, --bufferused, bufferlength);
|
|
|
|
return buffer[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineBuffer::Commit()
|
|
|
|
{
|
|
|
|
buffercommitted = bufferfrozen = bufferused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineBuffer::Freeze()
|
|
|
|
{
|
|
|
|
bufferfrozen = bufferused;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LineBuffer::CanPop() const
|
|
|
|
{
|
|
|
|
return buffercommitted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LineBuffer::CanBackspace() const
|
|
|
|
{
|
|
|
|
return bufferused - bufferfrozen;
|
|
|
|
}
|
|
|
|
}
|