Help with C Text Editor

Asked

Viewed 856 times

-1

I’m having some trouble making a text editor there will some of them:

1 - I don’t know how to delete a line;

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

3 - Put options as ctrl+s to save ctrl+f to look for;

This is my code, if anyone can help me I’d appreciate it, I’m 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;
}
  • 2

    Hello, Giovanni. Welcome to Stack Overflow in English. Your question, as it stands, is very wide. Try to break it into more specific (and smaller) questions and you’ll have a better chance of finding help.

  • Is it C or C++? Not always what you do in one serves another.

  • 1

    @Diegofelipe Even if it is c++ it seems that Giovanni is not using any language specifics.

  • Do you know anything about data structures? Inverted lists, B-Tree, those things?

  • @Intruder yes, yes I revoked the problem of erasing and moving the arrow but I can’t edit that list and only delete the last element of the list, another thing I couldn’t find was how to read Ctrl+some key

  • @Giovanni Qual compiler and S.O. (name and version) you are using to develop?

Show 1 more comment

1 answer

0


Your problem lies in considering 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. (Became philosophical was not?);

I noticed that your data structure for the file is based on a list chained to the lines of the document, when in fact, you should consist information for a more granular form (characters? words?). This is because you will need to save formatting and positioning information in the same positioning granularity and then 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, because 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 of 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 a clear and a printAllNodes to update the place of the edit pointer to each mouse or keyboard drive. The position of the current in the list is the position of the cursor, very easy.

Attention here, nay use an Array to represent your text. Although the data structure is easier to maintain, traverse, 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 Index of the vector will be the maximum integer of the system.

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

For the sake of viewing, you need to block the text while searching. So you need an edit mode and a search mode in the program.

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

In localization mode, you only accept 'exit' (back to edit mode), search (search a word) and 'locate next'.

I suppose that makes it easier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.