~marcusbritanicus/newbreeze/master

« back to all changes in this revision

Viewing changes to Plugins/MarkDown/buffer.h

  • Committer: Marcus Britanicus
  • Date: 2016-02-24 22:14:39 UTC
  • Revision ID: git-v1:1725d245965cb9fb0ab4095b69b569bbc0b30224
NewBreeze v3.0.0. MarkDownPreview plugin now uses libsoldout. Thu Feb 25 03:44:39 IST 2016

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* buffer.h - automatic buffer structure */
 
2
 
 
3
/*
 
4
 * Copyright (c) 2008, Natacha Porté
 
5
 *
 
6
 * Permission to use, copy, modify, and distribute this software for any
 
7
 * purpose with or without fee is hereby granted, provided that the above
 
8
 * copyright notice and this permission notice appear in all copies.
 
9
 *
 
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
17
 */
 
18
 
 
19
#ifndef LITHIUM_BUFFER_H
 
20
#define LITHIUM_BUFFER_H
 
21
 
 
22
#include <stddef.h>
 
23
 
 
24
 
 
25
/********************
 
26
 * TYPE DEFINITIONS *
 
27
 ********************/
 
28
 
 
29
/* struct buf • character array buffer */
 
30
struct buf {
 
31
        char *  data;   /* actual character data */
 
32
        size_t  size;   /* size of the string */
 
33
        size_t  asize;  /* allocated size (0 = volatile buffer) */
 
34
        size_t  unit;   /* reallocation unit size (0 = read-only buffer) */
 
35
        int     ref;            /* reference count */
 
36
};
 
37
 
 
38
 
 
39
 
 
40
/**********
 
41
 * MACROS *
 
42
 **********/
 
43
 
 
44
/* CONST_BUF • global buffer from a string litteral */
 
45
#define CONST_BUF(name, string) static struct buf name = { string, sizeof string -1, sizeof string }
 
46
 
 
47
 
 
48
/* VOLATILE_BUF • macro for creating a volatile buffer on the stack */
 
49
#define VOLATILE_BUF(name, strname) struct buf name = { strname, strlen(strname) }
 
50
 
 
51
 
 
52
/* BUFPUTSL • optimized bufputs of a string litteral */
 
53
#define BUFPUTSL(output, litteral) bufput(output, litteral, sizeof litteral - 1)
 
54
 
 
55
 
 
56
/***********************
 
57
 * FUNCTION ATTRIBUTES *
 
58
 ***********************/
 
59
 
 
60
/* BUF_ALLOCATOR • the function returns a completely new ponter */
 
61
#ifdef __GNUC__
 
62
#define BUF_ALLOCATOR __attribute__ ((malloc))
 
63
#else
 
64
#define BUF_ALLOCATOR
 
65
#endif
 
66
 
 
67
 
 
68
/* BUF_PRINTF_LIKE • marks the function as behaving like printf */
 
69
#ifdef __GNUC__
 
70
#define BUF_PRINTF_LIKE(format_index, first_variadic_index) __attribute__ ((format (printf, format_index, first_variadic_index)));
 
71
#else
 
72
#define BUF_PRINTF_LIKE(format_index, first_variadic_index)
 
73
#endif
 
74
 
 
75
 
 
76
/********************
 
77
 * BUFFER FUNCTIONS *
 
78
 ********************/
 
79
 
 
80
/* bufcasecmp • case-insensitive buffer comparison */
 
81
int bufcasecmp(const struct buf *, const struct buf *);
 
82
 
 
83
/* bufcmp • case-sensitive buffer comparison */
 
84
int bufcmp(const struct buf *, const struct buf *);
 
85
 
 
86
/* bufcmps • case-sensitive comparison of a string to a buffer */
 
87
int bufcmps(const struct buf *, const char *);
 
88
 
 
89
/* bufdup • buffer duplication */
 
90
struct buf * bufdup(const struct buf *, size_t) BUF_ALLOCATOR;
 
91
 
 
92
/* bufgrow • increasing the allocated size to the given value */
 
93
int bufgrow(struct buf *, size_t);
 
94
 
 
95
/* bufnew • allocation of a new buffer */
 
96
struct buf * bufnew(size_t) BUF_ALLOCATOR;
 
97
 
 
98
/* bufnullterm • NUL-termination of the string array (making a C-string) */
 
99
void bufnullterm(struct buf *);
 
100
 
 
101
/* bufprintf • formatted printing to a buffer */
 
102
void bufprintf(struct buf *, const char *, ...) BUF_PRINTF_LIKE(2, 3);
 
103
 
 
104
/* bufput • appends raw data to a buffer */
 
105
void bufput(struct buf *, const void*, size_t);
 
106
 
 
107
/* bufputs • appends a NUL-terminated string to a buffer */
 
108
void bufputs(struct buf *, const char*);
 
109
 
 
110
/* bufputc • appends a single char to a buffer */
 
111
void bufputc(struct buf *, char);
 
112
 
 
113
/* bufrelease • decrease the reference count and free the buffer if needed */
 
114
void bufrelease(struct buf *);
 
115
 
 
116
/* bufreset • frees internal data of the buffer */
 
117
void bufreset(struct buf *);
 
118
 
 
119
/* bufset • safely assigns a buffer to another */
 
120
void bufset(struct buf **, struct buf *);
 
121
 
 
122
/* bufslurp • removes a given number of bytes from the head of the array */
 
123
void bufslurp(struct buf *, size_t);
 
124
 
 
125
/* buftoi • converts the numbers at the beginning of the buf into an int */
 
126
int buftoi(struct buf *, size_t, size_t *);
 
127
 
 
128
#ifdef BUFFER_STDARG
 
129
#include <stdarg.h>
 
130
 
 
131
/* vbufprintf • stdarg variant of formatted printing into a buffer */
 
132
void
 
133
vbufprintf(struct buf *, const char*, va_list);
 
134
 
 
135
#endif /* def BUFFER_STDARG */
 
136
 
 
137
#endif /* ndef LITHIUM_BUFFER_H */