/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
#include <debug.h>
#include <string.h>
#include <iConstants.h>


/*
** Direct inbound trace output to appropriate output.
** For now, this is just stdout.
*/
#if defined( DEBUGALL ) || defined( DEBUGFLOW ) || defined( DEBUGINFO ) || defined( DEBUGERROR )
void iobxDebug( const char *format, ... ) {
   va_list args;
   va_start(args, format);
   vprintf(format, args);
   va_end(args);
}
#endif

#if defined( _WIN32 ) && (defined( DEBUGALL ) || defined( DEBUGFLOW ) || defined( DEBUGINFO ) || defined( DEBUGERROR ))
void _describeWin32SocketError() {
   char *msgbuffer;
   long errcode;
   unsigned long rc;

   errcode = WSAGetLastError();
   rc = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
                       NULL,
                       errcode,
                       0,
                       (char *)
                       &msgbuffer,
                       0,
                       NULL
                      );
   if ( !rc ) {
      fprintf(stderr, "_describeWin32SocketError() Received error code %ld from FormatMessage() while"
                      " trying\nto format message for error code %ld: \n", GetLastError(), errcode);
   } else {
      iobxDebug( "Socket: (code %ld):  %s", errcode, msgbuffer );
      LocalFree( msgbuffer );
   }
}
#endif

#if defined( DEBUGALL )
void  outputStorage( const char *tagline, const void *mem, int len ) {
   #define SHOW_BYTES 8
   int show = len;
   int i,spc,toshow = 0;
   void *cursor = (void *)mem;
   void *cursorloop = (void *)mem;


   while ( show > 0 ) {
      toshow = min( SHOW_BYTES, show );

      printf("%s\t", tagline);
      cursorloop = cursor;
      spc = 0;
      for (i=0; i<SHOW_BYTES; i++) {
         if ( i < toshow ) {
            printf("%02x",*((char *)cursorloop));
            (char *)cursorloop += 1;
         } else {
            printf("  ");
         }
         if (++spc == 4) {
            spc = 0;
            printf(" ");
         }
      }

      printf("\t");
      cursorloop = cursor;
      spc = 0;
      for (i=0; i<SHOW_BYTES; i++) {
         if ( i < toshow ) {
            if ( *((char *)cursorloop) >= 0x20 && *((char *)cursorloop) <= 0x7E ) {
               printf("%c",*((char *)cursorloop));
            } else {
               printf(".");
            }
            (char *)cursorloop += 1;
         } else {
            printf(".");
         }
         if (++spc == 4) {
            spc = 0;
            printf(" ");
         }
      }

      (char *)cursor += 8;
      show -= 8;

      printf("\n");
   }
}
#endif


