home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / desklib / Libraries / Template / c / LoadFile < prev    next >
Encoding:
Text File  |  1994-03-02  |  5.7 KB  |  180 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.12 (02 Mar 1994)
  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.   /* Remember the end of the template list - this will be NULL for the
  104.    * first call to Template_LoadFile, and non-NULL for subsequent calls.
  105.    */
  106.   tptr = (template_record *) template_list.previous;
  107.  
  108.   ReadHeader(filename);     /* Find out how many templates, names, and sizes */
  109.   Wimp_OpenTemplate(filename);
  110.  
  111.   /* If tptr is NULL, then this is the first time we have loaded any templates,
  112.    * so get the head of the list to find the first blank record supplied by
  113.    * ReadHeader().  If tptr is not NULL, then we are appending templates, so
  114.    * get the next record after tptr, as this will be the first blank record
  115.    * just added by ReadHeader().
  116.    */
  117.   if (tptr == NULL)
  118.     tptr = (template_record *) template_list.next;
  119.   else
  120.     tptr = (template_record *) tptr->header.next;
  121.   
  122.   while (tptr != NULL)
  123.   {
  124.     tptr->windowdef = (window_block *) malloc(tptr->templatesize);
  125.     if (tptr->windowdef == NULL)
  126.     {
  127.       Wimp_CloseTemplate();
  128.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  129.     }
  130.  
  131. /*  Now, read template once to determine the indirected data size needed.
  132.  *  I tried many different methods to do this, but thrashing in all the icons
  133.  *  and counting their indirect text buffer sizes, and then thrashing in
  134.  *  all of the validation strings and counting their lengths was slow, nasty,
  135.  *  and didn't seem very reliable.
  136.  *  This way also produces a much smaller code lump to include in DeskLib
  137.  *  It works very nicely, so long as your indirected data doesn't expand
  138.  *  to more than 5kB (unlikely unless you are being very antisocial towards
  139.  *  Mr. Wimp)
  140.  */
  141.     tblock.buffer   = tptr->windowdef;
  142.     tblock.workfree = tempdata;
  143.     tblock.workend  = tblock.workfree + 5188;
  144.  
  145.     /* The following line has been changed. If the font array was not passed
  146.      * in here as well, the fonts were not handled at all...
  147.      */
  148.     tblock.font     = template_fontarray;   /* was  (font_array *) -1 */
  149.     strcpy(tempname, tptr->identifier);
  150.     tblock.name     = tempname;
  151.     tblock.index    = 0;
  152.     Wimp_LoadTemplate(&tblock);
  153.  
  154.     tptr->indirectsize = (tblock.workfree - tempdata) + 4;
  155.     tptr->indirectdata = (char *) malloc(tptr->indirectsize);
  156.     if (tptr->indirectdata == NULL)
  157.     {
  158.       Wimp_CloseTemplate();
  159.       Error_ReportFatalInternal(ERR3, ERRMESS3);
  160.     }
  161.  
  162. /*  Now, do a Template_Load to actually load in the template. Should be nice
  163.  *  and quick, as the previous load should have cached the data in a buffer
  164.  *  so more disc reading is unlikely. (Though don't quote me on that! ;-)
  165.  */
  166.     tblock.buffer   = tptr->windowdef;
  167.     tblock.workfree = tptr->indirectdata;
  168.     tblock.workend  = tblock.workfree + tptr->indirectsize;
  169.     tblock.font     = template_fontarray;
  170.     strcpy(tempname, tptr->identifier);
  171.     tblock.name     = tempname;
  172.     tblock.index    = 0;
  173.     Wimp_LoadTemplate(&tblock);
  174.  
  175.     tptr = (template_record *) tptr->header.next;
  176.   }
  177.  
  178.   Wimp_CloseTemplate();
  179. }
  180.