Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "memory.h"
#define INITIAL 64
#define REALLOCATE ( INITIAL + 64 )
char *GetInputLine(FILE *f)
{
size_t stBufferSize, stInput;
char *pBuffer = NULL, *pSaveBuffer = NULL, *pInput = NULL;
if ( ( pBuffer = malloc ( INITIAL ) ) == NULL )
return NULL;
stBufferSize = stInput = INITIAL;
pInput = pBuffer;
if ( fgets ( pInput, stInput, f ) == NULL ) {
free ( pBuffer );
return NULL;
}
while ( ( pSaveBuffer = strchr ( pInput, '\n' ) ) == NULL ) {
stInput = REALLOCATE + 1;
stBufferSize += REALLOCATE;
if ( ( pSaveBuffer = realloc ( pBuffer, stBufferSize ) ) == NULL )
return pBuffer;
pBuffer = pSaveBuffer;
pInput = pBuffer + ( stBufferSize - REALLOCATE - 1 );
if ( fgets ( pInput, stInput, f ) == NULL ) {
free ( pBuffer );
return NULL;
}
}
*pSaveBuffer = '\0';
stInput = pSaveBuffer - pBuffer;
if ( ( pSaveBuffer = realloc ( pBuffer, stInput + 1 ) ) == NULL )
return pBuffer;
return pSaveBuffer;
}
/*The function MAIN*/
int main()
{
int n,i;
char szCurrentLine[MAXLINE] = {0};
char newline[MAXLINE];
//char filename;
char filename[128] = {0};
char s,t;
char *l;
/*Allocate memory*/
struct list *buffer = MALLOC(sizeof (struct list));
/*initialize the list*/
buffer->head = 0;
buffer->tail = 0;
buffer->size = 0;
buffer->currentpos = 0;
buffer->current = 0;
/*-----------------------------------------------*/
/*-------------Computer-User Dialog--------------*/
/*-----------------------------------------------*/
/*Welcome Message!*/
printf("\t--------------------------------\n");
printf("\t| Welcome to TExt EDItor v0.01 |\n");
printf("\t--------------------------------\n");
printf("\t| ()**() |\n");
printf("\t| ( T T ) |\n");
printf("\t| (_ee_) |\n");
printf("\t| (d) (i) |\n");
printf("\t| (_)----(_) |\n");
printf("\t--------------------------------\n");
printf("Insert command (or h for help):\n");
while(1) //To repeat the dialog!
{
printf("TEEDI>");
//scanf("%s",&l);
l = GetInputLine (stdin);
if(l[0]=='h'){
/*List of commands*/
printf("You can insert the following commands:\n");
printf("\t1. r, to read a file.\n");
printf("\t2. w, to save the text in a file.\n");
printf("\t3. p, to print the current line.\n");
printf("\t4. g, to change the current line.\n");
printf("\t5. d, to delete the current line.\n");
printf("\t6. b, to insert a new line, before the current line.\n");
printf("\t7. a, to insert a new line, after the current line.\n");
printf("\t8. q, to exit\n");
}
else if (l[0]=='r'){
/*Dialog to read the File */
printf("Insert filename to be read:\nTEEDI>");
//scanf("%s", &filename);
strcpy(filename, GetInputLine (stdin));
printf("filename = %s\n", filename);
ReadFile(buffer, filename);
}
else if (l[0]=='w'){
/*Dialog to write/store the File*/
printf("Insert filename to be written:\nTEEDI>");
//scanf("%s", &filename);
strcpy(filename, GetInputLine (stdin));
WriteFile(buffer, filename);
}
else if (l[0]=='p'){
/*Print the Line*/
printf("The current line is:\n");
PrintLine(buffer);
}
else if (l[0]=='g'){
/*Dialog to change current line*/
printf("Insert the number of the line you want to make current:\nTEEDI>");
strcpy(szCurrentLine, GetInputLine (stdin));
//scanf("%d",&n);
// GoToLine(buffer, n);
GoToLine(buffer, atoi(szCurrentLine));
}
else if (l[0]=='d'){
/*Delete the line*/
DeleteLine(buffer);
}
else if (l[0]=='b'){
fflush(stdout); /*To clean up the memory!*/
/*Dialog to insert a new line before current line.*/
printf("Insert the new line:\nTEEDI>"); /*The function works but I can't read the line correctly*/
strcpy(newline, GetInputLine (stdin));
// fgets(newline, MAXLINE,stdin); /*If i put the line directly in InsertBefore it gets inserted*/
InsertBefore(buffer, newline); /*The same at Insert after*/
}
else if (l[0]=='a'){
fflush(stdout);
/*Dialog to insert a new line after current line.*/
printf("Insert the new line:\nTEEDI>");
strcpy(newline, GetInputLine (stdin));
//fgets(newline,MAXLINE,stdin);
InsertAfter(buffer, newline);
}
else if (l[0]=='q'){
/*Exit the programm*/
exit(2);
}
else{
/*Dialog in case of False command.*/
printf("ERROR! False command! Try again or give command h to see a list whith all the available");
printf(" commands.\n");
}
}
}