1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2015-2016 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#pragma once
20
21#include "../grn.h"
22
23#include "ts_types.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/*-------------------------------------------------------------
30 * grn_ts_buf
31 */
32
33/* grn_ts_buf works as a buffer for arbitrary data. */
34
35typedef struct {
36 void *ptr; /* The starting address. */
37 size_t size; /* The size in bytes. */
38 size_t pos; /* The current position for grn_ts_buf_write(). */
39} grn_ts_buf;
40
41/* grn_ts_buf_init() initializes a buffer. */
42void grn_ts_buf_init(grn_ctx *ctx, grn_ts_buf *buf);
43
44/* grn_ts_buf_fin() finalizes a buffer. */
45void grn_ts_buf_fin(grn_ctx *ctx, grn_ts_buf *buf);
46
47#if 0
48/* grn_ts_buf_open() creates a buffer. */
49grn_rc grn_ts_buf_open(grn_ctx *ctx, grn_ts_buf **buf);
50
51/* grn_ts_buf_close() destroys a buffer. */
52void grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf);
53#endif
54
55/*
56 * grn_ts_buf_reserve() reserves enough memory to store `min_size` bytes.
57 * Note that this function never shrinks a buffer and does nothing if
58 * `min_size` is not greater than `buf->size`.
59 */
60grn_rc grn_ts_buf_reserve(grn_ctx *ctx, grn_ts_buf *buf, size_t min_size);
61
62/* grn_ts_buf_resize() resizes a buffer. */
63grn_rc grn_ts_buf_resize(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size);
64
65/*
66 * grn_ts_buf_write() writes data into a buffer. `buf->pos` specifies the
67 * position and it will be modified on success.
68 * Note that this function resizes a buffer if required.
69 */
70grn_rc grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf,
71 const void *ptr, size_t size);
72
73/*-------------------------------------------------------------
74 * grn_ts_rbuf
75 */
76
77/* grn_ts_rbuf works as a buffer for records. */
78
79typedef struct {
80 grn_ts_record *recs; /* Pointer to records. */
81 size_t n_recs; /* The number of records. */
82 size_t max_n_recs; /* The maximum number of records. */
83} grn_ts_rbuf;
84
85/* grn_ts_rbuf_init() initializes a buffer. */
86void grn_ts_rbuf_init(grn_ctx *ctx, grn_ts_rbuf *rbuf);
87
88/* grn_ts_rbuf_fin() finalizes a buffer. */
89void grn_ts_rbuf_fin(grn_ctx *ctx, grn_ts_rbuf *rbuf);
90
91/* grn_ts_rbuf_open() creates a buffer. */
92/*grn_rc grn_ts_rbuf_open(grn_ctx *ctx, grn_ts_rbuf **rbuf);*/
93
94/* grn_ts_rbuf_close() destroys a buffer. */
95/*void grn_ts_rbuf_close(grn_ctx *ctx, grn_ts_rbuf *rbuf);*/
96
97/*
98 * grn_ts_rbuf_reserve() reserves enough memory to store `n_recs` records.
99 * Note that this function never shrinks a buffer and does nothing if `n_recs`
100 * is not greater than the `rbuf->max_n_recs`.
101 */
102grn_rc grn_ts_rbuf_reserve(grn_ctx *ctx, grn_ts_rbuf *rbuf, size_t n_recs);
103
104/* grn_ts_rbuf_resize() resizes a buffer. */
105grn_rc grn_ts_rbuf_resize(grn_ctx *ctx, grn_ts_rbuf *rbuf,
106 size_t new_max_n_recs);
107
108#ifdef __cplusplus
109}
110#endif
111
112