From 2c82751a1a72b9986471ae1408cbd2dad5ce8972 Mon Sep 17 00:00:00 2001 From: gram Date: Wed, 10 Sep 2025 01:51:51 +0900 Subject: [PATCH] Add intelligent output format selection for EXI encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Terminal output: Display hex string for easy copying/viewing - File redirection: Write binary data for proper file creation - Auto-detect output destination using fstat() and isatty() - Clean terminal display without debug messages - Support both viewing (hex) and file creation (binary) workflows Usage examples: - ./enhanced_exi_viewer -encode file.xml # Shows hex string - ./enhanced_exi_viewer -encode file.xml > out.exi # Creates binary file 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- enhanced_exi_viewer.c | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/enhanced_exi_viewer.c b/enhanced_exi_viewer.c index fdf3ae4..4298054 100644 --- a/enhanced_exi_viewer.c +++ b/enhanced_exi_viewer.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include /* EXI codec headers */ #include "iso1EXIDatatypes.h" @@ -997,9 +999,14 @@ int main(int argc, char *argv[]) { free(xml_content); return -1; } - fprintf(stderr, "XML parsing successful\\n"); - fprintf(stderr, "SessionID length: %d\\n", iso1Doc.V2G_Message.Header.SessionID.bytesLen); - fprintf(stderr, "CurrentDemandReq_isUsed: %s\\n", iso1Doc.V2G_Message.Body.CurrentDemandReq_isUsed ? "true" : "false"); + // Show encoding info only when redirecting to file (keeps terminal output clean) + struct stat stat_buf_debug; + int is_file_redirect = (fstat(fileno(stdout), &stat_buf_debug) == 0 && S_ISREG(stat_buf_debug.st_mode)); + if (is_file_redirect) { + fprintf(stderr, "XML parsing successful\\n"); + fprintf(stderr, "SessionID length: %d\\n", iso1Doc.V2G_Message.Header.SessionID.bytesLen); + fprintf(stderr, "CurrentDemandReq_isUsed: %s\\n", iso1Doc.V2G_Message.Body.CurrentDemandReq_isUsed ? "true" : "false"); + } free(xml_content); @@ -1017,8 +1024,29 @@ int main(int argc, char *argv[]) { return -1; } - // Write EXI data to stdout (binary) - fwrite(buffer, 1, pos, stdout); + // Check if output is redirected (for Windows/MINGW compatibility) + // Use environment variable approach or check for redirection + char* term = getenv("TERM"); + int use_hex_output = (term != NULL) || isatty(fileno(stdout)); + + // Also check if user specifically wants binary output by checking for redirection + struct stat stat_buf; + if (fstat(fileno(stdout), &stat_buf) == 0 && S_ISREG(stat_buf.st_mode)) { + use_hex_output = 0; // Regular file, use binary + } + + if (use_hex_output) { + // Terminal output: show hex string + printf("Encoded EXI data (%zu bytes):\n", pos); + for(size_t i = 0; i < pos; i++) { + printf("%02X", buffer[i]); + if ((i + 1) % 32 == 0) printf("\n"); + } + if (pos % 32 != 0) printf("\n"); + } else { + // Redirected output: write binary data + fwrite(buffer, 1, pos, stdout); + } return 0; }