/* Copyright © 1995-1996 : David Atkinson */
/* V1.22 Filter.c */
/* Calgor */

#include<stdio.h>
#include<fcntl.h>

#include<hold/extra.h>
#include<exec/types.h>
#include<sys/stat.h>
#include<string.h>
#include<stdlib.h>

extern void alert(TEXT *);
extern void clean_local(void);
#ifdef TEST
extern void write_log(TEXT *, SHORT);
#endif

char *buffer=NULL;
char *display=NULL;
char *text[LISTMAX];

int get_source(TEXT *ftitle)
{
void clear(TEXT *,int,int);
void seperate(int);
void code_format(void);

int demfd,err,howbig;
struct stat details;

  err=stat(ftitle, &details);                    /* stat file how big is it   */
  if (err!=0){
    clean_local();
    printf("Couldn't get hold of %s\n",ftitle);
    alert("Couldn't stat file (get_source)\n");
  }
  howbig = details.st_size;
  buffer =(char *) malloc(howbig);      /* Allocate memory for buffer */
  if(buffer==NULL){
    clean_local();
    alert("Failed Memory Allocation (A) (get_source)\n");
  }
  display = (char *) malloc(howbig);    /* Just give me some memory */
  if(display==NULL){
    clean_local();
    alert("Failed Memory Allocation (B) (get_source)\n");
  }
  clear(buffer,0,howbig);
  clear(display,0,howbig);

  demfd=open(ftitle,O_RDONLY);                   /* Open the file for reading */
  if(demfd<0){
    clean_local();
    alert("Couldn't open file (get_source)\n");
  }
  err=read(demfd,buffer,howbig);
  if (err==-1){
    clean_local();
    alert("Error while reading (get_source)\n");
  }
  close(demfd);
  seperate(howbig);
  code_format();
  free(buffer);
  buffer=NULL;
  #ifdef TEST
    write_log("get_source PASSED\n",PRITWO);
  #endif
}

void clear(TEXT *work, int start,int limit)
{
int x;

   #ifdef TEST
     write_log("clear STARTED\n",PRITWO);
   #endif
   for(x=start;x<limit;x++)
     *(work+x)='\0';
   #ifdef TEST
      write_log("clear PASSED\n",PRITWO);
   #endif

}

void seperate(int size){

int count,lastline,path,dismove;
int c,next;

/* Copys text from buffer into display, process this when in memory. */


   /* path 0 = display buffer */
   /* path 1 = Nowhere */
   #ifdef TEST
     write_log("Seperate Started\n",PRITWO);
   #endif
   count=path=dismove=lastline=0;
   c= (int) '0';
   while( (c!=(int) EOF) && (count < size) && (dismove<size) ){
     c= (int) *(buffer+count);
     if( (c!=EOF) && (count<size-1) ){
       next= (int) *(buffer+count+1);
     }
     else{
       next=(int) EOF;
     }
     switch(c){
       case '#' :
         path=1;
         break;
       case '/' :
         if(next=='*'){
           path=1;
           count++;
         }
         else{
           if(path==0){
             *(display+dismove)= (char) c;
             dismove++;
           }
         }
         break;
       case '*' :
         if(next=='/'){
           path=0;
           count++;
         }
         else{
           if(path==0){
             *(display+dismove)= (char) c;
             dismove++;
           }
         }
         break;
       case '\n' :
         if(path==1){
           path=0;
           dismove=lastline;
         }
         else{
           *(display+dismove)='\n';
           dismove++;
           lastline=dismove;
         }
         break;
       case 'a' :
         if (next=='_'){
           path=1;
         }
         else{
           *(display+dismove)=(char) c;
           dismove++;
         }
         break;
       case EOF :
         *(display+dismove)=(char) EOF;
         break;
       default :
         if(path==0){
           *(display+dismove)= (char) c;
           dismove++;
         }
         break;
     }
     count++;
   }
   #ifdef TEST
     if(c==(int) EOF)
        write_log("Seperate Finished On EOF\n",PRITWO);
     if(count>=size)
        write_log("Seperate Finished On count test\n",PRITWO);
     if(dismove>=size)
        write_log("Seperate Finished On dismove test\n",PRITWO);
     if(next==(int) EOF)
        write_log("Next is equal to EOF\n",PRITWO);
     else
        write_log("Next is not equal to EOF\n",PRITWO);
   #endif
   clear(display,dismove,size);
   #ifdef TEST
     write_log("Seperate PASSED\n",PRITWO);
   #endif
}

void code_format(void)
{
int h=0;
char *junk;

    #ifdef TEST
      write_log("Code_format STARTED\n",PRITWO);
    #endif
    junk=display;
    text[h]=(char *) strtok(junk,"\n");
    for(h=1;h<LISTMAX;h++){
      text[h]=strtok(NULL,"\n");
    }
    #ifdef TEST
      write_log("Code_format PASSED\n",PRITWO);
    #endif
}
