| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 3 мар. 2021 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_IO_OUTBITSTREAM_H_ |
| 23 |
#define LSP_PLUG_IN_IO_OUTBITSTREAM_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/io/IOutStream.h> |
| 28 |
#include <lsp-plug.in/io/Path.h> |
| 29 |
#include <lsp-plug.in/io/File.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace io |
| 34 |
{ |
| 35 |
/** |
| 36 |
* Output bit stream |
| 37 |
*/ |
| 38 |
class OutBitStream: public IOutStream |
| 39 |
{ |
| 40 |
private: |
| 41 |
IOutStream *pOS; // Output stream for writing |
| 42 |
size_t nWrapFlags; // Wrapping flags |
| 43 |
umword_t nBuffer; // Fixed-size buffer |
| 44 |
size_t nBits; // Number of bits stored |
| 45 |
|
| 46 |
public: |
| 47 |
explicit OutBitStream(); |
| 48 |
OutBitStream(const OutBitStream &) = delete; |
| 49 |
OutBitStream(OutBitStream &&) = delete; |
| 50 |
virtual ~OutBitStream() override; |
| 51 |
|
| 52 |
OutBitStream & operator = (const OutBitStream &) = delete; |
| 53 |
OutBitStream & operator = (OutBitStream &&) = delete; |
| 54 |
|
| 55 |
protected: |
| 56 |
status_t do_flush_buffer(); |
| 57 |
|
| 58 |
public: |
| 59 |
status_t open(const char *path, size_t mode); |
| 60 |
status_t open(const LSPString *path, size_t mode); |
| 61 |
status_t open(const io::Path *path, size_t mode); |
| 62 |
|
| 63 |
status_t wrap(FILE *fd, bool close); |
| 64 |
status_t wrap_native(fhandle_t fd, bool close); |
| 65 |
status_t wrap(File *fd, size_t flags = 0); |
| 66 |
status_t wrap(IOutStream *os, size_t flags = 0); |
| 67 |
|
| 68 |
public: |
| 69 |
virtual ssize_t bwrite(const void *buf, size_t bits); |
| 70 |
|
| 71 |
public: |
| 72 |
virtual ssize_t write(const void *buf, size_t count) override; |
| 73 |
|
| 74 |
virtual status_t flush() override; |
| 75 |
virtual status_t close() override; |
| 76 |
|
| 77 |
public: |
| 78 |
|
| 79 |
status_t bwrite(bool value); |
| 80 |
inline status_t writev(bool value) { return bwrite(value); } |
| 81 |
inline status_t writev(uint8_t value, size_t bits = sizeof(uint8_t)*8) { return writev(uint32_t(value), bits); } |
| 82 |
inline status_t writev(int8_t value, size_t bits = sizeof(int8_t)*8) { return writev(uint32_t(value), bits); } |
| 83 |
inline status_t writev(uint16_t value, size_t bits = sizeof(uint16_t)*8) { return writev(uint32_t(value), bits); } |
| 84 |
inline status_t writev(int16_t value, size_t bits = sizeof(int16_t)*8) { return writev(uint32_t(value), bits); } |
| 85 |
|
| 86 |
status_t writev(uint32_t value, size_t bits = sizeof(uint32_t)*8); |
| 87 |
inline status_t writev(int32_t value, size_t bits = sizeof(int32_t)*8) { return writev(uint32_t(value), bits); } |
| 88 |
status_t writev(uint64_t value, size_t bits = sizeof(uint64_t)*8); |
| 89 |
inline status_t writev(int64_t value, size_t bits = sizeof(int64_t)*8) { return writev(uint64_t(value), bits); } |
| 90 |
}; |
| 91 |
|
| 92 |
} /* namespace io */ |
| 93 |
} /* namespace lsp */ |
| 94 |
|
| 95 |
#endif /* LSP_PLUG_IN_IO_OUTBITSTREAM_H_ */ |