ftpiiu_plugin/include/ioBuffer.h

89 lines
2.5 KiB
C
Raw Normal View History

2020-04-05 21:16:16 +02:00
// ftpd is a server implementation based on the following:
// - RFC 959 (https://tools.ietf.org/html/rfc959)
// - RFC 3659 (https://tools.ietf.org/html/rfc3659)
// - suggested implementation details from https://cr.yp.to/ftp/filesystem.html
//
// Copyright (C) 2020 Michael Theall
//
// This program 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.
//
// This program 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <cstddef>
#include <memory>
2020-04-06 07:36:03 +02:00
/// \brief I/O buffer
/// [unusable][usedArea][freeArea]
2020-04-05 21:16:16 +02:00
class IOBuffer
{
public:
~IOBuffer ();
2020-04-06 07:36:03 +02:00
/// \brief Parameterized constructor
/// \param size_ Buffer size
2020-04-05 21:16:16 +02:00
IOBuffer (std::size_t size_);
2020-04-06 07:36:03 +02:00
/// \brief Get pointer to writable area
2020-04-05 21:16:16 +02:00
char *freeArea () const;
2020-04-06 07:36:03 +02:00
/// \brief Get size of writable area
2020-04-05 21:16:16 +02:00
std::size_t freeSize () const;
2020-04-06 07:36:03 +02:00
/// \brief Get pointer to readable area
2020-04-05 21:16:16 +02:00
char *usedArea () const;
2020-04-06 07:36:03 +02:00
/// \brief Get size of readable area
2020-04-05 21:16:16 +02:00
std::size_t usedSize () const;
2020-04-06 07:36:03 +02:00
/// \brief Consume data from the beginning of usedArea
/// \param size_ Size to consume
/// [unusable][+++++usedArea][freeArea]
/// becomes
/// [unusable+++++][usedArea][freeArea]
void markFree (std::size_t size_);
/// \brief Produce data to the end of usedArea from freeArea
/// [unusable][usedArea][++++++freeArea]
/// becomes
/// [unusable][usedArea++++++][freeArea]
2020-04-05 21:16:16 +02:00
void markUsed (std::size_t size_);
2020-04-06 07:36:03 +02:00
/// \brief Whether usedArea is empty
2020-04-05 21:16:16 +02:00
bool empty () const;
2020-04-06 07:36:03 +02:00
/// \brief Get buffer capacity
2020-04-05 21:16:16 +02:00
std::size_t capacity () const;
2020-04-06 07:36:03 +02:00
/// \brief Clear buffer; usedArea becomes empty
/// [unusable][usedArea][++++++freeArea]
/// becomes
/// [freeArea++++++++++++++++++++++++++]
2020-04-05 21:16:16 +02:00
void clear ();
2020-04-06 07:36:03 +02:00
/// \brief Move usedArea to the beginning of the buffer
/// [unusable][usedArea][freeArea]
/// becomes
/// [usedArea][freeArea++++++++++]
2020-04-05 21:16:16 +02:00
void coalesce ();
private:
2020-04-06 07:36:03 +02:00
/// \brief Buffer
2020-04-05 21:16:16 +02:00
std::unique_ptr<char[]> m_buffer;
2020-04-06 07:36:03 +02:00
/// \brief Buffer size
2020-04-05 21:16:16 +02:00
std::size_t const m_size;
2020-04-06 07:36:03 +02:00
/// \brief Start of usedArea
2020-04-05 21:16:16 +02:00
std::size_t m_start = 0;
2020-04-06 07:36:03 +02:00
/// \brief Start of freeArea
std::size_t m_end = 0;
2020-04-05 21:16:16 +02:00
};