LispBM
lbm_channel.h
Go to the documentation of this file.
1 /*
2  Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program 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
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
19 #ifndef LBM_CHANNEL_H_
20 #define LBM_CHANNEL_H_
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <platform_mutex.h>
25 
26 #define TOKENIZER_BUFFER_SIZE 257
27 
28 #define CHANNEL_SUCCESS 1
29 #define CHANNEL_MORE 2
30 #define CHANNEL_END 3
31 #define CHANNEL_FULL 4
32 
33 #define CHANNEL_READER_CLOSED 1000
34 
37 typedef struct {
38  char buffer[TOKENIZER_BUFFER_SIZE];
39  unsigned int write_pos;
40  unsigned int read_pos;
41  bool more;
42  bool comment;
44  mutex_t lock;
46  // statistics
47  unsigned int row;
48  unsigned int column;
50 
54 typedef struct {
55  char *str;
56  unsigned int length;
57  unsigned int read_pos;
58  unsigned int write_pos;
59  bool more;
60  bool comment;
62  unsigned int row;
63  unsigned int column;
65 
68 typedef struct lbm_char_channel_s {
69 
70  void *state;
71  bool (*more)(struct lbm_char_channel_s *chan);
72  int (*peek)(struct lbm_char_channel_s *chan, unsigned int n, char *res);
73  bool (*read)(struct lbm_char_channel_s *chan, char *res);
74  bool (*drop)(struct lbm_char_channel_s *chan, unsigned int n);
75  bool (*comment)(struct lbm_char_channel_s *chan);
76  void (*set_comment)(struct lbm_char_channel_s *chan, bool comment);
77  void (*reader_close)(struct lbm_char_channel_s *chan);
78 
79  /* Either side */
80  bool (*channel_is_empty)(struct lbm_char_channel_s *chan);
81  bool (*channel_is_full)(struct lbm_char_channel_s *chan);
82  bool (*reader_is_closed)(struct lbm_char_channel_s *chan);
83 
84  /* Write side */
85  int (*write)(struct lbm_char_channel_s *chan, char c);
86  void (*writer_close)(struct lbm_char_channel_s *chan);
87 
88  /* Statistics */
89  unsigned int (*row)(struct lbm_char_channel_s *chan);
90  unsigned int (*column)(struct lbm_char_channel_s *chan);
91 
92  bool (*may_block)(struct lbm_char_channel_s *chan);
93 
95 
96 
97 /* Read side */
105 
115 int lbm_channel_peek(lbm_char_channel_t *chan, unsigned int n, char *res);
116 
122 bool lbm_channel_read(lbm_char_channel_t *chan, char *res);
123 
129 bool lbm_channel_drop(lbm_char_channel_t *chan, unsigned int n);
130 
136 
142 
147 
153 
154 /* Either side */
155 
161 
167 
168 /* Write side */
169 
178 int lbm_channel_write(lbm_char_channel_t *chan, char c);
179 
184 
185 /* Statistics */
186 
191 unsigned int lbm_channel_row(lbm_char_channel_t *chan);
192 
196 unsigned int lbm_channel_column(lbm_char_channel_t *chan);
197 
202 
203 /* Interface */
211  lbm_char_channel_t *chan,
212  char *str);
213 
215  lbm_char_channel_t *chan,
216  char *str,
217  unsigned int size);
218 
219 
225  lbm_char_channel_t *chan);
226 
227 
228 #endif
TOKENIZER_BUFFER_SIZE
#define TOKENIZER_BUFFER_SIZE
Definition: lbm_channel.h:26
lbm_char_channel_s::writer_close
void(* writer_close)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:86
lbm_string_channel_state_t::reader_closed
bool reader_closed
Definition: lbm_channel.h:61
lbm_channel_reader_is_closed
bool lbm_channel_reader_is_closed(lbm_char_channel_t *chan)
Definition: lbm_channel.c:68
lbm_channel_may_block
bool lbm_channel_may_block(lbm_char_channel_t *chan)
Definition: lbm_channel.c:80
lbm_channel_write
int lbm_channel_write(lbm_char_channel_t *chan, char c)
Definition: lbm_channel.c:56
lbm_buffered_channel_state_t::read_pos
unsigned int read_pos
Definition: lbm_channel.h:40
lbm_string_channel_state_t
Definition: lbm_channel.h:54
lbm_create_string_char_channel_size
void lbm_create_string_char_channel_size(lbm_string_channel_state_t *st, lbm_char_channel_t *chan, char *str, unsigned int size)
Definition: lbm_channel.c:411
lbm_string_channel_state_t::column
unsigned int column
Definition: lbm_channel.h:63
lbm_channel_writer_close
void lbm_channel_writer_close(lbm_char_channel_t *chan)
Definition: lbm_channel.c:60
lbm_char_channel_s::more
bool(* more)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:71
lbm_channel_drop
bool lbm_channel_drop(lbm_char_channel_t *chan, unsigned int n)
Definition: lbm_channel.c:36
lbm_string_channel_state_t::write_pos
unsigned int write_pos
Definition: lbm_channel.h:58
lbm_char_channel_s::read
bool(* read)(struct lbm_char_channel_s *chan, char *res)
Definition: lbm_channel.h:73
lbm_buffered_channel_state_t::reader_closed
bool reader_closed
Definition: lbm_channel.h:43
lbm_char_channel_s
Definition: lbm_channel.h:68
lbm_char_channel_s::drop
bool(* drop)(struct lbm_char_channel_s *chan, unsigned int n)
Definition: lbm_channel.h:74
lbm_string_channel_state_t::length
unsigned int length
Definition: lbm_channel.h:56
lbm_buffered_channel_state_t::mutex_initialized
bool mutex_initialized
Definition: lbm_channel.h:45
lbm_char_channel_s::channel_is_empty
bool(* channel_is_empty)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:80
lbm_char_channel_s::may_block
bool(* may_block)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:92
lbm_buffered_channel_state_t::row
unsigned int row
Definition: lbm_channel.h:47
lbm_char_channel_s::column
unsigned int(* column)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:90
lbm_channel_peek
int lbm_channel_peek(lbm_char_channel_t *chan, unsigned int n, char *res)
Definition: lbm_channel.c:28
lbm_char_channel_s::peek
int(* peek)(struct lbm_char_channel_s *chan, unsigned int n, char *res)
Definition: lbm_channel.h:72
lbm_string_channel_state_t::read_pos
unsigned int read_pos
Definition: lbm_channel.h:57
lbm_channel_column
unsigned int lbm_channel_column(lbm_char_channel_t *chan)
Definition: lbm_channel.c:76
lbm_string_channel_state_t::more
bool more
Definition: lbm_channel.h:59
lbm_channel_reader_close
void lbm_channel_reader_close(lbm_char_channel_t *chan)
Definition: lbm_channel.c:64
lbm_buffered_channel_state_t::lock
mutex_t lock
Definition: lbm_channel.h:44
lbm_channel_is_empty
bool lbm_channel_is_empty(lbm_char_channel_t *chan)
Definition: lbm_channel.c:48
lbm_char_channel_s::row
unsigned int(* row)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:89
lbm_char_channel_s::state
void * state
Definition: lbm_channel.h:70
lbm_buffered_channel_state_t::comment
bool comment
Definition: lbm_channel.h:42
lbm_channel_row
unsigned int lbm_channel_row(lbm_char_channel_t *chan)
Definition: lbm_channel.c:72
lbm_channel_more
bool lbm_channel_more(lbm_char_channel_t *chan)
Definition: lbm_channel.c:24
lbm_char_channel_s::reader_close
void(* reader_close)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:77
lbm_buffered_channel_state_t::column
unsigned int column
Definition: lbm_channel.h:48
lbm_channel_read
bool lbm_channel_read(lbm_char_channel_t *chan, char *res)
Definition: lbm_channel.c:32
lbm_char_channel_s::write
int(* write)(struct lbm_char_channel_s *chan, char c)
Definition: lbm_channel.h:85
lbm_buffered_channel_state_t::write_pos
unsigned int write_pos
Definition: lbm_channel.h:39
lbm_channel_comment
bool lbm_channel_comment(lbm_char_channel_t *chan)
Definition: lbm_channel.c:40
lbm_string_channel_state_t::row
unsigned int row
Definition: lbm_channel.h:62
lbm_create_string_char_channel
void lbm_create_string_char_channel(lbm_string_channel_state_t *st, lbm_char_channel_t *chan, char *str)
Definition: lbm_channel.c:380
lbm_string_channel_state_t::str
char * str
Definition: lbm_channel.h:55
lbm_char_channel_s::comment
bool(* comment)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:75
lbm_channel_set_comment
void lbm_channel_set_comment(lbm_char_channel_t *chan, bool comment)
Definition: lbm_channel.c:44
lbm_buffered_channel_state_t::more
bool more
Definition: lbm_channel.h:41
lbm_char_channel_s::channel_is_full
bool(* channel_is_full)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:81
lbm_char_channel_s::reader_is_closed
bool(* reader_is_closed)(struct lbm_char_channel_s *chan)
Definition: lbm_channel.h:82
lbm_char_channel_s::set_comment
void(* set_comment)(struct lbm_char_channel_s *chan, bool comment)
Definition: lbm_channel.h:76
lbm_char_channel_t
struct lbm_char_channel_s lbm_char_channel_t
lbm_channel_is_full
bool lbm_channel_is_full(lbm_char_channel_t *chan)
Definition: lbm_channel.c:52
lbm_create_buffered_char_channel
void lbm_create_buffered_char_channel(lbm_buffered_channel_state_t *st, lbm_char_channel_t *chan)
Definition: lbm_channel.c:221
lbm_buffered_channel_state_t
Definition: lbm_channel.h:37
lbm_string_channel_state_t::comment
bool comment
Definition: lbm_channel.h:60