/*

File: general.c
Author: Neil Cafferkey
Copyright (C) 2000 Neil Cafferkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

#include "viewer.h"

#include "general_protos.h"
#include <proto/exec.h>
#include <proto/intuition.h>


/* Function: GetLittleEndianUWord
 * ==============================
 * Reads a little-endian UWORD from memory.
 */

UWORD GetLittleEndianUWord(UBYTE *data)
{
   return (*data+*(data+1)*0x100);
}



/* Function: GetLittleEndianULong
 * ==============================
 * Reads a little-endian ULONG from memory.
 */

ULONG GetLittleEndianULong(UBYTE *p)
{
   return *p|(*(p+1)<<8)|(*(p+2)<<16)|(*(p+3)<<24);
}



/* Function: StripIntuiMessages
 * ============================
 */

VOID StripWindowMessages(struct Window *win)
{
   struct MsgPort *mp=win->UserPort;
   struct IntuiMessage *msg;
   struct Node *succ;

   msg=(struct IntuiMessage *)mp->mp_MsgList.lh_Head;

   while(succ=msg->ExecMessage.mn_Node.ln_Succ)
   {

      if(msg->IDCMPWindow==win)
      {
         Remove((struct Node *)msg);

         ReplyMsg((struct Message *)msg);
      }

   msg=(struct IntuiMessage *)succ;
   }
}



/* Function: GetMinListNode
 * ========================
 */

struct MinNode *GetMinListNode(struct MinList *list,ULONG node_no)
{
   ULONG i;
   struct MinNode *node=list->mlh_Head;

/*printf("node_no=%lu ((struct Node *)node)->ln_Name=%s\n",node_no,
   ((struct Node *)node)->ln_Name);
*/

   for(i=0;i<node_no;i++)
      node=node->mln_Succ;

   return node;

}



/* Function: ReportError
 * =====================
 */

ULONG ReportError(struct Window *window,UBYTE report_type,
   TEXT *resource_name,UWORD version_no)
{
   ULONG result;
   struct EasyStruct easy_struct;

   easy_struct.es_StructSize=sizeof(struct EasyStruct);
   easy_struct.es_Flags=0;
   easy_struct.es_Title="WhirlDisc";
   easy_struct.es_GadgetFormat="OK";

   switch(report_type)
   {
   case ERROR_REPORT_MEM:
      easy_struct.es_TextFormat="Not enough memory.";
      result=EasyRequest(window,&easy_struct,NULL);
      break;
   case ERROR_REPORT_LIB:
      easy_struct.es_TextFormat="Could not open \"%s\" version %lu or"
         " greater.";
      result=EasyRequest(window,&easy_struct,NULL,resource_name,version_no);
      break;
   case ERROR_REPORT_FILE:
      easy_struct.es_TextFormat="Could not open file \"%s\".";
      result=EasyRequest(window,&easy_struct,NULL,resource_name);
      break;
   case ERROR_REPORT_WIN:
      easy_struct.es_TextFormat="Could not open window.";
      result=EasyRequest(window,&easy_struct,NULL);
      break;
   }

   return result;
}



