/* Program name: rm4mat.c
   Remove format codes in .gb files without changing anything else.
   May need to edit the file for better look after removing the format codes.
   Usage: rm4mat file_name
   Output: file_name.new
   Author: Chenghong Wang
   Date: 4/3/93
   First revision: 4/18/93 -- converts /ZI(20) and /ZI(40) to characters

   This revision: 4/24/93 --  convert /ZI(15), /ZI(58), /ZI(66), /ZI(77), 
			      /ZI(95), /ZI(97) and /ZI(98) to characters.
   It also can print the unknown /ZI number onthe screen, and make a square
     mark in the out_file.
   It can be compiled by most C complier.  I have compiled it by Turbo C,
     Turbo C++ and VAX C.
   Zhouhong Zhang mezhan@sn01.sncc.lsu.edu   
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0

char in_name[81];
char out_name[81];
char name[81];
FILE *in_file;
FILE *out_file;

void usage() {
  fprintf(stderr, "Usage: rm4mat file_name\n");
  exit(1);
}

void open_file() {
  if ((in_file = fopen(in_name, "r+b")) == NULL) {
    fprintf(stderr, "Cannot open in_file\n");
    exit(1);
  }
  if ((out_file = fopen(out_name, "w+b")) == NULL) {
    fprintf(stderr, "Cannot open out_file\n");
    exit(1);
  }
}

void rm4mat() {
  int b, flag = FALSE, flag1 = FALSE;
  unsigned int c;
  int d;
  char in_string[3];

  while ((b = fgetc(in_file)) != EOF) {
    c = (unsigned int) b;
    if (flag1 == TRUE) {
      if (c == '\\' || c == '{' || c == '}' || c == '%' || c == '|') {
	fputc(c, out_file);
	flag = FALSE;
      }
      else if (c == 0xD7 && 
	       (fgetc(in_file) == 0xD6) && 
	       (fgetc(in_file) == '(') ) {
	fgets(in_string,3,in_file);
	d = atoi(in_string);
	switch (d) {
	  case  0: break;   /* new */
	  case 15: fputc(0xB7,out_file);    /* new */
		   fputc(0xA2,out_file);
		   break;
	  case 20: fputc(0xB8,out_file);
		   fputc(0xC9,out_file);
		   break;
	  case 40: fputc(0xC0,out_file);
		   fputc(0xEF,out_file);
		   break;
	  case 58: fputc(0xCB,out_file);    /* new */
		   fputc(0xC9,out_file);
		   break;
	  case 66: fputc(0xCF,out_file);    /* new */
		   fputc(0xB5,out_file);
		   break;
	  case 77: fputc(0xB4,out_file);    /* new */
		   fputc(0xEB,out_file);
		   break;
	  case 95: fputc(0xC9,out_file);    /* new */
		   fputc(0xED,out_file);
		   break;
	  case 97: fputc(0xD7,out_file);    /* new */
		   fputc(0xBC,out_file);
		   break;
	  case 98: fputc(0xD3,out_file);    /* new */
		   fputc(0xE0,out_file);
		   break;
	  default: printf("%d\n", d);       /* new */
		   fputc(0xA1,out_file);
		   fputc(0xF5,out_file);
		   break;
	}
      }
      flag1 = FALSE;
      continue;
    }
    if (c == '\\') {
      flag = TRUE;
      flag1 = TRUE;
      continue;
    }
    if (c == '}')
      continue;
    if (flag == TRUE) {
      if (c == '.' || c == '{')
	flag = FALSE;
      continue;
    }
    if (c == '|')
      fputc('\t', out_file);
    else
      fputc(c, out_file);
    if (c == 32)
      fputc(32, out_file);
  }
}

int main (argc, argv)
int argc;
char *argv[];

{
  if (argc <= 1)
    usage();

  in_file = NULL;
  out_file = NULL;

  strcpy(in_name, argv[1]);
  sscanf(in_name, "%[^.]", name);
  strcpy(out_name, "");
  strcat(out_name, name);
  strcat(out_name, ".new");

  open_file();
  rm4mat();
  return;
}


