/*///---------------------------------------------------------------------\\\*/
/*// Copyright (c)               (_)/=\~/_(_)         All rights reserved. \\*/
/*/  2005-2006                (_)  /=\  ~/_  (_)        Released under the  \*/
/*\  Jim Peters            (_)    /=\    ~/_    (_)             GNU GPL v2  /*/
/*\\ http://uazu.net    (_) ____ /=\ ____ ~/_ ____ (_)      http://fsf.org //*/
/*\\\---------------------------------------------------------------------///*/

//
//	SMTsave -- see SYMIRR.jot for docs
//   

#ifdef HEADER

typedef struct SMTsave SMTsave;
struct SMTsave {
   char **inp;			// Input string reference
   char *out;
   char *fnam;			// Filename
   int xtr2tr;			// Turn extended tree into normal tree on output
   int skipX;			// Set to skip the next 'X' line
   FILE *outuc;			// Output stream for uncompressed
   gzFile *outgz;		// Output stream for compressed
};

#else

#include "all.h"


//
//	SMTsave
//

SMTsave *
smtsave(char *fnam, char **inp, int xtr2tr) {
   SMTsave *sv= ALLOC(SMTsave);

   sv->inp= inp;
   sv->fnam= StrDup(fnam);
   sv->xtr2tr= xtr2tr;

   if (ends_with(fnam, ".gz")) {
      sv->outgz= gzopen(fnam, "w");
   } else {
      sv->outuc= fopen(fnam, "w");
   }

   if (!sv->outgz && !sv->outuc) 
      error("Unable to create output file: %s", fnam);

   return sv;
}

int 
smtsave_do(SMTsave *sv) {
   char *p;

   if (!*sv->inp) return 0;	// Nothing to do until we have input
   if (sv->out) return 0;	// Nothing to do until our output is clear

   if (isEOF(*sv->inp)) {
      sv->out= StrDup(SMEOF);	
      return 0;
   }

   p= *sv->inp;
   *sv->inp= 0;
   sv->out= p;

   // Get rid of flags on extended tree lines
   if (sv->xtr2tr) {
      if (sv->skipX) {
	 if (p[0] != 'X') DIE;
	 sv->skipX= 0;
	 p= 0;
      } else {
	 switch (p[1]) {
	  case 'd':
	     if (p[0] == 'D') sv->skipX= 1;
	     p= 0; break;
	  case 'n':
	  case 'u':
	     if (sv->outgz) { 
		if (-1 == gzputc(sv->outgz, p[0])) goto error; 
	     } else {
		if (-1 == fputc(p[0], sv->outuc)) goto error;
	     }
	     p += 2; break;
	 }
      }
   }
      
   // Output 'p' if non-0
   if (p) {
      if (sv->outgz) {
	 if (-1 == gzputs(sv->outgz, p) ||
	     -1 == gzputc(sv->outgz, '\n'))
	    goto error;
      } else {
	 if (-1 == fputs(p, sv->outuc) ||
	     -1 == fputc('\n', sv->outuc))
	    goto error;
      }
   }

   return 1;
   
 error:
   error("Error writing file: %s", sv->fnam);
}
   
void 
smtsave_del(SMTsave *sv) {
   if (sv->outuc && fclose(sv->outuc)) 
      error("Error writing file: %s", sv->fnam);

   if (sv->outgz && gzclose(sv->outgz))
      error("Error writing compressed file: %s", sv->fnam);

   free(sv->out);
   free(sv->fnam);
   free(sv);
}

#endif

/*///---------------------------------------------------------------------\\\*/
/*// )`( )()/)(`/\`//\`          (_)/=\~/_(_)           '/\\'/\')(\()( )'( \\*/
/*/ ()(')()\)(/`/\\ `         (_)  /=\  ~/_  (_)          ' //\'\)(/()(`)() \*/
/*\  )`()( )(/'            (_)    /=\    ~/_    (_)             `\)( )()'(  /*/
/*\\  ()()(`)(/         (_) ____ /=\ ____ ~/_ ____ (_)          \)(')()()  //*/
/*\\\---------------------------------------------------------------------///*/
