| 1 |
/* |
| 2 |
* Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2021 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 мар. 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_INBITSTREAM_H_ |
| 23 |
#define LSP_PLUG_IN_IO_INBITSTREAM_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/io/IInStream.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 |
class InBitStream: public IInStream |
| 36 |
{ |
| 37 |
protected: |
| 38 |
IInStream *pIS; // Input stream |
| 39 |
size_t nWrapFlags; // Wrap flags |
| 40 |
umword_t nBuffer; // Fixed-size buffer |
| 41 |
size_t nBits; // Number of bits stored |
| 42 |
|
| 43 |
protected: |
| 44 |
status_t fill(); |
| 45 |
void unread(umword_t v, size_t bits); |
| 46 |
status_t do_close(); |
| 47 |
|
| 48 |
public: |
| 49 |
explicit InBitStream(); |
| 50 |
InBitStream(const InBitStream &) = delete; |
| 51 |
InBitStream(InBitStream &&) = delete; |
| 52 |
virtual ~InBitStream() override; |
| 53 |
|
| 54 |
InBitStream & operator = (const InBitStream &) = delete; |
| 55 |
InBitStream & operator = (InBitStream &&) = delete; |
| 56 |
|
| 57 |
public: |
| 58 |
/** Wrap stdio file descriptor. The Reader should be in closed state. |
| 59 |
* |
| 60 |
* @param fd file descriptor |
| 61 |
* @param close close file descriptor on close() |
| 62 |
* @return status of operation |
| 63 |
*/ |
| 64 |
status_t wrap(FILE *fd, bool close); |
| 65 |
|
| 66 |
/** Wrap native file descriptor. The Reader should be in closed state. |
| 67 |
* |
| 68 |
* @param fd file descriptor |
| 69 |
* @param close close file descriptor on close() |
| 70 |
* @return status of operation |
| 71 |
*/ |
| 72 |
status_t wrap_native(fhandle_t fd, bool close); |
| 73 |
|
| 74 |
/** Wrap file descriptor. The Reader should be in closed state. |
| 75 |
* |
| 76 |
* @param fd file descriptor |
| 77 |
* @param flags wrapping flags |
| 78 |
* @return status of operation |
| 79 |
*/ |
| 80 |
status_t wrap(File *fd, size_t flags); |
| 81 |
|
| 82 |
/** Wrap output stream |
| 83 |
* |
| 84 |
* @param is input stream |
| 85 |
* @param flags wrapping flags |
| 86 |
* @return status of operation |
| 87 |
*/ |
| 88 |
status_t wrap(IInStream *is, size_t flags = 0); |
| 89 |
|
| 90 |
/** Open input stream associated with file. The Reader should be in closed state. |
| 91 |
* |
| 92 |
* @param path file location path |
| 93 |
* @return status of operation |
| 94 |
*/ |
| 95 |
status_t open(const char *path); |
| 96 |
|
| 97 |
/** Open input stream associated with file. The Reader should be in closed state. |
| 98 |
* |
| 99 |
* @param path file location path |
| 100 |
* @return status of operation |
| 101 |
*/ |
| 102 |
status_t open(const LSPString *path); |
| 103 |
|
| 104 |
/** Open input stream associated with file. Before open currently open stream is closed and it's |
| 105 |
* state is reset. |
| 106 |
* |
| 107 |
* @param path file location path |
| 108 |
* @return status of operation |
| 109 |
*/ |
| 110 |
status_t open(const Path *path); |
| 111 |
|
| 112 |
public: |
| 113 |
virtual ssize_t read(void *dst, size_t count) override; |
| 114 |
ssize_t bread(void *buf, size_t bits); |
| 115 |
|
| 116 |
virtual wssize_t bskip(wsize_t amount); |
| 117 |
|
| 118 |
virtual status_t close() override; |
| 119 |
|
| 120 |
public: |
| 121 |
ssize_t readb(bool *value); |
| 122 |
ssize_t readv(bool *value) { return readb(value); } |
| 123 |
ssize_t readv(uint8_t *value, size_t bits = sizeof(uint8_t)*8); |
| 124 |
inline ssize_t readv(int8_t *value, size_t bits = sizeof(int8_t)*8) { return readv(reinterpret_cast<uint8_t *>(value), bits); } |
| 125 |
ssize_t readv(uint16_t *value, size_t bits = sizeof(uint16_t)*8); |
| 126 |
inline ssize_t readv(int16_t *value, size_t bits = sizeof(int16_t)*8) { return readv(reinterpret_cast<uint16_t *>(value), bits); } |
| 127 |
|
| 128 |
ssize_t readv(uint32_t *value, size_t bits = sizeof(uint32_t)*8); |
| 129 |
inline ssize_t readv(int32_t *value, size_t bits = sizeof(int32_t)*8) { return readv(reinterpret_cast<uint32_t *>(value), bits); } |
| 130 |
ssize_t readv(uint64_t *value, size_t bits = sizeof(uint64_t)*8); |
| 131 |
inline ssize_t readv(int64_t *value, size_t bits = sizeof(int64_t)*8) { return readv(reinterpret_cast<uint64_t *>(value), bits); } |
| 132 |
}; |
| 133 |
|
| 134 |
} /* namespace io */ |
| 135 |
} /* namespace lsp */ |
| 136 |
|
| 137 |
|
| 138 |
#endif /* LSP_PLUG_IN_IO_INBITSTREAM_H_ */ |