/*-------------------------------------------------------------------*/
/* Copyright (c) 1992-1993 SAS Institute Inc., Cary NC               */
/*                                                                   */
/* SUPPORT:    walker - Doug Walker                                  */
/* SCRIPT:     sprof                                                 */
/*-------------------------------------------------------------------*/
/* CUT HERE */

#include <proto/dos.h>
#include <string.h>

// These are linker-defined symbols.  Always use the ADDRESS of the
// symbol, not the symbol itself;  The linker will replace the reference
// with the start address and length of the coverage data section

extern long __far _CoverStart;
extern long __far _CoverLength;

void __stdargs _STI_210_cover_init(void)
{
    BPTR fh;
    char tag[4];
    long length;
    
    /* read in existing coverage data */
    fh = Open("cover.dat", MODE_OLDFILE);
    if (fh)
    {
       /* If the file looks OK, load the data.  The first four bytes */
       /* should be 'C' 'O' 'V' '\0'; the next four, the length.     */
       /* If the "COV\0" tag doesn't match up, either this isn't a   */
       /* coverage file or it's an incompatible file format; either  */
       /* way, give up.  If the length doesn't match, the file was   */
       /* generated by a different program, so don't try to merge    */
       /* the data.                                                  */
       /*                                                            */
       /* If the file looks good, we read it in to initialize the    */
       /* coverage section.  This means that the "cover.dat" file    */
       /* will reflect the results of all previous runs as well as   */
       /* this run.                                                  */

       if(Read(fh, tag, 4) == 4)                   // Read the tag
       {
         if(memcmp(tag, "COV", 4) == 0)            // Check the tag
         {
            if(Read(fh, &length, 4) == 4)          // Read the length
            {
               if(length == (long)&_CoverLength)   // Check the length
               {
                  Read(fh, &_CoverStart, length);  // Read the data
               }
            }
         }
      }
      Close(fh);
   }
}

void __stdargs _STD_210_cover_term(void)
{
   BPTR fh;
   long length;
    
   /* Write out coverage data                            */
   /* Note that we destroy the old "cover.dat" file here */
   fh = Open("cover.dat", MODE_NEWFILE);
   if (fh)
   {
      /* Note that the linker replaces &_CoverLength with the actual */
      /* length; this isn't really an address.  We have to assign it */
      /* to a local variable in order to take its address to pass it */
      /* to Write().                                                 */
      length = (long)&_CoverLength;
      Write(fh, "COV", 4);             // Write the tag
      Write(fh, &length, 4);           // And the length
      Write(fh, &_CoverStart, length); // Dump the data
      Close(fh);
   }
}

