home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 3 / CD_Magazyn_EXEC_nr_3.iso / Recent / misc / edu / WhirlDisc.lha / WhirlDisc / Source / section.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-11  |  3.8 KB  |  159 lines

  1. /*
  2.  
  3. File: section.c
  4. Author: Neil Cafferkey
  5. Copyright (C) 2000 Neil Cafferkey
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA.
  21.  
  22. */
  23.  
  24. #include "viewer.h"
  25. #include <exec/memory.h>
  26.  
  27. #include "general_protos.h"
  28. #include "paragraph_protos.h"
  29. #include "section_protos.h"
  30. #include <proto/exec.h>
  31.  
  32.  
  33. /* Function: CreateSection
  34.  * =======================
  35.  * Creates a Section.
  36.  */
  37.  
  38. Section CreateSection(UBYTE *raw_section)
  39. {
  40.    Section section;
  41.    UBYTE *data=raw_section,*paragraph_data;
  42.    UWORD i,paragraph_length;
  43.    BOOL success=TRUE;
  44.  
  45.    if((section=AllocMem(sizeof(Section_imp),MEMF_CLEAR))!=NULL)
  46.    {
  47.  
  48.       /* Extract data from section header */
  49.  
  50.       section->paragraph_count=GetLittleEndianUWord(data);
  51.       data+=2;
  52.       section->text_length=GetLittleEndianUWord(data);
  53.       data+=2;
  54.       section->something_a=GetLittleEndianUWord(data);
  55.       data+=2;
  56.       section->something_b=GetLittleEndianUWord(data);
  57.       data+=2;
  58.       section->something_c1=*(data++);
  59.       section->something_c2=*(data++);
  60.  
  61. /*      section->something_c=GetLittleEndianUWord(data);
  62.       data+=2;*/
  63.  
  64.       paragraph_data=data+4;
  65.  
  66.       /* Skip ahead to start of text */
  67.  
  68.       section->text=data+section->paragraph_count*10+
  69.          section->something_a*2+section->something_b+
  70.          section->something_c2*4;
  71.       data=section->text;
  72.  
  73.  
  74. /*
  75. printf("section->text-raw_section=%lu\n",section->text-raw_section);
  76.  
  77. printf("section->paragraph_count=%u\n",section->paragraph_count);
  78. printf("section->text_length=%u\n",section->text_length);
  79. printf("*section->text=%c\n",*section->text);
  80. printf("raw_section=%lx\n\n",raw_section);
  81. */
  82.  
  83.  
  84.  
  85.  
  86.       /* Create paragraphs from raw data */
  87.  
  88.       if((section->paragraphs=(Paragraph *)AllocMem(sizeof(Paragraph)*
  89.          section->paragraph_count,MEMF_CLEAR))!=NULL)
  90.       {
  91.          for(i=0;(i<section->paragraph_count)&&success;i++)
  92.          {
  93.  
  94.             /* Temporary check to see if this is a proper paragraph */
  95.  
  96.             if(*((ULONG *)(paragraph_data+2))==0)
  97.                paragraph_length=GetLittleEndianUWord(paragraph_data);
  98.             else
  99.                paragraph_length=0;
  100.  
  101.             section->paragraphs[i]=CreateParagraph(data,
  102.                paragraph_length);
  103.  
  104.             if(section->paragraphs[i]==NULL)
  105.                success=FALSE;
  106.             data+=paragraph_length;
  107.             paragraph_data+=10;
  108.          }
  109.       }
  110.       else
  111.       {
  112.          ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  113.          success=FALSE;
  114.       }
  115.  
  116.       if(!success)
  117.       {
  118.          KillSection(section);
  119.          section=NULL;
  120.       }
  121.  
  122.    }
  123.    else
  124.    {
  125.       ReportError(NULL,ERROR_REPORT_MEM,NULL,0);
  126.    }
  127.  
  128.    return section;
  129. }
  130.  
  131.  
  132. /* Function: KillSection
  133.  * =====================
  134.  * Destroys a section.
  135.  */
  136.  
  137. VOID KillSection(Section section)
  138. {
  139.    UWORD i;
  140.  
  141.    if(section->paragraphs!=NULL)
  142.    {
  143.       for(i=0;i<section->paragraph_count;i++)
  144.          if(section->paragraphs[i]!=NULL)
  145.             KillParagraph(section->paragraphs[i]);
  146.       FreeMem(section->paragraphs,section->paragraph_count
  147.          *sizeof(Paragraph));
  148.    }
  149.  
  150.    if(section->line_points!=NULL)
  151.       FreeMem(section->line_points,section->line_count*2*sizeof(UWORD));
  152.  
  153.    FreeMem(section,sizeof(Section_imp));
  154.  
  155.    return;
  156. }
  157.  
  158.  
  159.