/*Amiga Computing*/
/*Code Clinic*/
/* files*/
/*A500 upwards*/
/*c source code*/

#include <time.h>                     


#define MAXLEN 30                     /*maximum number of input lines*/
#define MAXCOLS 30                    /*maximum length of input lines*/
#define LINELEN 75                    /*length of output buffer*/     
#define MAXLINESONPAGE 20             /*number of lines per page*/
#define DELIM ','                     /*chosen end of line delimiter*/
#define DELIM2 '\n'                   /*chosen end of record delimitor*/ 

long timeval;                         /*current time*/
long dosbase=0;                       /*pointer for dos libraries*/
long filehandle=0;                    /*filehandle pointers*/
long filehandle2=0;
long bytes=0;                         /*bytes read*/
long lineno=0;                        /*current line number*/
long pageno=0;                        /*current page number*/
long lastline=0;                      /*number of address lines*/
char filename[40];                    /*buffers for filenames*/                         
char filename2[40];
char buffer[(MAXCOLS)*(MAXLEN+2)];    /*input buffer for current record*/
char rline[LINELEN];                  /*buffer for current output line*/


/*prototypes*/
void cleanup(),fillspace(),newline();
void putline(),newpage(),writeadd(),itoa(),reverse(),putstring();
int openlibraries(),readfile();

main(argc,argv)
int argc;
char *argv[];
{
long libraries=0;
if (argc!=3)                         /*no input and output filenames*/
  {
     printf("usage: fileprog inputfile outputfile\n ");
     exit(1);
  }
  time(&timeval);                      /*find current time*/
  strcpy(filename,argv[1]);            /*copy filenames to buffers*/
  strcpy(filename2,argv[2]);
  libraries= openlibraries();          /*open libraries and files*/
  if (libraries != 0)                  /*catch errors*/
    {
      switch(libraries)
        {
           case 1:
             cleanup("no libraries\n");
             break;
           case 2:
             cleanup("no input file\n");
             break;
           case 3:
             cleanup("cannot open output file\n");
             break;
           default:
             cleanup("error\n");
             break;
       }  
     exit(1);
    };
  newpage();                           /*header for first output file page*/
  while (readfile()==0) writeadd();    /*read and write the file*/
  cleanup("ok\n");                     /*close libraries and files*/
  return(0);
}
/*open libraries and files*/
int openlibraries()       
{
  dosbase= OpenLibrary("dos.library",0);       /*open dos library*/
  if (dosbase == 0) return(1);
  filehandle=Open(filename,1005);              /*open input file*/
  if (filehandle == 0) return(2);
  filehandle2=Open(filename2,1006);            /*open output file*/
  if (filehandle2 == 0) return(3);
  return(0);
}

/*close any libraries and files that are open*/
void cleanup(errormsg)                       
{                                            
  printf(errormsg);
  if (dosbase !=0) CloseLibrary(dosbase);
  if(filehandle!=0) Close(filehandle);
  if(filehandle2!=0) Close(filehandle2);
  return;
}

/*read a record*/
int readfile()
{
  int i;                      /*line counter*/
  int j;                      /*character counter*/
  char *pointer,*pointer2;    
  pointer= buffer;            /*pointer to current line*/
  pointer2=pointer;           /*pointer to current character*/
  bytes=1;
  lastline=-1;
  for (i=0;i<MAXLEN;i++)     /*for each line, if not end of file or record*/
   {
    j=0;
    while((bytes>0)&&(*(pointer2-1)!=DELIM)&&(*(pointer2-1)!=DELIM2)&&(j<MAXCOLS))
      {
         bytes=Read(filehandle,pointer2++,1);  /*read a byte*/
         j++;                                  /*increase character counter*/
       }
if ((*(pointer2-1)==DELIM)||(j==MAXCOLS))    /*end of line*/
    {
      *(pointer2-1)='\0';    /*put string terminator instead of delimitor*/
      pointer+=MAXCOLS;      /*update line pointer*/
      pointer2=pointer;      /*update character pointer*/
      lastline++;            /*update line count*/
    }
if (*(pointer2-1)==DELIM2)     /*end of record*/
    {
     *(pointer2-1)='\0';     /*put string terminator instead of newline*/
      return(0);             /*return to main program*/
    }
if (bytes == 0) return(1);   /*return to main program, no more data*/
 }
   *(pointer2-1)='\0';
   return(0);                /*return to main program*/
}

/*fill a buffer withn chosen characters*/
void fillspace(buffer,n,c)
char *buffer;                           /*buffer address*/
int n;                                  /*number required*/
char c;                                 /*chosen character*/
{
  int i;
  for (i=0;i<n;i++) *(buffer+i)=c;
  return;
}

/*copy a character string, without the string terminator */
void putstring(buffer,buffer2)
char *buffer,*buffer2;
{
  int i,j;
  j=strlen(buffer2);                          /*find length*/
  for (i=0;i<j;i++) *(buffer+i)=*(buffer2+i); /*copy the string*/
  return;
}

/*send new line characters to output file*/
void newline(n)
int n;                                   /*number of lines required*/
{
  int i;
  if (n==0) return;                      /*trivial case*/
  for (i=0;i<n;i++)                      /*write required number new lines*/
     {
       Write(filehandle2,"\n",1);
       lineno++;                         /*update line number*/
     }
  return;
}
/*write output buffer to output file*/
void putline()
{
  Write(filehandle2,rline,LINELEN);       /*write output buffer to file*/
  fillspace(rline,LINELEN,' ');           /*clear output buffer*/
  newline(1);                             /*send one new line character*/
  return;
}

/*new page headings*/
void newpage()
{
  char dspage[4];
  fillspace(rline,LINELEN,' ');            /*clear output line buffer*/
  Write(filehandle2,'\f',1);               /*form feed character*/
  pageno++;                                /*update page number*/
  lineno=0;                                /*set line number*/
  putstring(rline,"page");                 /*page number and text*/
  itoa(pageno,dspage);
  putstring(rline+5,dspage);
  putstring(rline+20,"List of addresses"); /*title*/
  putstring(rline+50,ctime(&timeval));     /*time and date*/
  putline();                               
  newline(2);
  putstring(rline,"Name");                 /*sub titles*/
  putstring(rline+20,"Phone");
  putstring(rline+40,"Address");
  putline();
  newline(1);
  return;
}

/*write name, phone number, address to output file*/
void writeadd()
{
  int i;
  if (lineno>MAXLINESONPAGE) newpage();      /*start new page if necessary*/
  putstring(rline,buffer);                   /*name*/
  putstring(rline+20,buffer+MAXCOLS);        /*phone number*/
  for (i=0;i<lastline;i++)                   /*address lines*/
   {
     putstring(rline+40,buffer+(2+i)*MAXCOLS);
     putline();
   }
  if (lastline==0) putline();                /*allow for missing address*/
  newline(1);                                /*skip a line */
  for (i=0;i<lastline;i++) fillspace(buffer+i*MAXCOLS,MAXCOLS,'\0');
  return;
}
/*this rouutine converts an integer into a character string for display*/
void itoa(n,s)
char *s;                              /*address of start of string*/
int n;                                /*integer to be displayed*/
{
   int i,sign;                        /*temporary variables*/
   if ((sign = n)<0) n=-n;            /*store the sign in case number is 
                                      negative*/
   i=0;
   do                                 /*starting from least significant digit*/
        {
           *(s+(i++)) = n%10+'0';     /*put ascii form of digit in string*/
        } 
   while ((n/=10)>0);                 /*find next digit*/
   if  (sign<0) *(s+(i++)) = '-';     /*restore the sign*/
   *(s+i) = '\0';                     /*put instring terminator*/
   reverse(s);                        /*put most significant digit first*/
   return;
}

/* this routine reverses a string of characters*/
void reverse(s)
char *s;                              /*address of start of string*/
{
  int c,i,j;                          /*temporary variables*/
  for (i=0,j=strlen(s)-1;i<j;i++,j--) /*working from both ends of the string*/
    {
        c=*(s+i);                     /*swap corresponding characters*/
        *(s+i)= *(s+j);
        *(s+j)=c;
    }
  return;
}
 