/*

File: section.c
Author: Neil Cafferkey
Copyright (C) 2000 Neil Cafferkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

#include "viewer.h"
#include <exec/memory.h>

#include "general_protos.h"
#include "paragraph_protos.h"
#include "section_protos.h"
#include <proto/exec.h>


/* Function: CreateSection
 * =======================
 * Creates a Section.
 */

Section CreateSection(UBYTE *raw_section)
{
   Section section;
   UBYTE *data=raw_section,*paragraph_data;
   UWORD i,paragraph_length;
   BOOL success=TRUE;

   if((section=AllocMem(sizeof(Section_imp),MEMF_CLEAR))!=NULL)
   {

      /* Extract data from section header */

      section->paragraph_count=GetLittleEndianUWord(data);
      data+=2;
      section->text_length=GetLittleEndianUWord(data);
      data+=2;
      section->something_a=GetLittleEndianUWord(data);
      data+=2;
      section->something_b=GetLittleEndianUWord(data);
      data+=2;
      section->something_c1=*(data++);
      section->something_c2=*(data++);

/*      section->something_c=GetLittleEndianUWord(data);
      data+=2;*/

      paragraph_data=data+4;

      /* Skip ahead to start of text */

      section->text=data+section->paragraph_count*10+
         section->something_a*2+section->something_b+
         section->something_c2*4;
      data=section->text;


/*
printf("section->text-raw_section=%lu\n",section->text-raw_section);

printf("section->paragraph_count=%u\n",section->paragraph_count);
printf("section->text_length=%u\n",section->text_length);
printf("*section->text=%c\n",*section->text);
printf("raw_section=%lx\n\n",raw_section);
*/




      /* Create paragraphs from raw data */

      if((section->paragraphs=(Paragraph *)AllocMem(sizeof(Paragraph)*
         section->paragraph_count,MEMF_CLEAR))!=NULL)
      {
         for(i=0;(i<section->paragraph_count)&&success;i++)
         {

            /* Temporary check to see if this is a proper paragraph */

            if(*((ULONG *)(paragraph_data+2))==0)
               paragraph_length=GetLittleEndianUWord(paragraph_data);
            else
               paragraph_length=0;

            section->paragraphs[i]=CreateParagraph(data,
               paragraph_length);

            if(section->paragraphs[i]==NULL)
               success=FALSE;
            data+=paragraph_length;
            paragraph_data+=10;
         }
      }
      else
      {
         ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
         success=FALSE;
      }

      if(!success)
      {
         KillSection(section);
         section=NULL;
      }

   }
   else
   {
      ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
   }

   return section;
}


/* Function: KillSection
 * =====================
 * Destroys a section.
 */

VOID KillSection(Section section)
{
   UWORD i;

   if(section->paragraphs!=NULL)
   {
      for(i=0;i<section->paragraph_count;i++)
         if(section->paragraphs[i]!=NULL)
            KillParagraph(section->paragraphs[i]);
      FreeMem(section->paragraphs,section->paragraph_count
         *sizeof(Paragraph));
   }

   if(section->line_points!=NULL)
      FreeMem(section->line_points,section->line_count*2*sizeof(UWORD));

   FreeMem(section,sizeof(Section_imp));

   return;
}


