/**************************************************************************

	   Prints messages, flexibility is reached by attributes

Calling convension to change the default attributes is (e.g.):
	   PrintMsg(NULL, MSGATT_RETURN, ERR_IFFOPENLIB_S, ...)
normal calling convension in this case is:
		      PrintMsg(ERR_IFFOPENLIB_S, ...)

       Copyright (C) 1994 by Alexandros Loghis, All Rights Reserved

**************************************************************************/

#include <stdarg.h>	/* va_start, va_arg, va_end */
#include <stdio.h>	/* fputc, fprintf, vfprintf, fflush */
#include <stdlib.h>	/* exit */

#include "gfx2grob.h"
#include "Convert.h"
#ifdef AMIGA
#include "IffStuff.h"
#endif
#include "PrintMsg.h"


#define PRGNAMESTR	"gfx2grob"
#define VERSIONSTR	"V1.0"
#define RELEASEDATESTR	"21-Nov-1994"


#define FS(T, A, M) MsgTypeArr T = { { A, M } }	  /* Fill struct macro */
#define A(M) MSGATT_##M		/* for better outfit */


static char VS[] = "\0$VER: "PRGNAMESTR" "VERSIONSTR" ("RELEASEDATESTR")";
static char US[] =
"Usage:\n"
"  "PRGNAMESTR" <-g[Dimension] | -r"
#ifdef AMIGA
" | -i"
#endif
"> <Inputfile> [Outputfile]\n"
"  %s ?  for help";
static char HS[] =
PRGNAMESTR" "VERSIONSTR" converts graphic files from and to a HP-48 calculator.\n"
"Options are:\n"
"\n"
"-g[Dimension]  RAW "
#ifdef AMIGA
"or IFF "
#endif
"to GROB format. Dimension is WidthxHeight\n"
"               Default is "DEFAULDWIDTHSTR"x"DEFAULDHEIGHTSTR
#ifdef AMIGA
" (only RAW to GROB)"
#endif
"\n"
"-r             GROB to RAW format\n"
#ifdef AMIGA
"-i             GROB to IFF format\n"
#endif
"\n"
"Put the case insensitive options in any order. Enter one option only\n"
"If no outputfile is entered then "PRGNAMESTR" will create its own.\n"
"\n"
"        "PRGNAMESTR" is FreeWare. "RELEASEDATESTR", by Alexandros Loghis";
static char NOMEMSTR[] = "Can't allocate memory";

FS(MSG_USAGE_S,
   A(NOERRTXT) | A(NOEXCMRK),
   US
  );
FS(MSG_HELP,
   A(STDOUT) | A(NOEXCMRK),
   HS
  );
FS(ERR_NOMEM,
   A(NULL),
   NOMEMSTR
  );
FS(ERR_OPEN_S,
   A(ERRWHILE),
   "opening"
  );
FS(ERR_CLOSE_S,
   A(ERRWHILE),
   "closing"
  );
FS(ERR_READ_S,
   A(ERRWHILE),
   "reading from"
  );
FS(ERR_WRITE_S,
   A(ERRWHILE),
   "writing to"
  );
FS(ERR_GROB_S,
   A(NULL),
   GROBSTR" at line 2 col 1 expected in \"%s\""
  );
FS(ERR_EOF_S,
   A(NULL),
   "EOF expected in \"%s\""
  );
FS(ERR_WORD_S,
   A(NULL),
   "Word longer than expected in \"%s\""
  );
FS(MSG_OUTFNAM_S,
   A(STDOUT) | A(NOBEGLF) | A(RETURN),
   "Outputfile is \"%s\""
  );
FS(MSG_OUTFEX_S,
   A(STDOUT) | A(NOEXCMRK) | A(NOENDLF) | A(RETURN),
   "Outputfile \"%s\" already exists, overwrite ?  (y/n): "
  );
FS(MSG_SKIPLN,
   A(STDOUT) | A(NOBEGLF) | A(NOEXCMRK) | A(RETURN),
   "Skipping first line."
  );
FS(MSG_GPHFORMIS_DD,
   A(STDOUT) | A(NOBEGLF) | A(NOEXCMRK) | A(RETURN),
   "Graphic format is %d x %d"
  );
FS(MSG_OPSUCCESS,
   A(STDOUT),
   "Operation successful"
  );
#ifdef AMIGA
FS(ERR_IFFREM_S,
   A(ERRWHILE),
   "deleting"
  );
FS(ERR_IFFLOCK_S,
   A(ERRWHILE),
   "locking"
  );
FS(ERR_IFFEXAM_S,
   A(ERRWHILE),
   "examining"
  );
FS(ERR_IFFOPENLIB_SD,
   A(NULL),
   "Can't open %s V%d+"
  );
FS(ERR_IFFMEM,
   A(NULL),
   NOMEMSTR
  );
FS(ERR_IFFSIZE_S,
   A(NULL),
   "Size of \"%s\" is zero"
  );
FS(ERR_IFFNOILBM_S,
   A(NULL),
   "IFF file \"%s\" is not of type ILBM"
  );
FS(ERR_IFFNOBMHD_S,
   A(NULL),
   "BMHD chunk not found in \"%s\""
  );
FS(ERR_IFFNOBODY_S,
   A(NULL),
   "BODY chunk not found in \"%s\""
  );
FS(ERR_IFFBADCOMP_S,
   A(NULL),
   "\"%s\" has an unknown compression type"
  );
FS(ERR_IFFUNKNOWN_S,
   A(NULL),
   "Unknown error type in \"%s\""
  );
FS(ERR_IFFNO1PL_S,
   A(NULL),
   "\"%s\" has more than one bitplane"
  );
FS(ERR_IFFNOIFF_S,
   A(STDOUT) | A(RETURN) | A(NOBEGLF) | A(NOEXCMRK),
   "\"%s\" is not an IFF file."
  );
FS(MSG_ASSUMBIN_S,
   A(NOBEGLF) | A(NOERRTXT) | A(RETURN),
   "Assuming that \"%s\" is binary"
  );
#endif

/*************************************************************************/

extern void PrintMsg(MsgTypePtr Msg, ...)
{
  va_list pArg;

  FILE *Stream;

  ATTRTYPE Att;


  va_start(pArg, Msg);
  if (Msg)
    Att = Msg->DefAtt;
  else {
    Att = va_arg(pArg, ATTRTYPE);
    Msg = va_arg(pArg, MsgTypePtr);
  }
  Stream = (Att & MSGATT_STDOUT) ? stdout : stderr;
  if (!(Att & MSGATT_NOBEGLF)) fputc('\n', Stream);
  if (Stream == stderr)
    if (!(Att & MSGATT_NOERRTXT)) fprintf(Stream, "ERROR: ");
  if (Att & MSGATT_ERRWHILE) {
    fprintf(Stream, "While %s", Msg->MsgTxt);
    vfprintf(Stream, " \"%s\"", pArg);
  }
  else
    vfprintf(Stream, Msg->MsgTxt, pArg);
  if (!(Att & MSGATT_NOEXCMRK)) fprintf(Stream, " !");
  if (!(Att & MSGATT_NOENDLF)) fputc('\n', Stream);
  fflush(Stream);
  if (!(Att & MSGATT_RETURN)) exit((Stream == stderr) ? EXIT_FAILURE : 0);
  va_end(pArg);
}
