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

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<string.h>
#include<stdlib.h>

#if !defined(LISTMAX)
  #define LISTMAX 100
#endif

#if !defined(TABSIZE)
  #define TABSIZE 7
#endif

extern alert(char *);
extern clean_local(void);

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

int get_source(char *ftitle)
{
void clear(char *,int,int);
void seperate(int);
void look_buf(char *[], 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();
  /* look_buf(text,LISTMAX); */
  free(buffer);
  buffer=NULL;
}

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

   for(x=start;x<limit;x++)
     *(work+x)='\0';
}

void seperate(int size)
{

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

int count,last,path,dismove,widget;
char c,next;

   /* path 0 = display buffer */
   /* path 1 = Nowhere */

   widget=count=path=dismove=last=0;
   c='0';
   while( (c!=EOF) && (count < (size-1)) ){
     c=*(buffer+count);
     if(c!=EOF){
       next=*(buffer+count+1);
     }
     else{
       next=EOF;
     }

     switch(c){
       case '\t':
         widget = dismove;
         while( (dismove<(size-1)) && (dismove<(widget+TABSIZE)) ){
           *(display+dismove)=' ';
           dismove++;
         }
         break;
       case '#' :
         path=1;
         break;
       case '/' :
         if(next=='*'){
           path=1;
           count++;
         }
         else{
           if(path==0){
             *(display+dismove)=c;
             dismove++;
           }
         }
         break;
       case '*' :
         if(next=='/'){
           path=0;
           count++;
         }
         else{
           if(path==0){
             *(display+dismove)=c;
             dismove++;
           }
         }
         break;
       case '\n' :
         if(path==1){
           path=0;
           dismove=last;
         }
         else{
           *(display+dismove)='\n';
           dismove++;
           last=dismove;
         }
         break;
       case 'a' :
         if (next=='_'){
           path=1;
         }
         else{
           *(display+dismove)=c;
           dismove++;
         }
         break;
       case EOF :
         *(display+dismove)='\0';
         break;
       default :
         if(path==0){
           *(display+dismove)=c;
           dismove++;
         }
         break;
     }
     count++;
   }
   clear(display,dismove,size);
}

void look_buf(char *test[], int lard)
{
int win;

   for(win=0;win<lard;win++){
     printf("line %d %s\n",win,test[win]);
   }
}

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

    junk=display;
    text[h]=strtok(junk,"\n");
    for(h=1;h<LISTMAX;h++){
      text[h]=strtok(NULL,"\n");
    }
}
