Add intelligent output format selection for EXI encoding
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user