/* Copyright (C) 1997, Alexander Pukhov 
 * Copyright (C) 2001-2007, CompHEP Collaboration
 *---------------------------------------------------
 *$Id: saveres.c,v 1.3 2003/11/08 19:46:05 kryukov Exp $
 *
 *$Log: saveres.c,v $
 *Revision 1.3  2003/11/08 19:46:05  kryukov
 *To bring into according the usage of messanykey and warnanykey functions.
 *
*/
#include <limits.h>
#include "chep_crt/include/chep_crt.h"
#include "chep_crt/include/crt.h"
#include "service2/include/tptcmac.h"
#include "polynom/include/polynom.h"
#include "service2/include/syst2.h"
#include "service2/include/s_files.h"

#include "physics.h"
#include "pvars.h"
#include "sos.h"
#include "ghosts.h"
#include "rfactor.h"
#include "screen.h"
#include "saveres.h"

denom_struct denom[2 * maxvert - 2];
byte denrno;

char denStr[2 * maxvert - 2][MAXINOUT];

static void 
wAbort (void)
{
  saveent (menulevel);
  warnanykey (5, 20, "Error in writing on the disk. \n"
	      "Check the existence of the \n"
	      "'tmp' and 'results' directories \n"
	      "or the existence of free disk space");
  finish ("Error...");
  exit (0);			/*  End of work  */
}



static void 
savevars (FILE * file, polyvars * v)
{
  FWRITE1 (v->nvar, file);
  if (v->nvar &&
   fwrite (v->vars, (v->nvar) * sizeof (varinfo), 1, file) != 1)
    wAbort ();
}

static void 
savepoly (FILE * file, poly p, polyvars * v)
{

  int width;
  char *b, *e;
  poly zero = plusone ();

  zero->coef.num = 0;

  b = (char *) &(zero->coef.num);
  if (v->nvar)
    e = (char *) &(zero->tail.power[v->vars[v->nvar - 1].wordpos]);
  else
    e = (char *) &(zero->tail.power[0]);
  width = e - b;

  while (p)
    {
      if (fwrite (&(p->coef.num), width, 1, file) != 1)
	wAbort ();
      p = p->next;
    }

  if (fwrite (&(zero->coef.num), width, 1, file) != 1)
    wAbort ();
}


void 
save_analitic_results (FILE * fres, poly rnum, poly factn, poly factd, polyvars * vars1, polyvars * vars2, vcsect vcs)
{
  catrec cr;
  int i;
  int m;

  diskerror = wAbort;

  cr.nsub_ = nsub;
  cr.ndiagr_ = ndiagr;

  cr.factpos = ftell (fres);
  savevars (fres, vars2);
  savepoly (fres, factn, vars2);
  savepoly (fres, factd, vars2);

  cr.rnumpos = ftell (fres);
  savevars (fres, vars1);
  savepoly (fres, rnum, vars1);

  cr.denompos = ftell (fres);
  cr.status = 1;
  calcdenominators (vcs);
  FWRITE1 (denrno, fres);        /*  number of demominators  */
  for (i = 0; i < denrno; i++)
    {
      FWRITE1 (denom[i].power, fres);      /*  power  1 or 2  */
      FWRITE1 (denom[i].mass, fres);
      FWRITE1 (denom[i].width, fres);
      m = 0;
      do 
        {
          FWRITE1 (denom[i].momStr[m], fres);
        }
      while (denom[i].momStr[m++]);
    }

  FWRITE1 (cr, catalog);
  diskerror = NULL;
}

void 
save_empty_analitic_results (FILE * fres, vcsect vcs)
{
  catrec cr;
  int i;
  int m;

  diskerror = wAbort;

  cr.nsub_ = nsub;
  cr.ndiagr_ = ndiagr;
  cr.factpos = ftell (fres);
  cr.rnumpos = ftell (fres);
  cr.denompos = ftell (fres);
  cr.status = 0;

  calcdenominators (vcs);
  FWRITE1 (denrno, fres);        /*  number of demominators  */
  for (i = 0; i < denrno; i++)
    {
      FWRITE1 (denom[i].power, fres);      /*  power  1 or 2  */
      FWRITE1 (denom[i].mass, fres);
      FWRITE1 (denom[i].width, fres);
      m = 0;
      do 
        {
          FWRITE1 (denom[i].momStr[m], fres);
        }
      while (denom[i].momStr[m++]);
    }

  FWRITE1 (cr, catalog);
  diskerror = NULL;
}
