diff --git a/xwords4/common/strutils.c b/xwords4/common/strutils.c index 1e24fb6b1..c8c5e6320 100644 --- a/xwords4/common/strutils.c +++ b/xwords4/common/strutils.c @@ -202,6 +202,55 @@ randIntArray( XP_U16* rnums, XP_U16 count ) return changed; } /* randIntArray */ +#ifdef DEBUG +#define NUM_PER_LINE 8 +void +log_hex( const XP_U8* memp, XP_U16 len, const char* tag ) +{ + const char* hex = "0123456789ABCDEF"; + XP_U16 i, j; + XP_U16 offset = 0; + + while ( offset < len ) { + XP_UCHAR buf[128]; + XP_UCHAR vals[NUM_PER_LINE*3]; + XP_UCHAR* valsp = vals; + XP_UCHAR chars[NUM_PER_LINE+1]; + XP_UCHAR* charsp = chars; + XP_U16 oldOffset = offset; + + for ( i = 0; i < NUM_PER_LINE && offset < len; ++i ) { + XP_U8 byte = memp[offset]; + for ( j = 0; j < 2; ++j ) { + *valsp++ = hex[byte & 0x0F]; + byte >>= 4; + } + *valsp++ = ':'; + + byte = memp[offset]; + if ( (byte >= 'A' && byte <= 'Z') + || (byte >= 'a' && byte <= 'z') + || (byte >= '0' && byte <= '9') ) { + /* keep it */ + } else { + byte = '.'; + } + *charsp++ = byte; + ++offset; + } + *(valsp-1) = '\0'; /* -1 to overwrite ':' */ + *charsp = '\0'; + + if ( (NULL == tag) || (XP_STRLEN(tag) + sizeof(vals) >= sizeof(buf)) ) { + tag = ""; + } + XP_SNPRINTF( buf, sizeof(buf), "%s[%d]: %s %s", tag, oldOffset, + vals, chars ); + XP_LOGF( buf ); + } +} +#endif + #ifdef CPLUS } #endif diff --git a/xwords4/common/strutils.h b/xwords4/common/strutils.h index 7e1627ac5..d777f1bbe 100644 --- a/xwords4/common/strutils.h +++ b/xwords4/common/strutils.h @@ -50,6 +50,14 @@ XP_UCHAR* emptyStringIfNull( XP_UCHAR* str ); /* Produce an array of ints 0..count-1, juggled */ XP_Bool randIntArray( XP_U16* rnums, XP_U16 count ); + +#ifdef DEBUG +void log_hex( const XP_U8*memp, XP_U16 len, const char* tag ); +# define LOG_HEX(m,l,t) log_hex((const XP_U8*)(m),(l),(t)) +#else +# define LOG_HEX(m,l,t) +#endif + #ifdef CPLUS } #endif