better debug output
authorNiki Roo <niki@nikiroo.be>
Sun, 23 Jun 2024 12:19:22 +0000 (14:19 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 23 Jun 2024 12:19:22 +0000 (14:19 +0200)
src/cbook/cbook_csv.c
src/cutils

index 9348f7d102d03b18690ec4a2010a96421038bbcf..32e51f2538f72a2b2fa2da9b9f68929b3e1fbd97 100755 (executable)
@@ -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);
index 201682e2328086431c86a36358ba5569d51048ad..8a3dcb4e6495ca5a0fd8846a4e4dc6ed52e90136 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 201682e2328086431c86a36358ba5569d51048ad
+Subproject commit 8a3dcb4e6495ca5a0fd8846a4e4dc6ed52e90136