Help with Text Editor in C

-1

I'm having some problems with doing a text editor there are some of them:

1 - I do not know how to delete a line;

2 - I can not move with the mouse arrows up and edit a line of text and save in sequence;

3 - Put options like ctrl+s to save ctrl+f to search;

This is my code, if anyone can help me I would appreciate it, I am a beginner in C.

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

#include<conio.h>
#include<string.h>
#include<math.h>

#define praCima 72
#define praCima 72
#define praBaixo 80
#define enter 13
#define esc 27

typedef struct reg no;

struct reg{
    char infoLinha[100];
    no *prox, *ant;
};

void cria_lista (no *lista)
{
     lista = NULL;
} 

void exibeTexto(no *lista, int sel) {
    no *atual;
    atual = lista;
    int cont = 0;
    while(atual != NULL) {
        if (sel >= 0)
            printf("%c %s\n", ((sel == cont) ? '>' : ' '), atual->infoLinha);
        else
            printf("%s%s", ((cont == 0) ? " " : "\n "), atual->infoLinha);
        cont++;
        atual = atual->prox;
    }
}

no* Nelemento(no *lista, int n, int tam) {
    int cont = 0;
    no *elemento;
    elemento = lista;
    if (n > tam-1)
        return NULL;
    while (cont < n) {
        elemento = elemento->prox;
        cont++;
    }       
    return elemento;
}

void escreve() {
    no *lista;
    int sel = 0;
    int tam = 0;
    cria_lista(lista);
    lista = (no*)malloc(sizeof(no));
    lista->ant = lista->prox = NULL;
    tam++;
    strcpy(lista->infoLinha, "");
    no* atual;
    atual = lista;
    int sai = 0;

    char op = 0;
    do {
        system("cls");
        exibeTexto(lista, sel);
        printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
        op = getch();
        switch(op) {
            case enter:     
                system("cls");
                fflush(stdin);
                atual = Nelemento(lista, sel, tam);
                no *p;
                p = (no*)malloc(sizeof(no));
                p->prox = atual;
                p->ant = atual->ant;                
                gets(p->infoLinha);

                atual->ant = p;
                if (p->ant != NULL)
                    p->ant->prox = p;
                if(p->prox == lista)
                    lista = p;
                tam++;
                sel = tam-1;

                break;
            case esc:
                sai = 1;
                break;
            case praCima:
                if (sel > 0)
                    sel--;
                break;
            case praBaixo:
                if (sel < tam-1)
                    sel++;
                break;
        }
        if (sai)
            break;
    } while(op != '2');
}

void escreveSalva() {
    no *lista;
    FILE *arquivo;
     if ((arquivo = fopen("linhas.txt", "r+b")) == NULL) {
        if ((arquivo = fopen("linhas.txt", "wb")) == NULL) {
         printf ("\nErro da abertura do arquivo.\n\n");
         getch();
         return;
        }    
     }
    int sel = 0;
    int tam = 0;
    cria_lista(lista);
    lista = (no*)malloc(sizeof(no));
    lista->ant = lista->prox = NULL;
    tam++;
    strcpy(lista->infoLinha, "");
    no* atual;
    atual = lista;
    int sai = 0;

    char op = 0;
    do {
        system("cls");
        exibeTexto(lista, sel);
        printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
        op = getch();
        switch(op) {
            case enter:     
                system("cls");
                fflush(stdin);
                atual = Nelemento(lista, sel, tam);
                no *p;
                p = (no*)malloc(sizeof(no));
                p->prox = atual;
                p->ant = atual->ant;                
                gets(p->infoLinha);

                atual->ant = p;
                if (p->ant != NULL)
                    p->ant->prox = p;
                if(p->prox == lista)
                    lista = p;
                tam++;
                sel = tam-1;
                if(p->infoLinha != NULL)
                {                   
                   fprintf(arquivo, "%s\n", p->infoLinha);
                }
                break;
            case esc:
                sai = 1;
                break;
            case praCima:
                if (sel > 0)
                    sel--;
                break;
            case praBaixo:
                if (sel < tam-1)
                    sel++;
                break;
        }       
        if (sai)                
           break;       
    } while(op != '2');
}

void menu()
{
    char tc;
    int i,sai=0,sel=0;
    int tamMenu=2;
    while (!sai)
    {
        system("cls");
        printf("Selecione uma das opcoes:\n\n\n");
        char menu[2][50]={"Escrever informacoes e salvar em arquivo","Apenas escrever informacoes sem salvar"};
        for (i=0;i<tamMenu;i++)
        {
            if (sel==i)
            {
                printf("\t      > ");
            }
            else printf("\t        ");
            printf("%s\n",menu[i]);
        }
        printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
        tc = getch();
        switch(tc)
        {
            case praCima: if (sel>0) { sel--; } else sel=tamMenu-1;
            break;
            case praBaixo: if (sel<tamMenu-1) { sel++; } else sel=0;
            break;
            case enter:
                switch(sel)
                {
                    case 0:
                    {
                        escreveSalva();
                    }
                    break;
                    case 1:
                    {
                        escreve();
                    }
                    break;                  
                }
            break;
            case esc: sai=1;
                break;
        }
    }
}



int main(){
    menu();
    return 0;
}
    
asked by anonymous 04.02.2016 / 16:24

1 answer

0

Your problem is to consider the visual representation of the text (file) part of the editing functions, thus having to manipulate the screen during actions that are not part of the editing need. (It was philosophical, was not it?);

I noticed that your data structure for the file is based on a linked list for the document rows, when in fact, you should consist of information for a more granular form (characters? words?). This is because you will need to store formatting and positioning information in the same granularity of the placement and there the movement in the text would just be a pointer to the current structure in the double-chained list.

This more granular structure also makes editing easier, as the main loop is just a check of the button pressed on the keyboard, and changes the pointers to next and previous at the current position in the list.

Something like:

struct reg{
    char letra;/*guarda o caractere, inclusve '\n' */
    int tamanho;
    int fontStyle; /*0 para normal, 1 para negrito, 2 para italico*/
    no *prox, *ant;
};

As for the screen, just clear and printAllNodes to update the place of the edit pointer to each mouse or keyboard move. The position of the current No in the list is the position of the cursor, very easy.

Attention here, not use a Vector to represent your text. Although the data structure is easier to maintain, to navigate, and so on. Vectors have their allocation space created at compile time and either allocate all or nothing. In addition, the vector manipulation index is associated with the integer support of your compiler, so the maximum vector index will be the maximum integer of the system.

Now comes the slightly more complicated part, the data structure for search and location. Well, of course, all search algorithms applied to lists can be easily used here. Because your internal document structure is basically a 'vector' with more information about the current pointer. ;)

As a preview, you need to block the text while searching. So you need an editing mode and a search mode in the program.

The basic difference is the main loop in each mode, in editing you accept the commands required for editing (letters, delete, space, new line, etc. and a Find command).

Already in Location mode, you only accept 'exit' (return to edit mode), search (search for a word) and 'find next'.

I suppose this makes it all easier.

    
05.02.2016 / 15:07