/************************************************************************
*                                                                       *
*         SSSSSS     UU    UU    MM       MM    DATE: 23-03-1990        *
*        SS          UU    UU    MMM     MMM                            *
*       SS           UU    UU    MMMM   MMMM    AUTHOR: A. NEUPER       *
*        SS          UU    UU    MM MM MM MM                            *
*         SSSSS      UU    UU    MM  MMM  MM    COMPILER: AZTEC C 5.0   *
*             SS     UU    UU    MM   M   MM                            *
*              SS    UU    UU    MM       MM    CC: default             *
*             SS      UU  UU     MM       MM                            *
*       SSSSSSS        UUUU      MM       MM    LN: -lm -lc             *
*                                                                       *
************************************************************************/
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <exec/types.h>

main(int argc, char *argv[])
{  FILE  *infile;
   register int i;
   int   tempint=0, printint;
   int    maximal=120, line=0;
   double tempfloat=(double)0.0,printfloat;
   char  *buffer,filename[100],chary;
   int  format=0,start=0,stop=0,begin=0,end=0,query=0,verbose=0;
         /* format:   0-INTEGER  1-EXP.FLOAT */
         /* coloumns: from 'start' to 'stop'; 'stop' sets the EOLlabel(\0) */
         /* lines:    first line examined is 'begin', last one is 'end' */

   if(argc>1)
   if(argv[1][0]=='?')
   {  puts("\nPURPOSE: Sum integers ( or floats ) in a file. (cf. UNIX)");
      puts("\033[33m USAGE:  SUM [ - [?] [V] [L[nnn]-[nnn]] [C[nnn]-[nnn]] [I|F] ] <file>\033[31m");
      puts("MEANING:          Verbose Line           Coloumn      int|float");
      puts("DEFAULT:           (all) 0 - 0    (all) 0 - 0      I");
      puts("COMMENT: Don't use spaces in the option-expression.");
      puts("         int   means '[-]<number>'                      - Format [ atoi() ]");
      puts("         float means '[-]<number>.<number>E[-]<number>' - Format [ atof() ]");
      puts("         No difference is made between uppercase and lowercase letters.");
      puts("         Everything(!) behind the last coloumn will be cut off.\n");
      return(0);
   } else if(argv[1][1]=='?')
   {  puts("To use the default please enter '0' !");
      printf("first  line   >"); scanf("%d",&begin);
      printf("last   line   >"); scanf("%d",&end);
      printf("first coloumn >"); scanf("%d",&start);
      printf("last  coloumn >"); scanf("%d",&stop);
      printf("search format >"); scanf("%s",buffer);
      if(toupper((int)buffer[0])=='F') format=1; else format=0; 
      verbose = 1;
      if(argc==3) { strcpy(filename,argv[2]); }
      else  {  printf("InputFileName >"); scanf("%s",filename); }
      query = 1;
   }

   if( (argc>3) || (argc<2) )
   {
      fputs("ERROR in arg-list !\n",stderr);
       exit(11);
   } else if(argc==3)
   {  if(argv[1][0]=='-')

      {  i=1;
         while(argv[1][i])
         {  switch(toupper((int)argv[1][i++]))
            {  case 'C' :  if(argv[1][i]!='-')
                              start = atoi(&argv[1][i]);
                           while(argv[1][i]!='-')
                              if(!isdigit(argv[1][i++])) return(10);
                           i++;
                           stop = atoi(&argv[1][i]);
                           while(isdigit(argv[1][i])) i++;
                           break;
               case 'L' :  if(argv[1][i]!='-')
                              begin = atoi(&argv[1][i]);
                           while(argv[1][i]!='-')
                              if(!isdigit(argv[1][i++])) return(10);
                           i++;
                           end = atoi(&argv[1][i]);
                           while(isdigit(argv[1][i])) i++;
                           break;
               case 'V' :  verbose=1; break;
               case 'I' :  format=0; break;
               case 'E' :
               case 'G' :
               case 'F' :  format=1; break;
               case '?' :  break;
               default  :  exit(10);
            }
         }
      }
      if(!query) strcpy(filename,argv[2]);
   } else if(!query) strcpy(filename,argv[1]);
   if(!( infile=fopen(filename,"r") ) )
   {  fputs("\nERROR occurred while opening Input!\n"    ,stderr);
      exit(10);
   }
   if((begin>end&&end!=0)||(start>stop&&stop!=0))
   {  fputs("\nYou told me to do nothing!\n",stderr);
      exit(12);
   }
   if(stop>maximal-2) maximal = stop+2;
   if(!(buffer = (char *)calloc(maximal,sizeof(char)))) return(10);
   fgets(buffer,maximal,infile);
   while(!feof(infile))
   {  line++;
      if(start) i = start-1; else i=0;
      if(stop) { chary=buffer[stop]; buffer[stop]=0; }
      if(begin<=line && (end>=line||end==0))
      {  while(buffer[i])
         {  while(!(isdigit(buffer[i]) || buffer[i]=='-' || buffer[i]==0) ) i++;
            if(buffer[i]==0) break;
            if(format)
            {  tempfloat += printfloat = atof(&buffer[i]);
               if(verbose) printf("%22.10f\n",printfloat);
               if(buffer[i]=='-') i++;
               while(isdigit(buffer[i])) i++;
               if(buffer[i]=='.')
               {  i++;
                  while(isdigit(buffer[i])) i++;
               }
               if(toupper((int)buffer[i])=='E')
               {  i++;
                  if(buffer[i]=='-') i++;
                  while(isdigit(buffer[i])) i++;
               }
            } else
            {  tempint += printint = atoi(&buffer[i]);
               if(verbose) printf("%18d\n",printint);
               if(buffer[i]=='-') i++;
               while(isdigit(buffer[i])) i++;
            }
         }
      }
      buffer[stop]=chary; while(buffer[i]) i++;
      while(buffer[i-1]!=10&&stop!=0)
      {  fgets(buffer,maximal,infile);
         i=0; while(buffer[i]) i++;
      }
      fgets(buffer,maximal,infile);
   }
   free((void *)buffer);
   if(verbose)printf(" ญญญญญญญญญญญญญญญญญญญญญญ\n");
   if(format) printf("%22.10f\n",tempfloat);
   else       printf("%18d\n",tempint);

   fclose(infile);
   return(0);
}
