| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 14 янв. 2018 г. |
| 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_FMT_LSPC_CHUNKACCESSOR_H_ |
| 23 |
#define LSP_PLUG_IN_FMT_LSPC_CHUNKACCESSOR_H_ |
| 24 |
|
| 25 |
#include <lsp-plug.in/runtime/version.h> |
| 26 |
|
| 27 |
#include <lsp-plug.in/common/types.h> |
| 28 |
#include <lsp-plug.in/common/status.h> |
| 29 |
#include <lsp-plug.in/fmt/lspc/lspc.h> |
| 30 |
#include <lsp-plug.in/stdlib/stdio.h> |
| 31 |
|
| 32 |
namespace lsp |
| 33 |
{ |
| 34 |
namespace lspc |
| 35 |
{ |
| 36 |
typedef struct Resource |
| 37 |
{ |
| 38 |
fhandle_t fd; // File handle |
| 39 |
size_t refs; // Number of references |
| 40 |
size_t bufsize; // Default buffer size |
| 41 |
uint32_t chunk_id; // Chunk identifier allocator |
| 42 |
wsize_t length; // Length of the output file |
| 43 |
|
| 44 |
status_t acquire(); |
| 45 |
status_t release(); |
| 46 |
status_t allocate(uint32_t *id); |
| 47 |
status_t write(const void *buf, size_t count); |
| 48 |
ssize_t read(wsize_t pos, void *buf, size_t count); |
| 49 |
} Resource; |
| 50 |
|
| 51 |
class ChunkAccessor |
| 52 |
{ |
| 53 |
protected: |
| 54 |
friend class File; |
| 55 |
|
| 56 |
protected: |
| 57 |
enum const_t |
| 58 |
{ |
| 59 |
MIN_BUF_SIZE = 0x1000 |
| 60 |
}; |
| 61 |
|
| 62 |
protected: |
| 63 |
uint8_t *pBuffer; // Pointer to the buffer |
| 64 |
size_t nBufSize; // Size of the buffer |
| 65 |
size_t nBufPos; // Current buffer position |
| 66 |
Resource *pFile; // Pointer to the shared file resource |
| 67 |
uint32_t nMagic; // Magic number as size of chunk |
| 68 |
status_t nErrorCode; // Last error code |
| 69 |
chunk_id_t nUID; // Unique chunk identifier |
| 70 |
|
| 71 |
protected: |
| 72 |
inline status_t set_error(status_t err) { return nErrorCode = err; } |
| 73 |
status_t do_close(); |
| 74 |
|
| 75 |
protected: |
| 76 |
explicit ChunkAccessor(Resource *fd, uint32_t magic); |
| 77 |
|
| 78 |
public: |
| 79 |
ChunkAccessor(const ChunkAccessor &) = delete; |
| 80 |
ChunkAccessor(ChunkAccessor &&) = delete; |
| 81 |
virtual ~ChunkAccessor(); |
| 82 |
|
| 83 |
ChunkAccessor & operator = (const ChunkAccessor &) = delete; |
| 84 |
ChunkAccessor & operator = (ChunkAccessor &&) = delete; |
| 85 |
|
| 86 |
public: |
| 87 |
inline size_t buffer_size() const { return nBufSize; } |
| 88 |
inline status_t last_error() const { return nErrorCode; } |
| 89 |
inline chunk_id_t unique_id() const { return nUID; } |
| 90 |
inline uint32_t magic() const { return nMagic; } |
| 91 |
|
| 92 |
public: |
| 93 |
/** Close chunk accessor |
| 94 |
* |
| 95 |
* @return status of operation |
| 96 |
*/ |
| 97 |
virtual status_t close(); |
| 98 |
}; |
| 99 |
|
| 100 |
} /* namespace lspc */ |
| 101 |
} /* namespace lsp */ |
| 102 |
|
| 103 |
#endif /* LSP_PLUG_IN_FMT_LSPC_CHUNKACCESSOR_H_ */ |