]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_dbufbase.c
6600cf7efa3d95ad688a20a3142eb68f82b835a8
[ulut.git] / ulut / ul_dbufbase.c
1 /*******************************************************************
2   uLan Utilities Library - C library of basic reusable constructions
3
4   ul_dbufbase.c - dynamicaly allocated buffer
5
6   (C) Copyright 2001-2004 by Pavel Pisa - Originator
7   (C) Copyright 2003-2004 by Frantisek Vacek - Originator
8
9   The uLan utilities library can be used, copied and modified under
10   next licenses
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.
20   
21   See files COPYING and README for details.
22
23  *******************************************************************/
24 #include <string.h>
25 #include "ul_utmalloc.h"
26 #include "ul_dbuff.h"
27
28 typedef unsigned char byte;
29
30 //-----------------------------------------------------------------
31 /**
32  * ul_dbuff_init - init memory allocated for dynamic buffer 
33  * @buf: buffer structure
34  * @flags: flags describing behaviour of the buffer
35  *         only UL_DBUFF_IS_STATIC flag is supported.
36  *         in this case buffer use unly static array sbuf 
37  *
38  * Returns capacity of initialised buffer
39  */
40 int ul_dbuff_init(ul_dbuff_t *buf, int flags)
41 {
42     buf->capacity = UL_DBUFF_SLEN;
43     buf->data = buf->sbuff;
44     buf->len = 0;
45     buf->flags = flags;
46     return buf->capacity;
47 }                                                                               
48
49 //-----------------------------------------------------------------
50 /**
51  * ul_dbuff_destroy - frees all resources allocated by buf 
52  * @buf: buffer structure
53  */
54 void ul_dbuff_destroy(ul_dbuff_t *buf)
55 {
56     ul_dbuff_prep(buf, 0);
57 }
58
59 //-----------------------------------------------------------------
60 /**
61  * ul_dbuff_prep - sets a new len and capacity of the buffer
62  * @buf: buffer structure
63  * @new_len: new desired buffer length
64  *
65  * Returns new buffer length
66  */
67 int ul_dbuff_prep(ul_dbuff_t *buf, int new_len)
68 {
69     if(buf->flags & UL_DBUFF_IS_STATIC) {
70         if(!buf->data || !buf->capacity) {
71             buf->capacity = sizeof(buf->sbuff);
72             buf->data = buf->sbuff;
73         }
74         if(new_len > buf->capacity) {
75             buf->len=0;
76             return 0;
77         }
78     }else{
79         if(new_len > UL_DBUFF_SLEN) {
80             if(new_len != buf->capacity){
81                 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
82                     free(buf->data);
83                 }
84                 buf->data=malloc(new_len);
85                 if(!buf->data){
86                     buf->capacity=0;
87                     buf->len=0;
88                     return 0;
89                 }
90                 buf->capacity=new_len;
91             }
92         }else{
93             if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
94                 free(buf->data);
95                 buf->data = &(buf->sbuff[0]);
96             }
97             buf->capacity=UL_DBUFF_SLEN;
98         }
99     }
100     buf->len = new_len;
101     memset(buf->data,0,new_len);
102     return buf->len;
103 }