1 /*******************************************************************
2 uLan Utilities Library - C library of basic reusable constructions
4 ul_dbuff.c - dynamic buffer
6 (C) Copyright 2001-2004 by Pavel Pisa - Originator
7 (C) Copyright 2003-2004 by Frantisek Vacek - Originator
9 The uLan utilities library can be used, copied and modified under
11 - GPL - GNU General Public License
12 - LGPL - GNU Lesser General Public License
13 - MPL - Mozilla Public License
14 - and other licenses added by project originators
15 Code can be modified and re-distributed under any combination
16 of the above listed licenses. If contributor does not agree with
17 some of the licenses, he/she can delete appropriate line.
18 Warning, if you delete all lines, you are not allowed to
19 distribute source code and/or binaries utilizing code.
21 See files COPYING and README for details.
23 *******************************************************************/
28 #include "ul_utdefs.h"
34 #define UL_DBUFF_SLEN 8
36 #define UL_DBUFF_IS_STATIC 1
37 /* use only sbuf or reserved data space (no dynamic allocation) */
40 * struct ul_dbuff - Generic Buffer for Dynamic Data
41 * @len: actual length of stored data
42 * @capacity: capacity of allocated buffer
43 * @flags: only one flag (%UL_DBUFF_IS_STATIC) used now
44 * @data: pointer to dynamically allocated buffer
45 * @sbuff: static buffer for small data sizes
47 typedef struct ul_dbuff { /* generic buffer for dynamic data */
48 unsigned long len; /* actual length of stored data */
49 unsigned long capacity; /* capacity of allocated buffer */
50 int flags; /* only one flag (UL_DBUFF_IS_STATIC) exists*/
51 unsigned char *data; /* pointer to dynamically allocated buffer */
52 unsigned char sbuff[UL_DBUFF_SLEN]; /* static buffer */
57 int ul_dbuff_init(ul_dbuff_t *buf, int flags);
58 void ul_dbuff_destroy(ul_dbuff_t *buf);
59 int ul_dbuff_prep(ul_dbuff_t *buf, int new_len);
61 /* Extended set of routines */
63 int ul_dbuff_set_capacity(ul_dbuff_t *buf, int new_capacity);
64 int ul_dbuff_set_len(ul_dbuff_t *buf, int new_len);
65 int ul_dbuff_set(ul_dbuff_t *buf, unsigned char b, int len);
66 int ul_dbuff_cpy(ul_dbuff_t *buf, const void *b, int len);
67 int ul_dbuff_cat(ul_dbuff_t *buf, const void *b, int len);
68 int ul_dbuff_pos(const ul_dbuff_t *buf, unsigned char what, unsigned char quote);
69 int ul_dbuff_strcpy(ul_dbuff_t *buf, const char *str);
70 int ul_dbuff_strcat(ul_dbuff_t *buf, const char *str);
71 int ul_dbuff_append_byte(ul_dbuff_t *buf, unsigned char b);
72 int ul_dbuff_export(ul_dbuff_t *srcdb, void *dest, int maxlen);
74 void ul_dbuff_cut_pos(ul_dbuff_t *fromdb, ul_dbuff_t *todb, int n);
75 void ul_dbuff_cut_delimited(ul_dbuff_t *fromdb, ul_dbuff_t *todb, char delimiter, char quote);
76 void ul_dbuff_cut_token(ul_dbuff_t *fromdb, ul_dbuff_t *todb);
79 * ul_dbuff_ltrim - removes all white spaces from left of dbuff.
81 * Return Value: new len of dbuff (including terminating zero, if present)
83 int ul_dbuff_ltrim(ul_dbuff_t *buf);
85 * ul_dbuff_rtrim - removes all white spaces from right of dbuff.
86 * If dbuff is zero terminated, trimmed dbuff is also zero terminated
88 * Return Value: new len of dbuff (including terminating zero, if present)
90 int ul_dbuff_rtrim(ul_dbuff_t *buf);
92 * ul_dbuff_trim - removes all white spaces from left and right of dbuff.
93 * If dbuff is zero terminated, trimmed dbuff is also zero terminated
95 * Return Value: new len of dbuff (including terminating zero, if present)
97 int ul_dbuff_trim(ul_dbuff_t *buf);
99 void ul_dbuff_log_hex(ul_dbuff_t *buf, int log_level);
101 int ul_str_pos(const unsigned char *str, const unsigned char *what, unsigned char quote);
102 int ul_str_cpos(const unsigned char *str, unsigned char what, unsigned char quote);
103 int ul_str_ncpy(unsigned char *to, const unsigned char *from, int buff_size);
109 #endif /* _UL_DBUFF_H */