From: Niki Roo Date: Sun, 23 Jun 2024 12:19:22 +0000 (+0200) Subject: better debug output X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=79d0d13e37741000ea463947daba1ffedb509599;p=cbook.git better debug output --- diff --git a/src/cbook/cbook_csv.c b/src/cbook/cbook_csv.c index 9348f7d..32e51f2 100755 --- a/src/cbook/cbook_csv.c +++ b/src/cbook/cbook_csv.c @@ -154,15 +154,6 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { break; } - if (book->debug) { - fprintf(stderr, "\n"); - for (size_t i = 0 ; i < field->bytes ; i++) { - unsigned char byte = (unsigned char)data[i]; - fprintf(stderr, "%s -- Byte %zu = 0x%02X (%c)\n", - field->name, i+1, byte, byte); - } - } - switch(field->type) { case CBOOK_FMT_CHAR: fwrite(data, 1, field->bytes, outfile); @@ -189,7 +180,7 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { case CBOOK_FMT_UDECIMAL: ; cstring_t *str = new_cstring(); - int negative = 0; + int positive = 1; int last = 0; size_t dec_idx = ((field->size) - (field->decimals)); @@ -198,6 +189,8 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { ss++; int take_first = (ss % 2 == 0); + if (book->debug) + fprintf(stderr, "\n"); for (size_t i = 0 ; i < field->bytes ; i++) { unsigned char byte = (unsigned char)data[i]; unsigned char x1 = (byte & 0xF0) >> 4; @@ -207,23 +200,29 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { last = 1; if (last && field->type == CBOOK_FMT_DECIMAL) { - if (x2 < 10) { + switch(x2) { + case 10: case 12: case 14: case 15: + positive = 1; break; + case 11: case 13: + positive = 0; break; + default: + positive = -1; break; + } + + if (positive < 0) { char bit[100]; sprintf(bit, "%zu", i+1); char byte2[3]; sprintf(byte2, "%02X", byte); cstring_t *tmp = new_cstring(); cstring_add(tmp, - "Invalid DECIMAL value: "); + "Invalid DECIMAL sign value: "); cstring_add(tmp, "Byte "); cstring_add(tmp, bit); cstring_add(tmp, " = 0x"); cstring_add(tmp, byte2); book->err_mess = cstring_convert(tmp); } - - if ((x2 == 11) || (x2 == 13)) - negative = 1; } if ((x1 > 9) || (!last && x2 > 9)) { @@ -250,9 +249,32 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { if (str->length == dec_idx) cstring_add_car(str, '.'); } + + if (book->debug) { + if ((x1 >= 0) && (x1 <= 9)) + x1 = '0' + x1; + else + x1 = '?'; + if ((x2 >= 0) && (x2 <= 9)) + x2 = '0' + x2; + else + x2 = '?'; + if (last && field->type == CBOOK_FMT_DECIMAL) { + if (positive == 1) x2 = '+'; + else if(positive == 0) x2 = '-'; + else x2 = '!'; + } + fprintf(stderr, "%s -- Byte %zu = " + "0x%02X : %.3i (%c) -> (%c,%c)\n", + field->name, i+1, byte, byte, + (byte>=32 && byte<=126) ? byte : '?', + x1, x2 + ); + } + } - if (negative) + if (!positive) fwrite("-", 1, 1, outfile); fwrite(str->string, 1, str->length, outfile); @@ -260,6 +282,19 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { break; case CBOOK_FMT_PICTURE: fwrite(data, 1, field->bytes, outfile); + if (book->debug) { + fprintf(stderr, "\n"); + for (size_t i = 0 ; i < field->bytes ; i++) { + unsigned char byte = (unsigned char)data[i]; + fprintf(stderr, "%s -- Byte %zu = " + "0x%02X : %.3i " + "(%c)\n", + field->name, i, + byte, byte, + (byte>=32 && byte<=126) ? byte : '?' + ); + } + } break; //TODO: the rest @@ -267,6 +302,19 @@ void one_field(FILE *outfile, book_t *book, line_t *field, char *data) { case CBOOK_FMT_UFIXED_BINARY: default: ; + if (book->debug) { + fprintf(stderr, "\n"); + for (size_t i = 0 ; i < field->bytes ; i++) { + unsigned char byte = (unsigned char)data[i]; + fprintf(stderr, "%s -- Byte %zu = " + "0x%02X : %.3i " + "(%c)\n", + field->name, i, + byte, byte, + (byte>=32 && byte<=126) ? byte : '?' + ); + } + } cstring_t *tmp = new_cstring(); cstring_add(tmp, "FMT_"); cstring_add_car(tmp, '0' + field->type); diff --git a/src/cutils b/src/cutils index 201682e..8a3dcb4 160000 --- a/src/cutils +++ b/src/cutils @@ -1 +1 @@ -Subproject commit 201682e2328086431c86a36358ba5569d51048ad +Subproject commit 8a3dcb4e6495ca5a0fd8846a4e4dc6ed52e90136