diff options
author | Guy Harris <guy@alum.mit.edu> | 2004-10-29 00:39:56 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2004-10-29 00:39:56 +0000 |
commit | 898c26a82fc175d34622d3a6100af6077c2c6c07 (patch) | |
tree | 9a08f9691af8a3ad8bf54a6349db0e2ee457412c /epan/strutil.c | |
parent | e09e12621adefe25905804de8ba7e2687c2c72f2 (diff) |
Have the usual three separate buffers for "format_text()", so that it
can be used multiple times in a single formatting call.
svn path=/trunk/; revision=12428
Diffstat (limited to 'epan/strutil.c')
-rw-r--r-- | epan/strutil.c | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/epan/strutil.c b/epan/strutil.c index 7e78ba5233..9059ef4a07 100644 --- a/epan/strutil.c +++ b/epan/strutil.c @@ -157,19 +157,22 @@ get_token_len(const guchar *linep, const guchar *lineend, gchar * format_text(const guchar *string, int len) { - static gchar *fmtbuf; - static int fmtbuf_len; + static gchar *fmtbuf[3]; + static int fmtbuf_len[3]; + static int idx; int column; const guchar *stringend = string + len; guchar c; int i; + idx = (idx + 1) % 3; + /* * Allocate the buffer if it's not already allocated. */ - if (fmtbuf == NULL) { - fmtbuf = g_malloc(INITIAL_FMTBUF_SIZE); - fmtbuf_len = INITIAL_FMTBUF_SIZE; + if (fmtbuf[idx] == NULL) { + fmtbuf[idx] = g_malloc(INITIAL_FMTBUF_SIZE); + fmtbuf_len[idx] = INITIAL_FMTBUF_SIZE; } column = 0; while (string < stringend) { @@ -178,82 +181,82 @@ format_text(const guchar *string, int len) * a backslash plus 3 octal digits (which is the most it can * expand to), and also enough room for a terminating '\0'? */ - if (column+3+1 >= fmtbuf_len) { + if (column+3+1 >= fmtbuf_len[idx]) { /* * Double the buffer's size if it's not big enough. * The size of the buffer starts at 128, so doubling its size * adds at least another 128 bytes, which is more than enough * for one more character plus a terminating '\0'. */ - fmtbuf_len = fmtbuf_len * 2; - fmtbuf = g_realloc(fmtbuf, fmtbuf_len); + fmtbuf_len[idx] = fmtbuf_len[idx] * 2; + fmtbuf[idx] = g_realloc(fmtbuf[idx], fmtbuf_len[idx]); } c = *string++; if (isprint(c)) { - fmtbuf[column] = c; + fmtbuf[idx][column] = c; column++; } else { - fmtbuf[column] = '\\'; + fmtbuf[idx][column] = '\\'; column++; switch (c) { case '\\': - fmtbuf[column] = '\\'; + fmtbuf[idx][column] = '\\'; column++; break; case '\a': - fmtbuf[column] = 'a'; + fmtbuf[idx][column] = 'a'; column++; break; case '\b': - fmtbuf[column] = 'b'; + fmtbuf[idx][column] = 'b'; column++; break; case '\f': - fmtbuf[column] = 'f'; + fmtbuf[idx][column] = 'f'; column++; break; case '\n': - fmtbuf[column] = 'n'; + fmtbuf[idx][column] = 'n'; column++; break; case '\r': - fmtbuf[column] = 'r'; + fmtbuf[idx][column] = 'r'; column++; break; case '\t': - fmtbuf[column] = 't'; + fmtbuf[idx][column] = 't'; column++; break; case '\v': - fmtbuf[column] = 'v'; + fmtbuf[idx][column] = 'v'; column++; break; default: i = (c>>6)&03; - fmtbuf[column] = i + '0'; + fmtbuf[idx][column] = i + '0'; column++; i = (c>>3)&07; - fmtbuf[column] = i + '0'; + fmtbuf[idx][column] = i + '0'; column++; i = (c>>0)&07; - fmtbuf[column] = i + '0'; + fmtbuf[idx][column] = i + '0'; column++; break; } } } - fmtbuf[column] = '\0'; - return fmtbuf; + fmtbuf[idx][column] = '\0'; + return fmtbuf[idx]; } /* Max string length for displaying byte string. */ |