text.c
contents ::
  text.c
  input

/**********************************************
 *                                            *
 * Text Editor thing                          *
 *                                            *
 * We neeed to create a buffer the contains   *
 * all remaining lines, I am not sure how to  *
 * implant these back into the document.      *
 *                                            *
 * I have now added a two dimensional         *
 * buffer, but it didn't immediately work     *
 * bummer!                                    *
 *                                            *
 * James Little Sunday 19/8/01                *  
 *                                            *
 **********************************************/


#include <stdio.h>
#include <stdlib.h>

// DEFINE OUR OWN BOOLEAN TYPE
#define boolean int
#define true 1
#define false 0

// DEFINE STR-BUFFER FUNCTION
void create_buffer(char **s, char **buffer, int j, int i);

// DEFINE INSERT-BUFFER FUNCTION
void insert_buffer(char **s, char **buffer, int j, int i);

// DEFINE RETURN STR-LEN FUNCTION
int find_end(char *s);

// DEFINE INSERT_CHAR FUNCTION
void insert_char(char *s, int *j, int *i, char c);

// DEFINE INSERT OR OVERWRITE MODE
enum cond { INS, OVR };

// MAKE CHAR NUMS and CURRENT_BUFFER GLOBAL FOR GOODNESS
int num_chars = 0;
int max_chars = 80;
boolean current_buffer = false;

 
main(){
  char *s[max_chars];
  char *buffer[max_chars];
  int i=0;
  char a = '\0';
  char b = '\0';
  char c = '\0';
  int j=0;
  int q=0;
  boolean op = false;
  int mode = INS;

  for(i=0; i<max_chars; i++){
    buffer[i] = malloc(max_chars * sizeof buffer[0][0]);
    s[i] = malloc(max_chars * sizeof s[0][0]);
    buffer[i][0] = '\0';
    s[i][0] = '\0';
  }

  i=0;

  while((c = getchar())!=EOF){
    if(b == '\\'){
      if(c == 'I'){
         // INSERT MODE
         mode = INS;
         op = true;
         
      } else if(c == 'O'){
         // OVERWRITE MODE
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;
         //printf("inside overwrite mode <buffer> : %s\n", buffer[0]);
         //break;
         //for(q=0; s[q][0] != '\0'; q++){
         //  printf("OVR line %d:\t%s",q, s[q]);
         //}
         //printf("\n");
         mode = OVR;
         op = true;

      } else if(c == 'B'){
         // MOVE TO BEGINING OF LINE
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;

         j = 0;
         create_buffer(s, buffer, j, i);
         current_buffer = true;
         op = true;
      } else if(c == 'E'){
         // MOVE TO END OF LINE
         op = true;
         if(current_buffer)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;
           
         j = find_end(s[i]);
         create_buffer(s, buffer, j, i);
         current_buffer = true;
         //printf("inside move to end of line <buffer> : %s\n", buffer);
         //insert_char(s[i], &j, &i, '>');
       } else if(c == 'U'){
         // MOVE UP ONE LINE
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;
         
         if(i>0){
           i--;
           create_buffer(s, buffer, j, i);
           current_buffer = true;
           //printf("inside move up one line <buffer> : %s\n", buffer[0]);
           //insert_buffer(s[i], buffer, j);
           op = true;
         }
       } else if(c == 'D'){
         // MOVE DOWN ONE LINE
         //if(s[i+1][0] != '\0')
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;

         if(i<max_chars){
           i++;
           j = find_end(s[i]);
           //  printf("inside down one line <j> : %d  <s[i][j]> : %c\n",j, s[i][j]);
           create_buffer(s, buffer, j, i);
           current_buffer = true;
           //insert_buffer(s[i], buffer, j);
           op = true;
         }
       }/* else if(c == 'L'){ */
      /*           // MOVE ONE CHARACTER TO THE LEFT */
/*           if(j>0) */
/*             j--; */
/*           create_buffer(s[i], buffer, j); */
/*           op_fin = true; */

/*        } else if(c == 'R'){ */
/*           // MOVE ONE CHARACTER TO THE RIGHT */
/*           if(j<max_chars) */
/*             j++; */
/*           create_buffer(s[i], buffer, j); */
/*           op_fin = true; */

/*        } else if(c == 'S'){ */
/*           // MOVE TO THE START OF THE FILE */
/*           i=0; */
/*           j=0; */
/*           create_buffer(s[i], buffer, j); */
/*           op_fin = true; */

       else if(c == 'F'){
         // MOVE TO THE END OF THE FILE         
         int z=0;
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;

         while(s[z][0] != '\0')
           z++;
         i = z-1;
         j = find_end(s[i]);
         //create_buffer(s[i], buffer, j);
         //printf("end of file <line %d>", i);
         op = true;

      } else if(c == 'K'){
         // DELETE CURRENT CHARACTER
           create_buffer(s, buffer, j+1, i);
         current_buffer = true;
         op = true;
      } else if(c == 'X'){
         // OUTPUT FILE AND EXIT
         int q=0;
         if(current_buffer && mode == INS)
           insert_buffer(s, buffer, j, i);
         current_buffer = false;

         for(q=0; s[q][0] != '\0'; q++){
           printf("%s",s[q]);
         }
         printf("\n");
         exit(0);

      } else if(c == '\\'){
         // ??
         insert_char(s[i], &j, &i, c);
      }   
    } else if(!op && c!='\\'){
      insert_char(s[i], &j, &i, c);
    }
    b = c;
    // a = b;
    if(c != '\\')
      op = false;
  } // end while(()!=EOF)

  for(q=0; s[q][0] != '\0'; q++){
    printf("line %d:\t%s",q, s[q]);
  }
  printf("\n");
  return EXIT_SUCCESS;
}

// WE NEED TO CREATE A BUFFER TO CONTAIN THE REMAINING STR
// FOR OPS LIKE DELETE, INS, ETC.
void create_buffer(char **s, char **buffer, int j, int i){
  int z=0;
  int x=0;
  while(s[i][0]!='\0'){
    while(s[i][j]!='\0'){
      buffer[x][z++] = s[i][j++];
    }
    buffer[x][z] = '\0';
    x++;
    i++;
  }
  buffer[x][z] = '\0';
}

// INSERT BUFFER IS USED AFTER WE HAVE DELETED A CHARACTER OR INS SOME CHARS
void insert_buffer(char **s, char **buffer, int j, int i){
  int z=0;
  int x=0;
  while(buffer[x][0]!='\0'){
    while(buffer[x][z]!='\0'){
      s[i][j++] = buffer[x][z++];
    }
    s[i][j] = '\0';
    x++;
    i++;
  }
  s[i][j] = '\0';
}

// THIS IS THE SAME AS STR_LEN, WE NEED IT FOR THAT PURPOSE
int find_end(char *s){
  int z=0;
  while(s[z] != '\n' && s[z] != '\0'){
    z++;
  }
  if(s[z] == '\0'){
    s[z] = '\n';
    s[z+1] = '\0'; 
  }
  return z;
}

// INSERT_CHAR AVOIDS THE USE OF GOTOS AND REPEATED CODE FOR THIS PURPOSE:
void insert_char(char *s, int *j, int *i, char c){
  if(num_chars<max_chars){
    s[(*j)++] = c;
    if(c == '\n'){
      s[*j] = '\0';
      *j=0;
      (*i)++;
    }
  }
}

James Little