#include "Include.h"

int CurrLineNum = 0;


/* my own version of fgets, to avoid problems which occur with 
   240-character lines */

FGetS(char Line[],FILE * FPtr)
{  fgets(Line,240,FPtr);
   if (Line[strlen(Line)-1] != '\n')  {
      printf("line exceeds 240-character limit\n");
#if 1
      Line[strlen(Line)-1] = '\n';
      printf("%s\n",Line); 
      PERR(CurrLineNum,Line); 
#endif
      exit();
   }  
}

    
ParseCommand(CmdLine,ArgcPtr,Argv,StrLength)
   char CmdLine[];  int *ArgcPtr;  char *Argv;  int StrLength;

/*  parses a command line in a manner like argc and argv

    takes CmdLine as input, and counts Argc, the number
    of items in the line, and puts the items themselves
    into Argv[0], Argv[1], ...  

    items are separated by either spaces or commas
    
    it is assumed that there will be at most 5 items  */ 

{  int I,LineLength = 0,LLMinus1,Count = 0;

   for (I = 0; I < 240; I++)  {
      if (CmdLine[I] == 0 || CmdLine[I] == '\n') break;
      if (CmdLine[I] == ',') CmdLine[I] = ' ';
   }
   LineLength = I;
   
   if (LineLength == 0)  {
      *ArgcPtr = 0;
      return;
   }
   
   if (LineLength == 1)  {
      if (CmdLine[0] == ' ') *ArgcPtr = 0;
      else  {
         *ArgcPtr = 1;
         *Argv = CmdLine[0];
      }
      return;
   }
    
   LLMinus1 = LineLength - 1;
   for (I = 0; I <= LLMinus1; )  {
      while (CmdLine[I] == ' ') I++;
      if (I <= LLMinus1)  {
         sscanf(CmdLine+I,"%s",Argv+Count*StrLength);
         I += strlen(Argv+(Count++)*StrLength);
      }
      else break; 
   }
   *ArgcPtr = Count;
   return;
 }


MemSet(MemPtr,C,NB)
   char *MemPtr,C;  int NB;

/* sets bytes MemPtr, MemPtr+1, ..., MemPtr+NB-1 to C */

{  int I;

   for (I = 0; I < NB; I++) *MemPtr++ = C;
}


unsigned Value(F)
   double F;

/* copies (byte-for-byte) a float to an unsigned; call with float as
   actual parameter corresponding to the formal parameter F;
   the actual parameter will be promoted to double */

{  int I,Tmp;  char *P1,*P2;  float FSingle;

   FSingle = F;
   P1 = (char *) &FSingle;  P2 = (char *) &Tmp;
   for (I = 0; I < sizeof(int); I++)
      *(P2++) = *(P1++);
   return(Tmp);
}


float FloatValue(L)
   int L;

/* copies (byte-for-byte) an int to a float */

{  int I;  float Tmp;  char *P1,*P2;

   P1 = (char *) &L;  P2 = (char *) &Tmp;
   for (I = 0; I < sizeof(int); I++)
      *(P2++) = *(P1++);
   return(Tmp);
}


CheckMallocError(Ptr,PtrName)
   void * Ptr;  char PtrName[];


{  if (!Ptr)  {
      printf("malloc error for %s\n",PtrName);
      exit();
   }
}


