home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / Libraries / Template / c / LoadFile < prev    next >
Encoding:
Text File  |  1993-06-29  |  5.0 KB  |  165 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Template.LoadFile.c
  12.     Author:  Copyright © 1992 Jason Williams
  13.              Thanks to John Winters for supplying the code that I hacked
  14.              changed, hacked, rewrote, and then wrote again from scratch!
  15.     Version: 1.11 (29 Jun 1992)
  16.     Purpose: Loading, cacheing, and retrieval of window templates
  17.              Now correctly loads templates with outline fonts in them
  18. */
  19.  
  20.  
  21. #include "TempDefs.h"
  22.  
  23.  
  24. /* Not intended for user consumption */
  25. linklist_header template_list         = {NULL, NULL};
  26. font_array      *template_fontarray   = (font_array *) -1;
  27.  
  28.  
  29.  
  30. static void ReadHeader(char *filename)
  31. /* Find out how many templates, names, and sizes */
  32. {
  33.   FILE *filehandle;
  34.   int  buffer[10];  /* 40 byte file buffer */
  35.   int  i, numtemplates;
  36.   char *s;
  37.   template_record *temprec;
  38.  
  39.   filehandle = fopen(filename, "rb");
  40.   if (filehandle == NULL)
  41.     Error_ReportFatalInternal(ERR7, ERRMESS7);
  42.  
  43.   if (fread(buffer, HEADER_SIZE, 1, filehandle) != 1)
  44.   {
  45.     fclose(filehandle);
  46.     Error_ReportFatalInternal(ERR4, ERRMESS4);
  47.   }
  48.  
  49.   numtemplates = 0;
  50.   while(TRUE)
  51.   {
  52.     if (fread(&buffer[0], INDEX_SIZE, 1, filehandle) != 1)
  53.     {
  54.       fclose(filehandle);
  55.       Error_ReportFatalInternal(ERR5, ERRMESS5);
  56.     }
  57.  
  58.     if (buffer[0] == 0) /* template list terminator */
  59.       break;
  60.  
  61.     numtemplates++;
  62.     temprec = (template_record *) malloc(sizeof(template_record));
  63.  
  64.     i = 0;
  65.     s = (char *) &buffer[3];
  66.     buffer[6] = 0;  /* Ensure terminators after 12-bytes of name */
  67.     while (TRUE)
  68.     {      
  69.       if (s[i] < 32)
  70.       {
  71.         temprec->identifier[i] = '\0';
  72.         break;
  73.       }
  74.       temprec->identifier[i] = s[i];
  75.  
  76.       i++;
  77.     }
  78.  
  79.     temprec->dataoffset   = buffer[0];
  80.     temprec->templatesize = buffer[1];     /* size needed to load template */
  81.     temprec->windowdef    = NULL;
  82.     temprec->indirectdata = NULL;
  83.     LinkList_Init(&(temprec->header));
  84.     LinkList_AddToTail(&template_list, &(temprec->header));
  85.   }
  86.  
  87.   fclose(filehandle);
  88. }
  89.  
  90.  
  91.  
  92. extern void Template_LoadFile(char *leafname)
  93. {
  94.   template_record *tptr;
  95.   char            filename[60];
  96.   char            tempname[20];
  97.   template_block  tblock;
  98.   char            tempdata[5192];        /* temp. buffer for indirected data */
  99.  
  100.   strcpy(filename, resource_pathname);
  101.   strcat(filename, leafname);
  102.  
  103.   ReadHeader(filename);     /* Find out how many templates, names, and sizes */
  104.   Wimp_OpenTemplate(filename);
  105.  
  106.   tptr = (template_record *) template_list.next;
  107.   while (tptr != NULL)
  108.   {
  109.     tptr->windowdef = (window_block *) malloc(tptr->templatesize);
  110.     if (tptr->windowdef == NULL)
  111.     {
  112.       Wimp_CloseTemplate();
  113.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  114.     }
  115.  
  116. /*  Now, read template once to determine the indirected data size needed.
  117.  *  I tried many different methods to do this, but thrashing in all the icons
  118.  *  and counting their indirect text buffer sizes, and then thrashing in
  119.  *  all of the validation strings and counting their lengths was slow, nasty,
  120.  *  and didn't seem very reliable.
  121.  *  This way also produces a much smaller code lump to include in DeskLib
  122.  *  It works very nicely, so long as your indirected data doesn't expand
  123.  *  to more than 5kB (unlikely unless you are being very antisocial towards
  124.  *  Mr. Wimp)
  125.  */
  126.     tblock.buffer   = tptr->windowdef;
  127.     tblock.workfree = tempdata;
  128.     tblock.workend  = tblock.workfree + 5188;
  129.  
  130.     /* The following line has been changed. If the font array was not passed
  131.      * in here as well, the fonts were not handled at all...
  132.      */
  133.     tblock.font     = template_fontarray;   /* was  (font_array *) -1 */
  134.     strcpy(tempname, tptr->identifier);
  135.     tblock.name     = tempname;
  136.     tblock.index    = 0;
  137.     Wimp_LoadTemplate(&tblock);
  138.  
  139.     tptr->indirectsize = (tblock.workfree - tempdata) + 4;
  140.     tptr->indirectdata = (char *) malloc(tptr->indirectsize);
  141.     if (tptr->indirectdata == NULL)
  142.     {
  143.       Wimp_CloseTemplate();
  144.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  145.     }
  146.  
  147. /*  Now, do a Template_Load to actually load in the template. Should be nice
  148.  *  and quick, as the previous load should have cached the data in a buffer
  149.  *  so more disc reading is unlikely. (Though don't quote me on that! ;-)
  150.  */
  151.     tblock.buffer   = tptr->windowdef;
  152.     tblock.workfree = tptr->indirectdata;
  153.     tblock.workend  = tblock.workfree + tptr->indirectsize;
  154.     tblock.font     = template_fontarray;
  155.     strcpy(tempname, tptr->identifier);
  156.     tblock.name     = tempname;
  157.     tblock.index    = 0;
  158.     Wimp_LoadTemplate(&tblock);
  159.  
  160.     tptr = (template_record *) tptr->header.next;
  161.   }
  162.  
  163.   Wimp_CloseTemplate();
  164. }
  165.