Commit 3f397b0c authored by Niels Möller's avatar Niels Möller
Browse files

* src/format.c (format_decimal): New function (previously

duplicated in several places).
(ssh_format): New format %di to write decimal integers.
(write_decimal_length): Use the format_decimal() function.

* src/werror.c (werror_decimal): Use the format_decimal() function.

Rev: src/format.c:1.27
Rev: src/format.h:1.21
Rev: src/werror.c:1.29
parent 8c7de10e
......@@ -121,14 +121,15 @@ end_options:
break;
case 'i':
(void) va_arg(args, UINT32);
length += 4;
break;
#if 0
case 'd':
length += format_size_in_decimal(va_arg(args, UINT32));
break;
#endif
{
UINT32 i = va_arg(args, UINT32);
if (decimal)
length += format_size_in_decimal(i);
else
length += 4;
break;
}
case 's':
{
UINT32 l = va_arg(args, UINT32); /* String length */
......@@ -319,9 +320,17 @@ end_options:
case 'i':
{
UINT32 i = va_arg(args, UINT32);
WRITE_UINT32(buffer, i);
buffer += 4;
if (decimal)
{
unsigned length = format_size_in_decimal(i);
format_decimal(length, buffer, i);
buffer += length;
}
else
{
WRITE_UINT32(buffer, i);
buffer += 4;
}
break;
}
......@@ -529,17 +538,22 @@ unsigned format_size_in_decimal(UINT32 n)
return e+1;
}
static int write_decimal_length(UINT8 *buffer, UINT32 n)
void format_decimal(unsigned length, UINT8 *buffer, UINT32 n)
{
int length = format_size_in_decimal(n);
int i;
unsigned i;
for (i = 0; i<length; i++)
{
buffer[length - i - 1] = '0' + n % 10;
n /= 10;
}
}
static int write_decimal_length(UINT8 *buffer, UINT32 n)
{
int length = format_size_in_decimal(n);
format_decimal(length, buffer, n);
buffer[length] = ':';
return length + 1;
......
......@@ -39,8 +39,6 @@
*
* %i Insert a 32-bit integer, in network byte order
*
* %d Insert a 32-bit integer, in decimal
*
* %s Insert a string, given by a length and a pointer.
*
* %S Insert a string, given as a struct lsh_string pointer.
......@@ -65,15 +63,15 @@
* "l" (as in literal). It is applicable to the s, a, A, n and r
* specifiers, and outputs strings *without* a length field.
*
* "e" (as in expression). Formats the input string using sexp syntax;
* "d" (as in decimal). For integers, convert the integer to decimal
* digits. For strings, format the input string using sexp syntax;
* i.e. prefixed with the length in decimal.
*
* "f" (as in free). Frees the input string after it has been copied.
* Applicable to %S only.
*
* "u" (as in unsigned). Used with bignums, to use unsigned-only
* number format.
*/
* number format. */
struct lsh_string *ssh_format(const char *format, ...);
UINT32 ssh_format_length(const char *format, ...);
......@@ -89,5 +87,6 @@ struct lsh_string *format_cstring(const char *s);
struct lsh_string *make_cstring(struct lsh_string *s, int free);
unsigned format_size_in_decimal(UINT32 n);
void format_decimal(unsigned length, UINT8 *buffer, UINT32 n);
#endif /* LSH_FORMAT_H_INCLUDED */
......@@ -149,13 +149,9 @@ static void werror_decimal(UINT32 n)
{
unsigned length = format_size_in_decimal(n);
char *buffer = alloca(length);
unsigned i;
for (i = 0; i<length; i++)
{
buffer[length - i - 1] = '0' + n % 10;
n /= 10;
}
format_decimal(length, buffer, n);
werror_write(length, buffer);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment