Fix code style
This commit is contained in:
parent
632ab90161
commit
4e3c7efa5f
@ -7,7 +7,9 @@
|
||||
static StrFormatResult
|
||||
write_str(void *user_data, const char *data, unsigned int len)
|
||||
{
|
||||
if (len > 0) dbg_send_bytes((unsigned char*)data, len);
|
||||
if(len > 0) {
|
||||
dbg_send_bytes((unsigned char *)data, len);
|
||||
}
|
||||
return STRFORMAT_OK;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
@ -21,7 +21,8 @@ putc(int c, FILE *f)
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
__sp(struct _reent *_ptr, int c, FILE *_p) {
|
||||
__sp(struct _reent *_ptr, int c, FILE *_p)
|
||||
{
|
||||
dbg_putchar(c);
|
||||
return c;
|
||||
}
|
||||
|
@ -3,8 +3,7 @@
|
||||
#include <strformat.h>
|
||||
#include <string.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct FmtBuffer
|
||||
{
|
||||
struct FmtBuffer {
|
||||
char *pos;
|
||||
size_t left;
|
||||
};
|
||||
@ -24,7 +23,8 @@ buffer_str(void *user_data, const char *data, unsigned int len)
|
||||
return STRFORMAT_OK;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int snprintf(char *str, size_t size, const char *format, ...)
|
||||
int
|
||||
snprintf(char *str, size_t size, const char *format, ...)
|
||||
{
|
||||
int res;
|
||||
va_list ap;
|
||||
@ -34,7 +34,8 @@ int snprintf(char *str, size_t size, const char *format, ...)
|
||||
return res;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
int
|
||||
vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
{
|
||||
struct FmtBuffer buffer;
|
||||
StrFormatContext ctxt;
|
||||
|
@ -2,23 +2,23 @@
|
||||
#include <strformat.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define HAVE_DOUBLE
|
||||
|
||||
#define HAVE_LONGLONG
|
||||
|
||||
#ifndef LARGEST_SIGNED
|
||||
#ifdef HAVE_LONGLONG
|
||||
#define LARGEST_SIGNED long long int
|
||||
#else
|
||||
#define LARGEST_UNSIGNED long int
|
||||
#endif
|
||||
#endif
|
||||
#endif /* HAVE_LONGLONG */
|
||||
#endif /* LARGEST_SIGNED */
|
||||
|
||||
#ifndef LARGEST_UNSIGNED
|
||||
#ifdef HAVE_LONGLONG
|
||||
#define LARGEST_UNSIGNED unsigned long long int
|
||||
#else
|
||||
#define LARGEST_UNSIGNED unsigned long int
|
||||
#endif
|
||||
#endif
|
||||
#endif /* HAVE_LONGLONG */
|
||||
#endif /* LARGEST_UNSIGNED */
|
||||
|
||||
#ifndef POINTER_INT
|
||||
#define POINTER_INT unsigned long
|
||||
@ -27,13 +27,13 @@
|
||||
typedef unsigned int FormatFlags;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define MAKE_MASK(shift, size) (((1 << size) - 1) << (shift))
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define JUSTIFY_SHIFT 0
|
||||
#define JUSTIFY_SIZE 1
|
||||
#define JUSTIFY_RIGHT 0x0000
|
||||
#define JUSTIFY_LEFT 0x0001
|
||||
#define JUSTIFY_MASK MAKE_MASK(JUSTIFY_SHIFT, JUSTIFY_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* How a positive number is prefixed */
|
||||
#define POSITIVE_SHIFT (JUSTIFY_SHIFT + JUSTIFY_SIZE)
|
||||
#define POSITIVE_NONE (0x0000 << POSITIVE_SHIFT)
|
||||
@ -42,17 +42,16 @@ typedef unsigned int FormatFlags;
|
||||
#define POSITIVE_MASK MAKE_MASK(POSITIVE_SHIFT, POSITIVE_SIZE)
|
||||
|
||||
#define POSITIVE_SIZE 2
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define ALTERNATE_FORM_SHIFT (POSITIVE_SHIFT + POSITIVE_SIZE)
|
||||
#define ALTERNATE_FORM_SIZE 1
|
||||
#define ALTERNATE_FORM (0x0001 << ALTERNATE_FORM_SHIFT)
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define PAD_SHIFT (ALTERNATE_FORM_SHIFT + ALTERNATE_FORM_SIZE)
|
||||
#define PAD_SIZE 1
|
||||
#define PAD_SPACE (0x0000 << PAD_SHIFT)
|
||||
#define PAD_ZERO (0x0001 << PAD_SHIFT)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define SIZE_SHIFT (PAD_SHIFT + PAD_SIZE)
|
||||
#define SIZE_SIZE 3
|
||||
#define SIZE_CHAR (0x0001 << SIZE_SHIFT)
|
||||
@ -61,7 +60,7 @@ typedef unsigned int FormatFlags;
|
||||
#define SIZE_LONG (0x0003 << SIZE_SHIFT)
|
||||
#define SIZE_LONGLONG (0x0004 << SIZE_SHIFT)
|
||||
#define SIZE_MASK MAKE_MASK(SIZE_SHIFT, SIZE_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define CONV_SHIFT (SIZE_SHIFT + SIZE_SIZE)
|
||||
#define CONV_SIZE 3
|
||||
#define CONV_INTEGER (0x0001 << CONV_SHIFT)
|
||||
@ -72,26 +71,26 @@ typedef unsigned int FormatFlags;
|
||||
#define CONV_PERCENT (0x0006 << CONV_SHIFT)
|
||||
#define CONV_WRITTEN (0x0007 << CONV_SHIFT)
|
||||
#define CONV_MASK MAKE_MASK(CONV_SHIFT, CONV_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define RADIX_SHIFT (CONV_SHIFT + CONV_SIZE)
|
||||
#define RADIX_SIZE 2
|
||||
#define RADIX_DECIMAL (0x0001 << RADIX_SHIFT)
|
||||
#define RADIX_OCTAL (0x0002 << RADIX_SHIFT)
|
||||
#define RADIX_HEX (0x0003 << RADIX_SHIFT)
|
||||
#define RADIX_MASK MAKE_MASK(RADIX_SHIFT, RADIX_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define SIGNED_SHIFT (RADIX_SHIFT + RADIX_SIZE)
|
||||
#define SIGNED_SIZE 1
|
||||
#define SIGNED_NO (0x0000 << SIGNED_SHIFT)
|
||||
#define SIGNED_YES (0x0001 << SIGNED_SHIFT)
|
||||
#define SIGNED_MASK MAKE_MASK(SIGNED_SHIFT, SIGNED_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define CAPS_SHIFT (SIGNED_SHIFT + SIGNED_SIZE)
|
||||
#define CAPS_SIZE 1
|
||||
#define CAPS_NO (0x0000 << CAPS_SHIFT)
|
||||
#define CAPS_YES (0x0001 << CAPS_SHIFT)
|
||||
#define CAPS_MASK MAKE_MASK(CAPS_SHIFT, CAPS_SIZE)
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define FLOAT_SHIFT (CAPS_SHIFT + CAPS_SIZE)
|
||||
#define FLOAT_SIZE 2
|
||||
#define FLOAT_NORMAL (0x0000 << FLOAT_SHIFT)
|
||||
@ -106,6 +105,7 @@ parse_flags(const char **posp)
|
||||
{
|
||||
FormatFlags flags = 0;
|
||||
const char *pos = *posp;
|
||||
|
||||
while(1) {
|
||||
switch(*pos) {
|
||||
case '-':
|
||||
@ -127,9 +127,9 @@ parse_flags(const char **posp)
|
||||
*posp = pos;
|
||||
return flags;
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static unsigned int
|
||||
@ -138,11 +138,14 @@ parse_uint(const char **posp)
|
||||
unsigned v = 0;
|
||||
const char *pos = *posp;
|
||||
char ch;
|
||||
|
||||
while((ch = *pos) >= '0' && ch <= '9') {
|
||||
v = v * 10 + (ch - '0');
|
||||
pos++;
|
||||
}
|
||||
|
||||
*posp = pos;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@ -158,12 +161,15 @@ output_uint_decimal(char **posp, LARGEST_UNSIGNED v)
|
||||
{
|
||||
unsigned int len;
|
||||
char *pos = *posp;
|
||||
|
||||
while(v > 0) {
|
||||
*--pos = (v % 10) + '0';
|
||||
v /= 10;
|
||||
}
|
||||
|
||||
len = *posp - pos;
|
||||
*posp = pos;
|
||||
|
||||
return len;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -173,12 +179,15 @@ output_uint_hex(char **posp, LARGEST_UNSIGNED v, unsigned int flags)
|
||||
unsigned int len;
|
||||
const char *hex = (flags & CAPS_YES) ? "0123456789ABCDEF" : "0123456789abcdef";
|
||||
char *pos = *posp;
|
||||
|
||||
while(v > 0) {
|
||||
*--pos = hex[(v % 16)];
|
||||
v /= 16;
|
||||
}
|
||||
|
||||
len = *posp - pos;
|
||||
*posp = pos;
|
||||
|
||||
return len;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -187,12 +196,15 @@ output_uint_octal(char **posp, LARGEST_UNSIGNED v)
|
||||
{
|
||||
unsigned int len;
|
||||
char *pos = *posp;
|
||||
|
||||
while(v > 0) {
|
||||
*--pos = (v % 8) + '0';
|
||||
v /= 8;
|
||||
}
|
||||
|
||||
len = *posp - pos;
|
||||
*posp = pos;
|
||||
|
||||
return len;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -201,12 +213,19 @@ fill_space(const StrFormatContext *ctxt, unsigned int len)
|
||||
{
|
||||
StrFormatResult res;
|
||||
static const char buffer[16] = " ";
|
||||
|
||||
while(len > 16) {
|
||||
res = ctxt->write_str(ctxt->user_data, buffer, 16);
|
||||
if (res != STRFORMAT_OK) return res;
|
||||
if(res != STRFORMAT_OK) {
|
||||
return res;
|
||||
}
|
||||
len -= 16;
|
||||
}
|
||||
if (len == 0) return STRFORMAT_OK;
|
||||
|
||||
if(len == 0) {
|
||||
return STRFORMAT_OK;
|
||||
}
|
||||
|
||||
return ctxt->write_str(ctxt->user_data, buffer, len);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -215,12 +234,18 @@ fill_zero(const StrFormatContext *ctxt, unsigned int len)
|
||||
{
|
||||
StrFormatResult res;
|
||||
static const char buffer[16] = "0000000000000000";
|
||||
|
||||
while(len > 16) {
|
||||
res = ctxt->write_str(ctxt->user_data, buffer, 16);
|
||||
if (res != STRFORMAT_OK) return res;
|
||||
if(res != STRFORMAT_OK) {
|
||||
return res;
|
||||
}
|
||||
len -= 16;
|
||||
}
|
||||
if (len == 0) return STRFORMAT_OK;
|
||||
|
||||
if(len == 0) {
|
||||
return STRFORMAT_OK;
|
||||
}
|
||||
return ctxt->write_str(ctxt->user_data, buffer, len);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@ -242,26 +267,35 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
{
|
||||
unsigned int written = 0;
|
||||
const char *pos = format;
|
||||
|
||||
while(*pos != '\0') {
|
||||
FormatFlags flags;
|
||||
unsigned int minwidth = 0;
|
||||
int precision = -1; /* Negative means no precision */
|
||||
char ch;
|
||||
const char *start = pos;
|
||||
while( (ch = *pos) != '\0' && ch != '%') pos++;
|
||||
|
||||
while((ch = *pos) != '\0' && ch != '%') {
|
||||
pos++;
|
||||
}
|
||||
|
||||
if(pos != start) {
|
||||
CHECKCB(ctxt->write_str(ctxt->user_data, start, pos - start));
|
||||
written += pos - start;
|
||||
}
|
||||
|
||||
if(*pos == '\0') {
|
||||
va_end(ap);
|
||||
return written;
|
||||
}
|
||||
|
||||
pos++;
|
||||
|
||||
if(*pos == '\0') {
|
||||
va_end(ap);
|
||||
return written;
|
||||
}
|
||||
|
||||
flags = parse_flags(&pos);
|
||||
|
||||
/* parse width */
|
||||
@ -269,18 +303,21 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
minwidth = parse_uint(&pos);
|
||||
} else if(*pos == '*') {
|
||||
int w = va_arg(ap, int);
|
||||
|
||||
if(w < 0) {
|
||||
flags |= JUSTIFY_LEFT;
|
||||
minwidth = w;
|
||||
} else {
|
||||
minwidth = w;
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
|
||||
/* parse precision */
|
||||
if(*pos == '.') {
|
||||
pos++;
|
||||
|
||||
if(*pos >= '0' && *pos <= '9') {
|
||||
precision = parse_uint(&pos);
|
||||
} else if(*pos == '*') {
|
||||
@ -288,8 +325,10 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
precision = va_arg(ap, int);
|
||||
}
|
||||
}
|
||||
|
||||
if(*pos == 'l') {
|
||||
pos++;
|
||||
|
||||
if(*pos == 'l') {
|
||||
flags |= SIZE_LONGLONG;
|
||||
pos++;
|
||||
@ -298,6 +337,7 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
}
|
||||
} else if(*pos == 'h') {
|
||||
pos++;
|
||||
|
||||
if(*pos == 'h') {
|
||||
flags |= SIZE_CHAR;
|
||||
pos++;
|
||||
@ -370,6 +410,7 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
return written;
|
||||
}
|
||||
pos++;
|
||||
|
||||
switch(flags & CONV_MASK) {
|
||||
case CONV_PERCENT:
|
||||
CHECKCB(ctxt->write_str(ctxt->user_data, "%", 1));
|
||||
@ -389,8 +430,11 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
LARGEST_UNSIGNED uvalue = 0;
|
||||
int negative = 0;
|
||||
|
||||
if (precision < 0) precision = 1;
|
||||
else flags &= ~PAD_ZERO;
|
||||
if(precision < 0) {
|
||||
precision = 1;
|
||||
} else {
|
||||
flags &= ~PAD_ZERO;
|
||||
}
|
||||
|
||||
if(flags & SIGNED_YES) {
|
||||
/* signed integers */
|
||||
@ -465,7 +509,9 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
precision_fill = (precision > conv_len) ? precision - conv_len : 0;
|
||||
if((flags & (RADIX_MASK | ALTERNATE_FORM))
|
||||
== (RADIX_OCTAL | ALTERNATE_FORM)) {
|
||||
if (precision_fill < 1) precision_fill = 1;
|
||||
if(precision_fill < 1) {
|
||||
precision_fill = 1;
|
||||
}
|
||||
}
|
||||
|
||||
width += precision_fill;
|
||||
@ -511,8 +557,9 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
}
|
||||
}
|
||||
|
||||
if (prefix_len > 0)
|
||||
if(prefix_len > 0) {
|
||||
CHECKCB(ctxt->write_str(ctxt->user_data, prefix, prefix_len));
|
||||
}
|
||||
written += prefix_len;
|
||||
|
||||
CHECKCB(fill_zero(ctxt, precision_fill));
|
||||
@ -532,6 +579,7 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
unsigned int field_fill;
|
||||
unsigned int len;
|
||||
char *str = va_arg(ap, char *);
|
||||
|
||||
if(str) {
|
||||
char *pos = str;
|
||||
while(*pos != '\0') pos++;
|
||||
@ -540,13 +588,20 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
str = "(null)";
|
||||
len = 6;
|
||||
}
|
||||
if (precision >= 0 && precision < len) len = precision;
|
||||
|
||||
if(precision >= 0 && precision < len) {
|
||||
len = precision;
|
||||
}
|
||||
|
||||
field_fill = (minwidth > len) ? minwidth - len : 0;
|
||||
|
||||
if((flags & JUSTIFY_MASK) == JUSTIFY_RIGHT) {
|
||||
CHECKCB(fill_space(ctxt, field_fill));
|
||||
}
|
||||
|
||||
CHECKCB(ctxt->write_str(ctxt->user_data, str, len));
|
||||
written += len;
|
||||
|
||||
if((flags & JUSTIFY_MASK) == JUSTIFY_LEFT) {
|
||||
CHECKCB(fill_space(ctxt, field_fill));
|
||||
}
|
||||
@ -563,10 +618,12 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
unsigned int field_fill;
|
||||
|
||||
conv_len = output_uint_hex(&conv_pos, uvalue, flags);
|
||||
|
||||
if(conv_len == 0) {
|
||||
*--conv_pos = '0';
|
||||
conv_len++;
|
||||
}
|
||||
|
||||
*--conv_pos = 'x';
|
||||
*--conv_pos = '0';
|
||||
*--conv_pos = '#';
|
||||
@ -584,6 +641,7 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
if((flags & JUSTIFY_MASK) == JUSTIFY_LEFT) {
|
||||
CHECKCB(fill_space(ctxt, field_fill));
|
||||
}
|
||||
|
||||
written += field_fill;
|
||||
}
|
||||
break;
|
||||
@ -591,6 +649,7 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
{
|
||||
char ch = va_arg(ap, int);
|
||||
unsigned int field_fill = (minwidth > 1) ? minwidth - 1 : 0;
|
||||
|
||||
if((flags & JUSTIFY_MASK) == JUSTIFY_RIGHT) {
|
||||
CHECKCB(fill_space(ctxt, field_fill));
|
||||
written += field_fill;
|
||||
@ -611,7 +670,6 @@ format_str_v(const StrFormatContext *ctxt, const char *format, va_list ap)
|
||||
*p = written;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,8 +12,7 @@ typedef unsigned int StrFormatResult;
|
||||
/* The data argument may only be considered valid during the function call */
|
||||
typedef StrFormatResult (*StrFormatWrite)(void *user_data, const char *data, unsigned int len);
|
||||
|
||||
typedef struct _StrFormatContext
|
||||
{
|
||||
typedef struct _StrFormatContext {
|
||||
StrFormatWrite write_str;
|
||||
void *user_data;
|
||||
} StrFormatContext;
|
||||
|
Loading…
Reference in New Issue
Block a user