
// common declarations and routines
// Version 2.16, 03.Feb.98
// (c) 1997/98 by Stefan Diener

#ifndef STIMP_MISC_INC
#define STIMP_MISC_INC

// The next define makes it impossible to read PBM- and PPM-files for PGM-only operators !!!
// - PBM -> b/w PGM
// - PGM (as usual)
// - PPM -> red color channel as PGM
// If you want to use this feature comment the following line !
//#define NO_PSEUDO_PGM_MODE

// The next define makes it impossible to read PBM- and PGM-files for PPM-only operators !!!
// - PBM -> b/w PPM
// - PGM -> gray PPM
// - PPM (as usual)
// If you want to use this feature comment the following line !
//#define NO_PSEUDO_PPM_MODE

// define TRUE, FALSE and BOOL if not already done in <sys/types.h>
#ifndef TRUE
#define TRUE		1
#endif

#ifndef FALSE
#define FALSE		0
#endif

#ifndef BOOL
#define BOOL int
#endif

// define some types
#define TYPE_PBM 1
#define TYPE_PGM 2
#define TYPE_PPM 4

// Define a function for correcting errors due fgets.
// Please insert correct(string) always after using fgets,
// because of (UNIX-)compatibility.
#define correct(s) if ((s[0]) && (s[strlen(s)-1]==10)) s[strlen(s)-1]=0

// version string for AmigaOS
const char _AMIGA_Version[]="$VER: "OP_NAME" "VERSION" ("DATE") "AUTHOR;

static int beVerbose=TRUE;   // print messages ? default is YES !

// declare some functions
void Hilfe(void);
char *GetFilename(int nummer, int argc,char **argv);

// define some functions
void PrintMessage(char *fmt, ...)
// standard output routine
{
  va_list args;

  if (beVerbose)
  {
    va_start(args,fmt);
    fprintf(stderr,OP_NAME": ");
    vfprintf(stderr,fmt,args);
    fprintf(stderr,"\n");
    va_end(args);
  }
}

void PrintOpening(int argc, char **argv)
// standard welcome message
{
  int i;
  unsigned char *temp;

  if (argc==1) beVerbose=TRUE;
  else
    for (i=1; i<argc; i++)
    {
      temp=GetFilename(i,argc,argv);
      if (temp==NULL) i=argc;
      else if (strcmp("-",temp)==0) { i=argc; beVerbose=FALSE; }
    }

  if (beVerbose) fprintf(stderr,OP_NAME": "VERSION" ("DATE")  (c) "AUTHOR"\n");

  if (argc==1)
  {
    Hilfe();
    exit(0);
  }
}

void PrintClosing(void)
// standard end message
{
  if (beVerbose) fprintf(stderr,OP_NAME": Done !\n");
}

void ReadComment(FILE *datei)
// skip one or more lines of comments in a PBM/PGM/PPM-file
{
  int wert;

  do
  {
    wert=fgetc(datei);
    if (wert!=EOF)   // EOF while reading header ?
    {
      if (wert==0x23)   // '#' = start of a comment line
      {
        do
        {
          wert=fgetc(datei);
          if (wert==EOF) wert=0x0a;   // end of file = abort
        }
        while (wert!=0x0a);   // end of line
        wert=0x23;
      }
      else fseek(datei,-1,SEEK_CUR);   // seek one position left
    }
  }
  while (wert==0x23);
}

int GetType(char *name)
// get type of a file
// result: 0 = file not found, TYPE_PBM, TYPE_PGM, TYPE_PPM, other = error
{
  FILE *datei;
  char tempo[10];
  int type=128;

  if (strcmp("-",name)==0) datei=stdin;   // use stdin
  else
  {
    // really open a file
    datei=fopen(name,"rb");
    if (datei==0)
    {
      PrintMessage("File %s not found !", name);
      return 0;
    }
  }

  // set buffer size
  setvbuf(datei,0,_IOFBF,500);

  // read 1st line of the header
  fgets(tempo, 9, datei);
  correct(tempo);

  // check the type
  if (strcmp(tempo,"P4")==0) type=TYPE_PBM;
  if (strcmp(tempo,"P5")==0) type=TYPE_PGM;
  if (strcmp(tempo,"P6")==0) type=TYPE_PPM;

  // close file and exit
  if (strcmp("-",name)!=0) fclose(datei);
  return type;
}

int FilenameCount(int argc,char **argv)
// calculates the number of files in the commandline
{
  int i, counter=0, std=0;

  // counting
  for (i=1; i<argc; i++)
  {
    if ((argv[i][0]!='-') && (argv[i][0]!='+')) counter++;
    else if ((argv[i][0]=='-') && (argv[i][1]==0)) std++;
  }

  if (std>2) return (0);
  else return (counter+std);
}

char *GetFilename(int nummer, int argc,char **argv)
// looks for the ...th filename in the commandline
{
  int i, counter=0;

  // at least number one
  if (nummer<1) return NULL;

  // counting
  for (i=1; i<argc; i++)
  {
    if (((argv[i][0]!='-') && (argv[i][0]!='+')) || ((argv[i][0]=='-') && (argv[i][1]==0))) counter++;
    if (counter==nummer)
    {
      // successful
      counter=i;
      nummer=i;
      i=argc;
    }
  }

  // filename found ?
  return ((nummer==counter) ? argv[counter] : NULL);
}

void Hilfe(void)
// print help file on screen
{
  char name[200], *variable;
  FILE *datei;

  // filename and suffix, 1st try
  strcpy(name,OP_NAME);
  strcat(name,".syntax");

  // open file, 1st try
  datei=fopen(name,"rb");
  if (datei==0)
  {
    // help file not in local directory
    // cat the name of the helppath via AmigaOS-Assign, 2nd try
    strcpy(name,"STIMP:help/");
    strcat(name,OP_NAME);
    strcat(name,".syntax");

    // open file, 2nd try
    datei=fopen(name,"rb");
    if (datei==0)
    {
      // cat the name of the helppath via env, 3rd try
      variable=getenv("STIMP");
      strcpy(name,variable);
      strcat(name,"/help/");
      strcat(name,OP_NAME);
      strcat(name,".syntax");

      // open file, 3rd try
      datei=fopen(name,"rb");
      if (datei==0)
      {
        // not found, giving up
        PrintMessage("No help available (helpfile not found) !");
        PrintClosing();
        return;
      }
    }
  }

  // set buffer size
  setvbuf(datei,0,_IOFBF,1000);

  // preamble
  PrintMessage("Help:");
  PrintMessage("");

  // read file and print it on the screen
  while (fgets(name, 100, datei)!=NULL)
  {
    correct(name);
    PrintMessage("%s", name);
  }

  // close file
  fclose(datei);

  // one more line and goodbye
  PrintMessage("");
  PrintClosing();
}

#endif
