/*
* Copyright (C) 2001-2008, CompHEP Collaboration
* Copyright (C) 1997, Alexander Pukhov
* ---------------------------------------------------
* $Id$
*
* $Log$
*
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>

#include "chep_limits.h"
#include "syst.h"

void (*diskerror) (void) = NULL;

int 
f_printf (FILE * fp, char *format,...)
{
  va_list args;
  char dump[STRSIZ];
  int r;
  va_start (args, format);

  vsprintf (dump, format, args);
  va_end (args);
  r = fputs (dump, fp);
  if (r == EOF)
    {
      if (diskerror)
        (*diskerror) ();
      else
        exit (0);
    }
  return r;
}


size_t 
f_write (void *ptr, size_t size, size_t n, FILE * fp)
{
  size_t nn;
  if ((size == 0) || (n == 0))
    return 0;
  nn = fwrite (ptr, size, n, fp);
  if (nn != n)
    {
      if (diskerror)
        (*diskerror) ();
      else
        exit (0);
    }
  return nn;
}


char *
trim (char *p)
{
  int n1 = 0, n2, k = -1;
  n2 = (int) strlen (p) - 1;
  while (!isgraph (p[n1]) && n1 <= n2)
    n1++;
  while (!isgraph (p[n2]) && n1 < n2)
    n2--;
  while (++k < n2 - n1 + 1)
    p[k] = p[k + n1];
  p[k] = '\0';
  return p;
}

void 
lShift (char *s, int l)
{
  int i;
  if (l > 0)
    {
      int m = strlen (s);
      for (i = 0; i <= m - l; i++)
        s[i] = s[i + l];
    }
  if (l < 0)
    {
      i = strlen (s);
      while (i >= 0)
        {
          s[i - l] = s[i];
          i--;
        }
      for (i = 0; i < -l; i++)
        s[i] = ' ';
    }
}


void 
revers (void **list)
{
  void **q, **p, **r;

  if (*list == NULL)
    return;
  r = (void **) (*list);
  q = (void **) (*r);
  *r = NULL;
  while (q != NULL)
    {
      p = (void **) (*q);
      *q = (void *) r;
      r = q;
      q = p;
    }
  *list = (void *) r;
}
