4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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 MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
27 * Stack of paste buffers. Note that paste buffer data is not necessarily a C
32 paste_init_stack(struct paste_stack
*ps
)
38 paste_free_stack(struct paste_stack
*ps
)
40 while (paste_free_top(ps
) == 0)
44 /* Return each item of the stack in turn. */
46 paste_walk_stack(struct paste_stack
*ps
, uint
*idx
)
48 struct paste_buffer
*pb
;
50 pb
= paste_get_index(ps
, *idx
);
55 /* Get the top item on the stack. */
57 paste_get_top(struct paste_stack
*ps
)
59 if (ARRAY_LENGTH(ps
) == 0)
61 return (ARRAY_FIRST(ps
));
64 /* Get an item by its index. */
66 paste_get_index(struct paste_stack
*ps
, u_int idx
)
68 if (idx
>= ARRAY_LENGTH(ps
))
70 return (ARRAY_ITEM(ps
, idx
));
73 /* Free the top item on the stack. */
75 paste_free_top(struct paste_stack
*ps
)
77 struct paste_buffer
*pb
;
79 if (ARRAY_LENGTH(ps
) == 0)
91 /* Free an item by index. */
93 paste_free_index(struct paste_stack
*ps
, u_int idx
)
95 struct paste_buffer
*pb
;
97 if (idx
>= ARRAY_LENGTH(ps
))
100 pb
= ARRAY_ITEM(ps
, idx
);
101 ARRAY_REMOVE(ps
, idx
);
110 * Add an item onto the top of the stack, freeing the bottom if at limit. Note
111 * that the caller is responsible for allocating data.
114 paste_add(struct paste_stack
*ps
, char *data
, size_t size
, u_int limit
)
116 struct paste_buffer
*pb
;
121 while (ARRAY_LENGTH(ps
) >= limit
) {
128 pb
= xmalloc(sizeof *pb
);
129 ARRAY_INSERT(ps
, 0, pb
);
137 * Replace an item on the stack. Note that the caller is responsible for
141 paste_replace(struct paste_stack
*ps
, u_int idx
, char *data
, size_t size
)
143 struct paste_buffer
*pb
;
148 if (idx
>= ARRAY_LENGTH(ps
))
151 pb
= ARRAY_ITEM(ps
, idx
);