1 /***************************************************************************
2 * Copyright (C) 2009 By Duane Ellis *
3 * openocd@duaneellis.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
29 // buflen is alway "+1" bigger then
30 // what is shown here, the +1 is for
31 // the NULL string terminator
32 #define DEFAULT_BUFSIZE 100
33 size_t maxlen
; // allocated size
34 size_t curlen
; // where we are inserting at
40 #define space_avail( pBuf ) (pBuf->maxlen - pBuf->curlen)
41 #define dataend( pBuf ) ( ((char *)(pBuf->buf)) + pBuf->curlen )
44 membuf_len( struct membuf
*pBuf
)
50 membuf_datapointer( struct membuf
*pBuf
)
52 return ((void *)(pBuf
->buf
));
56 membuf_strtok( struct membuf
*pBuf
, const char *sep
, void **pLast
)
59 pBuf
->_strtoklast
= NULL
;
61 return strtok_r( ((char *)(pBuf
->buf
)), sep
, &(pBuf
->_strtoklast
) );
64 pBuf
= *((struct membuf
**)(pLast
));
65 return strtok_r( NULL
, sep
, &(pBuf
->_strtoklast
) );
74 // by default - parameters are zero.
77 pBuf
= calloc( 1, sizeof(*pBuf
) );
79 // we *ALWAYS* allocate +1 for null terminator.
80 pBuf
->buf
= calloc( DEFAULT_BUFSIZE
+1, sizeof(char));
81 if ( pBuf
->buf
== NULL
){
85 pBuf
->maxlen
= DEFAULT_BUFSIZE
;
93 membuf_grow( struct membuf
*pBuf
, int n
)
98 // this is a *SIGNED* value
99 newsize
= ((int)(pBuf
->maxlen
)) + n
;
101 // do not go negative, or too small
102 if ( newsize
< DEFAULT_BUFSIZE
){
103 newsize
= DEFAULT_BUFSIZE
;
106 // always alloc +1 for the null terminator
107 vp
= realloc( pBuf
->buf
, newsize
+1 );
110 pBuf
->maxlen
= newsize
;
118 void membuf_reset( struct membuf
*pBuf
)
124 void membuf_delete( struct membuf
*pBuf
)
128 // wack data so it cannot be reused
129 memset(pBuf
->buf
,0,pBuf
->maxlen
);
132 // wack dat so it cannot be reused
133 memset(pBuf
,0,sizeof(pBuf
));
139 membuf_sprintf( struct membuf
*pBuf
, const char *fmt
, ... )
144 r
= membuf_vsprintf( pBuf
, fmt
, ap
);
150 membuf_vsprintf( struct membuf
*pBuf
, const char *fmt
, va_list ap
)
159 sa
= space_avail(pBuf
);
162 r
= vsnprintf( dataend( pBuf
),
166 if ( (r
> 0) && (((size_t)(r
)) < sa
) ){
168 pBuf
->curlen
+= ((size_t)(r
));
169 // remember: We always alloc'ed +1
170 // so this does not overflow
171 ((char *)(pBuf
->buf
))[ pBuf
->curlen
] = 0;
178 // Option(A) format error
179 // Option(B) glibc2.0 bug
181 r
= (4 * DEFAULT_BUFSIZE
);
184 // don't do this again
190 pBuf
= membuf_grow( pBuf
, r
);
201 membuf_strcat( struct membuf
*pBuf
, const char *pStr
)
203 return membuf_append( pBuf
, pStr
, strlen( pStr
) );
207 membuf_append( struct membuf
*pBuf
, const void *pData
, size_t len
)
212 // how much room is there?
213 sa
= space_avail( pBuf
);
217 // if not, how much do we need?
218 r
= ((int)(sa
- len
));
220 pBuf
= membuf_grow( pBuf
, r
);
227 memcpy( dataend(pBuf
),