Hash :
414dca15
Author :
Thomas de Grivel
Date :
2024-12-04T20:19:29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
/**
* @file buf.h
* @brief Byte buffer.
*
* Structure to manipulate byte buffers.
*/
#ifndef LIBKC3_BUF_H
#define LIBKC3_BUF_H
#include <stdarg.h>
#include <stdlib.h>
#include "types.h"
#define BUF_SIZE 10240
#define PROTO_BUF_PEEK(type) \
sw buf_peek_ ## type (s_buf *buf, type *dest)
#define PROTO_BUF_READ(type) \
sw buf_read_ ## type (s_buf *buf, type *dest)
/* Stack-allocation compatible functions, call buf_clean after use. */
void buf_clean (s_buf *buf);
s_buf * buf_init (s_buf *buf, bool p_free, uw size, char *p);
s_buf * buf_init_const (s_buf *buf, uw size, const char *p);
s_buf * buf_init_1 (s_buf *buf, bool p_free, char *p);
s_buf * buf_init_1_const (s_buf *buf, const char *p);
s_buf * buf_init_alloc (s_buf *buf, uw size);
s_buf * buf_init_str (s_buf *buf, bool free, s_str *p);
s_buf * buf_init_str_const (s_buf *buf, const s_str *p);
s_buf * buf_init_str_copy (s_buf *buf, const s_str *str);
/* Heap-allocation compatible functions, call buf_delete after use. */
void buf_delete (s_buf *buf);
s_buf * buf_new (bool free, uw size, char *p);
s_buf * buf_new_1 (bool free, uw size, char *p);
s_buf * buf_new_alloc (uw bytes);
s_buf * buf_new_str (s_str *str);
/* Operators. */
void buf_empty (s_buf *buf);
sw buf_f (s_buf *buf, const char *fmt, ...);
sw buf_flush (s_buf *buf);
sw buf_ignore (s_buf *buf, uw size);
sw buf_ignore_line (s_buf *buf);
sw buf_ignore_newline (s_buf *buf);
sw buf_ignore_spaces (s_buf *buf);
sw buf_ignore_spaces_but_newline (s_buf *buf);
sw buf_peek_1 (s_buf *buf, const char *p);
sw buf_peek_character_utf8 (s_buf *buf, character *p);
PROTO_BUF_PEEK(f32);
PROTO_BUF_PEEK(f64);
sw buf_peek_next_character_utf8 (s_buf *buf, character *p);
PROTO_BUF_PEEK(s8);
PROTO_BUF_PEEK(s16);
PROTO_BUF_PEEK(s32);
PROTO_BUF_PEEK(s64);
PROTO_BUF_PEEK(sw);
sw buf_peek_str (s_buf *buf, const s_str *src);
sw buf_peek_to_str (s_buf *buf, s_str *dest);
PROTO_BUF_PEEK(u8);
PROTO_BUF_PEEK(u16);
PROTO_BUF_PEEK(u32);
PROTO_BUF_PEEK(u64);
PROTO_BUF_PEEK(uw);
sw buf_read_integer (s_buf *buf, s_integer *dst);
sw buf_read_character_utf8 (s_buf *buf, character *p);
s_str * buf_read (s_buf *buf, uw size, s_str *dest);
sw buf_read_1 (s_buf *buf, const char *p);
sw buf_read_f32 (s_buf *buf, f32 *p);
sw buf_read_f64 (s_buf *buf, f64 *p);
sw buf_read_s8 (s_buf *buf, s8 *p);
sw buf_read_s16 (s_buf *buf, s16 *p);
sw buf_read_s32 (s_buf *buf, s32 *p);
sw buf_read_s64 (s_buf *buf, s64 *p);
sw buf_read_str (s_buf *buf, const s_str *src);
sw buf_read_sym (s_buf *buf, const s_sym *src);
s_str * buf_read_to_str (s_buf *buf, s_str *dest);
sw buf_read_u8 (s_buf *buf, u8 *p);
sw buf_read_u16 (s_buf *buf, u16 *p);
sw buf_read_u32 (s_buf *buf, u32 *p);
sw buf_read_u64 (s_buf *buf, u64 *p);
s_str * buf_read_until_1_into_str(s_buf *buf, const char *end,
s_str *dest);
s_str * buf_read_until_character_into_str (s_buf *buf, character end,
s_str *dest);
sw buf_read_until_space_into_str (s_buf *buf, s_str *dest);
s_str * buf_read_until_str_into_str (s_buf *buf, const s_str *end,
s_str *dest);
sw buf_read_integer (s_buf *buf, s_integer *dst);
sw buf_refill (s_buf *buf, sw size);
sw buf_refill_compact (s_buf *buf);
s_buf * buf_restore (s_buf *buf, const s_buf *save);
sw buf_seek (s_buf *buf, sw offset, u8 whence);
sw buf_str_to_hex (s_buf *buf, const s_str *src);
sw buf_str_to_hex_size (const s_str *src);
sw buf_u8_to_hex (s_buf *buf, const u8 *x);
sw buf_u8_to_hex_size (const u8 *x);
sw buf_vf (s_buf *buf, const char *fmt, va_list ap);
sw buf_write (s_buf *buf, const void *data, uw len);
sw buf_write_1 (s_buf *buf, const char *p);
sw buf_write_1_size (s_pretty *pretty, const char *p);
sw buf_write_character_utf8 (s_buf *buf, character c);
sw buf_write_f32 (s_buf *buf, f32 x);
sw buf_write_f64 (s_buf *buf, f64 x);
sw buf_write_s8 (s_buf *buf, s8 i);
sw buf_write_s16 (s_buf *buf, s16 i);
sw buf_write_s32 (s_buf *buf, s32 i);
sw buf_write_s64 (s_buf *buf, s64 i);
sw buf_write_str (s_buf *buf, const s_str *src);
sw buf_write_str_size (s_pretty *pretty, const s_str *src);
sw buf_write_str_without_indent (s_buf *buf, const s_str *src);
sw buf_write_str_without_indent_size (s_pretty *pretty,
const s_str *src);
sw buf_write_u8 (s_buf *buf, u8 i);
sw buf_write_u16 (s_buf *buf, u16 i);
sw buf_write_u32 (s_buf *buf, u32 i);
sw buf_write_u64 (s_buf *buf, u64 i);
sw buf_xfer (s_buf *buf, s_buf *src, uw size);
sw buf_xfer_reverse(s_buf *src, s_buf *dest);
/* Observers. */
s_str * buf_slice_to_str (const s_buf *buf, uw start, uw end,
s_str *dest);
s_str * buf_to_str (const s_buf *buf, s_str *dest);
s_ratio * buf_to_ratio (const s_buf *buf, s_ratio *ratio);
#endif /* LIBKC3_BUF_H */