Skip to content

Commit

Permalink
bprint: add av_bprint_init_for_buffer().
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas George committed Jun 7, 2012
1 parent 9148ae5 commit b3e2bb0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libavutil/bprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
av_bprint_alloc(buf, size_init - 1);
}

void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size)
{
buf->str = buffer;
buf->len = 0;
buf->size = size;
buf->size_max = size;
*buf->str = 0;
}

void av_bprintf(AVBPrint *buf, const char *fmt, ...)
{
unsigned room;
Expand Down Expand Up @@ -178,6 +187,7 @@ static void bprint_pascal(AVBPrint *b, unsigned size)
int main(void)
{
AVBPrint b;
char buf[256];

av_bprint_init(&b, 0, -1);
bprint_pascal(&b, 5);
Expand Down Expand Up @@ -208,6 +218,10 @@ int main(void)
bprint_pascal(&b, 25);
printf("Long text count only buffer: %zu/%u\n", strlen(b.str), b.len);

av_bprint_init_for_buffer(&b, buf, sizeof(buf));
bprint_pascal(&b, 25);
printf("Long text count only buffer: %zu/%u\n", strlen(buf), b.len);

return 0;
}

Expand Down
11 changes: 11 additions & 0 deletions libavutil/bprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max);
#define AV_BPRINT_SIZE_AUTOMATIC 1
#define AV_BPRINT_SIZE_COUNT_ONLY 0

/**
* Init a print buffer using a pre-existing buffer.
*
* The buffer will not be reallocated.
*
* @param buf buffer structure to init
* @param buffer byte buffer to use for the string data
* @param size size of buffer
*/
void av_bprint_init_for_buffer(AVBPrint *buf, char *buffer, unsigned size);

/**
* Append a formated string to a print buffer.
*/
Expand Down

0 comments on commit b3e2bb0

Please sign in to comment.