/* herein are functions to read and write PolyBoard files, converting 
between the file format and working data structures */

/* 

file format:
    list of holes
    list of Poly defns

     holes:
       nofholes    (2 bytes)
       (for each hole)
       row, col


     Poly defns:
         nofpieces
         (for each piece)
              cogx
              cogy
              nofunits
              (for each unit) 
                   x
                   y
      
       notes: 
             all parts are byte size
             all coordinates are in units of HS
             cogx, cogy are in work area coords ( / HS)
             they are unsigned, cogx always >=0, cogy always <=0
             x,y are signed, relative to cogx,cogy for piece.


*/

#include "werr.h"
#include "wimp.h"
#include "win.h"
#include "wimpt.h"
#include "xferrecv.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "polysaw.h"




static FILE *outfp;
static FILE *infp;



static void write_out_poly_board(FILE *f);
static void read_in_poly_board(FILE *f);
static void read_in_holes(void);
static void read_in_pieces(void);
static void write_holes(void);
static void write_pieces(void);
static void writechar(int c);
static int  nextchar(void);



BOOL save_poly_board(char *filename, void *handle)
{
FILE *outfp;
os_filestr fileinfo;

handle = handle;
strcpy(filepath, filename);

outfp = fopen(filename, "w");
if (outfp == 0)
     {
     werr(FALSE, "Can't Save. Silly name? Locked file? Protected Disc?\n");
     return FALSE;
     }

write_out_poly_board(outfp);
  
if (fclose(outfp) == 0)
   {
   fileinfo.action = 18;
   fileinfo.name = filename;
   fileinfo.loadaddr = boardfiletype;
   wimpt_noerr(os_file(&fileinfo));  
   win_settitle(board_wh, filepath); 
   return TRUE;
   }
else
    {
    werr(TRUE, "can't close file");
    return FALSE;
    }

} 


void load_file(void)
{
wimp_redrawstr r;
FILE *infp;
char *filename;
int filetype;
int p;

filetype = xferrecv_checkinsert(&filename);

if (filetype != boardfiletype)
    {
    werr(FALSE, "wrong filetype");
    return;      
    }

infp = fopen(filename, "r");
if (infp==0)
  {
  werr(FALSE, "Can't open file");
  return;
  }
read_in_poly_board(infp);
fclose(infp);
strcpy(filepath, filename);
for (p=0; p<nofpieces; p++)
  fill_out_polydefn(p);       

xferrecv_insertfileok();

r.w = board_wh;
r.box = board_windefn->ex;
wimpt_noerr(wimp_force_redraw(&r));

win_settitle(board_wh, filepath);

}     




void read_in_poly_board(FILE *f)
{
infp = f;
read_in_holes();
read_in_pieces();
}

void write_out_poly_board(FILE *f)
{
outfp = f;
write_holes();
write_pieces();
}

void write_holes(void)
{
int nofh = 0;
int row, col;

for (row=0; row<MAX_BOARD; row++)
  {
  for (col=0; col<MAX_BOARD; col++)
    {
    if (grid[row][col] & HOLE_BIT)
        {
        nofh += 1;
        }
    }
  }
   
writechar(nofh & 255);
writechar(nofh >> 8);


for (row=0; row<MAX_BOARD; row++)
  {
  for (col=0; col<MAX_BOARD; col++)
    {
    if (grid[row][col] & HOLE_BIT)
        {
        writechar(row);
        writechar(col);
        }
    }
  }

}




void read_in_holes(void)
{
int nofh, i, row, col;

nofh = nextchar();
nofh += 256*nextchar();

for (row=0; row < MAX_BOARD; row++)
  {
  for (col=0; col<MAX_BOARD; col++)
    {
    grid[row][col] = 0;
    }
  }



for (i=0; i<nofh; i++)
  {
  row = nextchar();
  col = nextchar();
  grid[row][col] = HOLE_BIT;
  }

}




void write_pieces(void)
{
int i, p;
writechar(nofpieces);
for (p=0; p<nofpieces; p++)
  {
  writechar(   piece[p].cog.x/HS );
  writechar( -(piece[p].cog.y/HS) );
  writechar( piece[p].nofunits );
  for (i=0; i<piece[p].nofunits; i++)
    {
    writechar( 128 + (piece[p].units[i].x/HS) );
    writechar( 128 + (piece[p].units[i].y/HS) );
    }
  }
}



void read_in_pieces(void)
{
int i, p;

nofpieces = nextchar();
for (p=0; p<nofpieces; p++)
  {
  piece[p].cog.x = HS*nextchar();
  piece[p].cog.y = -HS*nextchar();
  piece[p].nofunits = nextchar();
  for (i=0; i<piece[p].nofunits; i++)
    {
    piece[p].units[i].x = HS*( nextchar()-128 );
    piece[p].units[i].y = HS*( nextchar()-128 );
    }
  }
}


int nextchar(void)
{
int c;
c = fgetc(infp);
if (c == EOF)  werr(FALSE, "failed to read file\n");
return c;
}

void writechar(int c)
{
if (fputc(c, outfp) == c) return;

werr(FALSE, "failed to write to file");
return;
}





