]> rtime.felk.cvut.cz Git - ulut.git/blob - ulut/ul_dbufbase.c
uLUt: Get rid of compiler warnings in custom GAVL delete functions.
[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  * ul_dbuff_init - init memory allocated for dynamic buffer 
32  * @buf: buffer structure
33  * @flags: flags describing behaviour of the buffer
34  *         only UL_DBUFF_IS_STATIC flag is supported.
35  *         in this case buffer use unly static array sbuf 
36  *
37  * Returns capacity of initialised buffer
38  */
39 int ul_dbuff_init(ul_dbuff_t *buf, int flags)
40 {
41     buf->capacity = UL_DBUFF_SLEN;
42     buf->data = buf->sbuff;
43     buf->len = 0;
44     buf->flags = flags;
45     return buf->capacity;
46 }                                                                               
47
48 /**
49  * ul_dbuff_destroy - frees all resources allocated by buf 
50  * @buf: buffer structure
51  */
52 void ul_dbuff_destroy(ul_dbuff_t *buf)
53 {
54     ul_dbuff_prep(buf, 0);
55 }
56
57 /**
58  * ul_dbuff_prep - sets a new len and capacity of the buffer
59  * @buf: buffer structure
60  * @new_len: new desired buffer length
61  *
62  * Returns new buffer length
63  */
64 int ul_dbuff_prep(ul_dbuff_t *buf, int new_len)
65 {
66     if(buf->flags & UL_DBUFF_IS_STATIC) {
67         if(!buf->data || !buf->capacity) {
68             buf->capacity = sizeof(buf->sbuff);
69             buf->data = buf->sbuff;
70         }
71         if(new_len > buf->capacity) {
72             buf->len=0;
73             return 0;
74         }
75     }else{
76         if(new_len > UL_DBUFF_SLEN) {
77             if(new_len != buf->capacity){
78                 if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
79                     free(buf->data);
80                 }
81                 buf->data=malloc(new_len);
82                 if(!buf->data){
83                     buf->capacity=0;
84                     buf->len=0;
85                     return 0;
86                 }
87                 buf->capacity=new_len;
88             }
89         }else{
90             if((buf->data) && (buf->data != &(buf->sbuff[0]))) {
91                 free(buf->data);
92                 buf->data = &(buf->sbuff[0]);
93             }
94             buf->capacity=UL_DBUFF_SLEN;
95         }
96     }
97     buf->len = new_len;
98     memset(buf->data,0,new_len);
99     return buf->len;
100 }