X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=support%2FUtil%2Fdbuf_string.c;h=6a523af729e38dcc4e5b0eb7b35a53fd61d8bd14;hb=193aa0c8689791ae7d723d428ebd84e3d06fe7fd;hp=a58d555fdc79867d057b65ddffaf9125df10461b;hpb=ba3245b2350c424ced782193797186e3167d6f30;p=fw%2Fsdcc diff --git a/support/Util/dbuf_string.c b/support/Util/dbuf_string.c index a58d555f..6a523af7 100644 --- a/support/Util/dbuf_string.c +++ b/support/Util/dbuf_string.c @@ -1,6 +1,6 @@ /* dbuf_string.c - Append formatted string to the dynamic buffer - version 1.0.0, January 6th, 2007 + version 1.1.0, December 29th, 2007 Copyright (c) 2002-2007 Borut Razem @@ -33,11 +33,11 @@ */ int -dbuf_append_str(struct dbuf_s *dbuf, const char *str) +dbuf_append_str (struct dbuf_s *dbuf, const char *str) { - assert(str != NULL); + assert (str != NULL); - return dbuf_append(dbuf, str, strlen(str)); + return dbuf_append (dbuf, str, strlen(str)); } @@ -46,9 +46,9 @@ dbuf_append_str(struct dbuf_s *dbuf, const char *str) */ int -dbuf_append_char(struct dbuf_s *dbuf, char chr) +dbuf_append_char (struct dbuf_s *dbuf, char chr) { - return dbuf_append(dbuf, &chr, 1); + return dbuf_append (dbuf, &chr, 1); } /* @@ -75,62 +75,62 @@ calc_result_length (const char *format, va_list args) while (*p != '\0') { if (*p++ == '%') - { - while (strchr ("-+ #0", *p)) - ++p; - if (*p == '*') - { - ++p; - total_width += abs (va_arg (ap, int)); - } - else - total_width += strtoul (p, (char **) &p, 10); - if (*p == '.') - { - ++p; - if (*p == '*') - { - ++p; - total_width += abs (va_arg (ap, int)); - } - else - total_width += strtoul (p, (char **) &p, 10); - } - while (strchr ("hlL", *p)) - ++p; - /* Should be big enough for any format specifier except %s and floats. */ - total_width += 30; - switch (*p) - { - case 'd': - case 'i': - case 'o': - case 'u': - case 'x': - case 'X': - case 'c': - (void) va_arg (ap, int); - break; - case 'f': - case 'e': - case 'E': - case 'g': - case 'G': - (void) va_arg (ap, double); - /* Since an ieee double can have an exponent of 307, we'll - make the buffer wide enough to cover the gross case. */ - total_width += 307; - break; - case 's': - total_width += strlen (va_arg (ap, char *)); - break; - case 'p': - case 'n': - (void) va_arg (ap, char *); - break; - } - p++; - } + { + while (strchr ("-+ #0", *p)) + ++p; + if (*p == '*') + { + ++p; + total_width += abs (va_arg (ap, int)); + } + else + total_width += strtoul (p, (char **) &p, 10); + if (*p == '.') + { + ++p; + if (*p == '*') + { + ++p; + total_width += abs (va_arg (ap, int)); + } + else + total_width += strtoul (p, (char **) &p, 10); + } + while (strchr ("hlL", *p)) + ++p; + /* Should be big enough for any format specifier except %s and floats. */ + total_width += 30; + switch (*p) + { + case 'd': + case 'i': + case 'o': + case 'u': + case 'x': + case 'X': + case 'c': + (void) va_arg (ap, int); + break; + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + (void) va_arg (ap, double); + /* Since an ieee double can have an exponent of 307, we'll + make the buffer wide enough to cover the gross case. */ + total_width += 307; + break; + case 's': + total_width += strlen (va_arg (ap, char *)); + break; + case 'p': + case 'n': + (void) va_arg (ap, char *); + break; + } + p++; + } } #ifdef va_copy va_end (ap); @@ -148,21 +148,23 @@ dbuf_vprintf (struct dbuf_s *dbuf, const char *format, va_list args) { int size = calc_result_length (format, args); - assert(dbuf != NULL); - assert(dbuf->alloc != 0); - assert(dbuf->buf != NULL); + assert (dbuf != NULL); + assert (dbuf->alloc != 0); + assert (dbuf->buf != NULL); - if (_dbuf_expand(dbuf, size) != 0) { - int len = vsprintf(&(((char *)dbuf->buf)[dbuf->len]), format, args); - - if (len >= 0) { - /* if written length is greater then the calculated one, - we have a buffer overrun! */ - assert(len <= size); - dbuf->len += len; + if (0 != _dbuf_expand (dbuf, size)) + { + int len = vsprintf (&(((char *)dbuf->buf)[dbuf->len]), format, args); + + if (len >= 0) + { + /* if written length is greater then the calculated one, + we have a buffer overrun! */ + assert (len <= size); + dbuf->len += len; + } + return len; } - return len; - } return 0; } @@ -179,13 +181,46 @@ dbuf_printf (struct dbuf_s *dbuf, const char *format, ...) int len; va_start (arg, format); - len = dbuf_vprintf(dbuf, format, arg); + len = dbuf_vprintf (dbuf, format, arg); va_end (arg); return len; } +/* + * Append line from file to the dynamic buffer + */ + +size_t +dbuf_getline (struct dbuf_s *dbuf, FILE *infp) +{ + int c; + char chr; + + while ((c = getc (infp)) != '\n' && c != EOF) + { + chr = c; + + dbuf_append (dbuf, &chr, 1); + } + + /* add trailing NL */ + if (c == '\n') + { + chr = c; + + dbuf_append (dbuf, &chr, 1); + } + + /* terminate the line without increasing the length */ + if (0 != _dbuf_expand (dbuf, 1)) + ((char *)dbuf->buf)[dbuf->len] = '\0'; + + return dbuf_get_length (dbuf); +} + + /* * Write dynamic buffer to the file. */ @@ -193,7 +228,7 @@ dbuf_printf (struct dbuf_s *dbuf, const char *format, ...) void dbuf_write (struct dbuf_s *dbuf, FILE *dest) { - fwrite(dbuf_get_buf(dbuf), 1, dbuf_get_length(dbuf), dest); + fwrite (dbuf_get_buf (dbuf), 1, dbuf_get_length (dbuf), dest); } @@ -206,5 +241,5 @@ dbuf_write_and_destroy (struct dbuf_s *dbuf, FILE *dest) { dbuf_write (dbuf, dest); - dbuf_destroy(dbuf); + dbuf_destroy (dbuf); }