/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* |_o_o|\\ Copyright (c) 1989 The Software Distillery.                    *
* |. o.| ||          All Rights Reserved                                  *
* | .  | ||          Written by John Toebes and Doug Walker               *
* | o  | ||          The Software Distillery                              *
* |  . |//           235 Trillingham Lane                                 *
* ======             Cary, NC 27513                                       *
*                    BBS:(919)-471-6436                                   *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <libraries/dos.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include <string.h>

#include "pickpack.h"
#include "struct.h"

/* Macro to declare automatic pointer to longword-aligned area */
#define STALIGN(name,type) char c_##name[sizeof(type)+3]; \
                           type * name = (type *)(((long)(c_##name + 3)>>2)<<2)

int GetHelp(data, len)
char **data;
int *len;
{
   struct VIEWDATA *vd;
   int rc;
   BPTR fp, lock;
   STALIGN(fib, struct FileInfoBlock);

   BUG(1, ("GetHelp: Entry\n"))

   if(!(lock=Lock(HELPFILE, ACCESS_READ)) ||
      !Examine(lock, fib) )
   {
      BUG(2, ("GetHelp: Can't %s file '%s'", 
         lock ? "Lock" : "Examine", HELPFILE))
      if(lock) UnLock(lock);
      status("Can't find help file '" HELPFILE "'");
      BUG(1, ("GetHelp: Exit, returning 1\n"))
      return(1);
   }

   *len = fib->fib_Size+sizeof(struct VIEWDATA);
   UnLock(lock);

   fp = NULL;
   rc = 0;
   if(!(vd = (struct ViewData *)AllocMem(*len, 0)) ||
      !(fp=Open(HELPFILE, MODE_OLDFILE)) ||
      Read(fp, vd->buf, (long)*len) <= 0)
   {
      BUG(2, ("GetHelp: Can't read file '%s'\n", HELPFILE))
      status("Can't find help file '" HELPFILE "'");
      if(*data) FreeMem(*data, *len);
      *data = NULL;
      rc = 2;
   }
   else
      *data = (char *)vd;
   if(fp) Close(fp);

   BUG(1, ("GetHelp: Exit, returning %d\n", rc))

   return(rc);
}


int CountLines(data, len)
char *data;
int len;
{
   int i, lines;
   char *pos;

   for(i=lines=0, pos=data; i<len; i++, pos++)
   {
      if(*pos == '\n')
      {
         lines++;
         *pos = '\0';
      }
   }
   /* Now at end of buffer - add an extra zero */
   *pos = '\0';

   return(lines);
}
