# include <string.h>
# include "srgp.h"
# include "default_.h"

#define BUFFERSIZE   150

static char   inputline [BUFFERSIZE+1];
extern srgp__attribute_group current_attributes;
typedef unsigned char eightchar[8];
extern eightchar *srgp__bitmapPatternTable;

/** LOAD A SINGLE BITMAP PATTERN
Application must send data in the form of 8 characters, the
same format created by the X bitmap editor.
**/

void printit (char *data)
{
  int i;
  printf ("[ ");
  for (i=0; i < 8; i++)
    printf ("0x%x ",data[i]);
  printf ("]\n");
}

void SRGP_loadBitmapPattern (int pattern_id, char *data)
{
  register int i;
  printit (data);
  printf ("   %p --> %p (%d)\n",data,srgp__bitmapPatternTable[pattern_id],
	sizeof(eightchar));
/*  memcpy (srgp__bitmapPatternTable[pattern_id], data, sizeof(eightchar)); */
  for (i=0; i < 8; i++)
    (*(srgp__bitmapPatternTable + pattern_id))[i] = data[i];
  printf ("    did it!\n");
}

static char TheEmptyBitmap[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

void SRGP__initDefaultPatterns ()
{
   register i;

printf ("%d x %d = %d\n",sizeof(eightchar),MAX_PATTERN_INDEX+1,
        sizeof(eightchar) * (MAX_PATTERN_INDEX+1));

   srgp__bitmapPatternTable = (eightchar *) malloc (sizeof(eightchar) * (MAX_PATTERN_INDEX+1));
   for (i=0; i <= 104; i++) {
	SRGP_loadBitmapPattern (i, default_bitpat[i]);
   }
   for ( ; i <= MAX_PATTERN_INDEX; i++) {
	SRGP_loadBitmapPattern (i, TheEmptyBitmap);
   }
}



/** LOAD BITMAP PATTERNS
This command reads from an input stream (already
opened by the caller) that should contain one or more bitmap pattern specs.
It reads until EOF is seen, or until any parsing error occurs.

Each spec must occupy exactly two lines.  The first line must look like this:

static char bitpat_X[] = {

where X is a non-negative integer.

The second line must specify 8 hexademical numbers in this format:

   0x??, 0x??, 0x??, 0x??, 0x??, 0x??, 0x??, 0x??};

Each 2-hexdigit number is 8 bits and thus describes one "scan line"
of the pattern.

This format is compatible with the output of
the X bitmap editor (called, appropriately, "bitmap") AS LONG AS
that editor is invoked in this manner:

	bitmap bitpat_X 8x8

The output files from several bitmap editing sessions can be concatenated
to form a single file defining many bitmap patterns, to be loaded
in a single call to SRGP_loadBitmapPatterns.

As a convenience, any lines beginning with '#' are ignored, but these
comment lines must not interrupt a single two-line spec sequence.

The closing of the input stream must be performed by the caller.

RETURNS 0 if no problem opening the file and parsing the input.
RETURNS 1 if any problems at all occurred.
**/

int SRGP_loadBitmapPatternsFromFile (FILE *stream)
{
   int intbit[8];
   char charbit[8];
   int i, pattern_id, retval;

   while (fgets(inputline, BUFFERSIZE, stream)) {   /* WHILE NOT EOF */

	  /* IGNORE COMMENT LINES. */
	  if (inputline[0] == '#')
	 continue;

	  /* PARSE TO FIND PATTERN INDEX. */
	  if (sscanf (inputline, "static char bitpat_%d", &pattern_id) != 1)
	 return 1;  /* ERROR! spec start doesn't match required format */

	  /* GET NEXT LINE: IT CONTAINS THE PATTERN BITS. */
	  if ( ! fgets(inputline, BUFFERSIZE, stream))
	 return 1;  /* ERROR! premature eof */

	  /* PARSE THE PATTERN BITS LINE */
	  retval = sscanf (inputline,
			   " 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
			   &intbit[0], &intbit[1], &intbit[2], &intbit[3],
			   &intbit[4], &intbit[5], &intbit[6], &intbit[7]);
	  if (retval != 8)
	 return 1;  /* ERROR! spec 2nd line doesn't match required format */

	  /* TRANSFORM THE INT-ARRAY SPEC INTO CHAR-ARRAY SPEC. */
	  for (i=0; i<8; i++)
	 charbit[i]=intbit[i];

	  SRGP_loadBitmapPattern (pattern_id, charbit);
   }

   return 0;
}
