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

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

#ifdef HEADER

typedef struct SMLoad SMLoad;
struct SMLoad {
   char *out;			// Output string, or 0
   char *fnam;			// Filename
   gzFile *in;			// Input stream
   FILE *in_pipe;		// Pipe input stream
};

#else

#include "all.h"


//
//	SMLoad
//

SMLoad *
smload(char *fnam) {
   SMLoad *ld= ALLOC(SMLoad);
   ld->in= gzopen(fnam, "r");
   ld->fnam= StrDup(fnam);
   if (!ld->in) 
      error("Unable to open input file: %s", fnam);
   return ld;
}

SMLoad *
smload_sorted(char *fnam) {
   SMLoad *ld= ALLOC(SMLoad);
   char *tmp= StrDup(fnam);
   ld->fnam= StrDupF("LC_ALL=C sort %s", tmp); 
   free(tmp);
   ld->in_pipe= popen(ld->fnam, "r");
   if (!ld->in_pipe) 
      error("Unable to open input command (%s): %s", strerror(errno), ld->fnam);
   return ld;
}

int 
smload_do(SMLoad *ld) {
   char buf[1024];
   char *p;
   int rc;

   if (ld->out) return 0;	// Nothing to do until the output string is removed
   if (!ld->in && !ld->in_pipe) {
      ld->out= StrDup(SMEOF);
      return 0;
   }

   if (ld->in) {
      errno= 0;
      if (!gzgets(ld->in, buf, sizeof(buf))) {
	 if (errno) 
	    error("Error reading file (%s): %s", strerror(errno), ld->fnam);
	 gzclose(ld->in);
	 ld->in= 0;
	 ld->out= StrDup(SMEOF);
	 return 0;
      }
   } else {
      errno= 0;
      if (!fgets(buf, sizeof(buf), ld->in_pipe)) {
	 if (errno) 
	    error("Error reading file (%s): %s", strerror(errno), ld->fnam);
	 if (-1 == (rc= pclose(ld->in_pipe)))
	    error("Error closing command pipe (%s): %s", strerror(errno), ld->fnam);
	 if (!WIFEXITED(rc))
	    error("Command pipe failed with exit code %08X: %s", rc, ld->fnam);
	 if (0 != WEXITSTATUS(rc))
	    error("Command pipe failed with exit status %d: %s", WEXITSTATUS(rc), ld->fnam);
	 ld->in_pipe= 0;
	 ld->out= StrDup(SMEOF);
	 return 0;
      }
   }

   p= strchr(buf, 0);
   if (p>buf && p[-1] == '\n') *--p= 0;
   if (p>buf && p[-1] == '\r') *--p= 0;

   ld->out= StrDup(buf);
   return 1;
}
   
void 
smload_del(SMLoad *ld) {
   free(ld->out);
   free(ld->fnam);
   if (ld->in) gzclose(ld->in);
   free(ld);
}

#endif

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