/**********************************************************************
  makarray.c       compile:  msc makarray.c;    for Microsoft C 4.0
                             link makarray;

  Makarray will convert any file into C array format.  It was written
  to allow 4000 byte screen dumps, like the .bin files generated by
  ANSIPAINT,  to be linked into a C program and displayed by the
  putscrn(screen array, video page)  function.  The output for
  makarray() is sent to stdout and can be redirected to any .c or .h
  file.  The resulting char * is called hexdata, but can be changed
  to any name.

     Usage:   makarray filename >screen.h

     10/24/86    by Mike Elkins   Mike's C Board -- 619-722-8724

************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#define BUFSIZE 512

extern int errno;
char buffer[BUFSIZE];    /* input buffer */

main(argc,argv)         /* Generate arrar */
int  argc;
char *argv[]; 
{
    char c;
    unsigned int i,numin,tot,file,cfrom;

    if (argc < 2) {
        abort("Usage:makarray filespec ",0,0);
    }

    if ((file=open(argv[1],O_BINARY)) == -1) {
        abort("cannot open",argv[1],errno);
    }

    tot = 0;

    /*    read and dump BUFSIZE at a time      */

    printf("char  hexdata[] = { \n");
    do {
        numin=read(file,buffer,BUFSIZE);
        tot += numin;
        if (numin == -1) {
            abort("cannot read",argv[1],errno);
        }
        cfrom=0;
        while (cfrom < numin) {


            /*    print 16 bytes in hex    */

            for (i=0; i < 16; i++) {
                putchar('0');
                putchar('x');
                ohb(buffer[cfrom++]);
                putchar(',');
            }

            /*    print the bytes in ascii as a comment    */
            /****  REMOVE COMMENTS TO ENABLE ASCII  *****/

            /*         printf(" /* ");                 */
            /*            for (i=0; i < 16; i++) {     */
            /*                c = buffer[cfrom] & 0x7f;*/
            /*                if ( isprint(c) )        */
            /*                    putchar(c);          */
            /*                else                     */
            /*                    putchar('.');        */
            /*                cfrom++;                 */
            /*                }                        */
            /*         printf(" * /");    */   /* remove space in comment */

            putchar('\n');
        }
    }
    while (numin == BUFSIZE);

    printf("0x00   };    /* %u bytes */", tot);     /* terminate w/NULL */
}

ohb(byt)            /*    print a byte in hex    */
char byt; 
{
    onib(byt>>4);
    onib(byt);
}

onib(nib)            /*    print a nibble as a hex character   */
char nib; 
{

    nib&=15;
    putchar((nib >= 10) ? nib-10+'A': nib+'0');
}

/** abort.c - print error messages & number then call _exit(FAIL)
          9.15.83
**/

abort( msg1 ,msg2 ,errno)       /*  print error msg1, msg2, and nbr */
char *msg1,*msg2;           /*    Does not close files.  */
short errno;
{
    fprintf(stderr,"ERR: ");
    if (msg1)
        fprintf(stderr,msg1);
    if (msg2)
        fprintf(stderr," ");
    fprintf(stderr,msg2);
    if (errno)    {
        fprintf(stderr," %-d",errno);
    }
    exit(1);
}
