简体   繁体   English

C程序-自定义文本编辑器程序

[英]C Program - Custom Text Editor Program

I am building a program for class that is supposed to function as a very basic text-based text editor. 我正在为类构建一个程序,该程序应该用作非常基本的基于文​​本的文本编辑器。 It has 9 commands that can be passed to it, each resulting in a different command and we are using a doubly linked list to manage the lines of text that can be appended, inserted, removed and navigated through. 它有9个可以传递给它的命令,每个命令导致一个不同的命令,并且我们使用双向链表来管理可以添加,插入,删除和浏览的文本行。 I have been given a few functions to work with, and although most of my questions I think are more conceptual, I'll provide these given functions as a basis of background information: 我已经获得了一些可以使用的功能,尽管我认为大多数问题都更具概念性,但我将提供这些给定的功能作为背景信息的基础:

// Function: get_line
// Reads a line (of arbitrary length) from an input source (stdin or file)
// and returns a pointer to the array that stores the line read
//
char *get_line(FILE *f)
{
    int size = 80; // initial size of memory block allocated

    char *linePtr = malloc(size);  // allocate memory block

    if (linePtr != NULL) { // if allocation successful
        int c = EOF;

        // read a line of text and replace newline character
        // with a string terminator character
        int i = 0;
        while ((c = getc(f)) != '\n' && c != EOF) {
            linePtr[i++] = (char) c;
            if (i == size) {
                size *= 2;
                linePtr = realloc(linePtr, size);
            }
        }
        linePtr[i] = '\0';

        // if end-of-file before any characters were read,
        // release the whole buffer
        if (c == EOF && i == 0) {
            free(linePtr);
            linePtr = NULL;
        } else {
            // release unused portion of memory
            linePtr = realloc(linePtr, i+1);
        }
    }

    return linePtr;
}

My self-defined "append" function: 我自定义的“追加”功能:

//
// Function: append_line
// Inserts a line after the current line (or node) in the linked-list
//
void append_line(char *t)
{
    line *new_stack;
    line *tmp;
    new_stack = malloc(sizeof(line));
    tmp = malloc(sizeof(line));

    if((new_stack == NULL) || (tmp == NULL)) {
        fprintf(stderr, "Insufficient memory to allocate. Closing application.\n");
            exit(1);
    }

    if(current_line == NULL) {
        if(head == NULL) {
            head = new_stack;
            current_line = new_stack;
            new_stack->prev = NULL;
            new_stack->next = NULL;
        }
    }
    else {
        tmp = current_line->next;
        current_line->next = new_stack->prev;
        new_stack->next = tmp;
        current_line = new_stack;
    }

    new_stack->text = t;
    free(tmp);
}

And this is my read_file function that doesn't really do anything yet, but I'm not exactly sure if I've got the right mind-set going into the creation of this function: 这是我的read_file函数,它实际上还没有执行任何操作,但是我不确定是否可以正确地将心态投入到此函数的创建中:

// Function: read_file
// Reads text from the specified file and calls append_line to insert lines
// into the linked-list. It returns the number of lines read.
//
int read_file(char *filename)
{
    char * temp, no_command;
    temp = strtok(filename, " ");
    while(temp != NULL) {
        no_command = temp;
        temp = strtok (NULL, " ");
    }
    /* By doing this --^, I hope it will set temp to the actual
    // file name after tokenization and completely ignore
    // the command that comes with filename */
    FILE *fin;
    int counter = 0;

    fin = fopen(no_command, "r");
    if(fin == NULL) {
        printf("You have entered a file that does not exist.");
        exit(0);
    }
    get_line(fin);
    fclose(fin);
}
  1. If I wanted to send get_line input entered by the user from another function, could I send get_line stdin for it to recognize user input typed on-screen? 如果我想发送用户从另一个函数输入的get_line输入,是否可以发送get_line stdin使其识别在屏幕上键入的用户输入? Or would I have to make use of some form of fgets to send it the information? 还是我必须利用某种形式的fgets将信息发送给它?

  2. If the user should be allowed to enter in multiple lines by separating them with the enter key (aka \\n ), and is expected to be allowed to press CTRL + D to continue with the function, how does one tell the application to make CTRL + D the EOF? 如果应该允许用户使用回车键(aka \\n )将它们分开输入多行,并且希望允许用户按CTRL + D继续该功能,那么如何告诉应用程序进行CTRL + d的EOF?

  3. My get_line function takes in an entire file, and outputs a line. 我的get_line函数接收整个文件,并输出一行。 I am instructed to make use of multiple calls of get_line to take files of multiple lines and send each line into their own respective stack entry. 我被指示使用get_line的多个调用来获取多行文件,并将每一行发送到各自的堆栈条目中。 How do I tell the application, "Here's the same file, but I want you to NOW check the next line instead of the one you output previously"? 我如何告诉应用程序“这里是相同的文件,但是我希望您现在检查下一行而不是以前输出的那一行”? I assume that once I figure this out, I can apply the same logic to input entered in on the fly by the user. 我假设一旦弄清楚了这一点,便可以将相同的逻辑应用于用户即时输入的输入。

I have been instructed that the get_line function is complete, so I feel like in the places I would call get_line (such as in read_file ), I would need to control how much is sent to get_line at a time by making read_file read up to the point where a \\n is encountered, change it to an end-of-line symbol (aka '\\0' ) and send it to get_line , and then somehow have read_file continue after that point doing the same thing until EOF is reached. 我被告知get_line函数是完整的,所以我感觉在我将调用get_line的地方(例如在read_file ),我需要通过使read_file读至get_line来控制一次发送到get_line的数量。遇到\\n点,将其更改为行尾符号(aka '\\0' ),然后将其发送到get_line ,然后以某种方式使read_file在该点之后继续执行相同的操作,直到达到EOF。 But I also feel like a lot of this functionality is within the get_line function as well... so I suppose I'm confused on the implementation of my given functions. 但是我也觉得很多功能也都在get_line函数中...所以我想我对给定函数的实现感到困惑。

I'll probably have more questions in the future, but for now, I believe this initial question is long-winded enough. 将来我可能还会有更多问题,但就目前而言,我相信这个最初的问题已经解决了很多。 I'm hoping that figuring these things out, the rest of it will just click in place within my mind. 我希望弄清楚这些事情,其余的事情只需在我的脑海中点击就位。 Thank you for your time! 感谢您的时间!

There are a couple of more errors in the append_line function, for example you use new_stack->prev before it's initialized. append_line函数中还有两个错误,例如,在初始化之前,您使用new_stack->prev

Here comes my take on it: 这是我的看法:

void append_line(char *t)
{
    /* Allocate and clear (i.e. set all to 0) */
    line *new_stack = calloc(1, sizeof(line));

    if(current_line == NULL) {
        if(head == NULL) {
            head = current_line = new_stack;
        }
    }
    else {
        new_stack->next = current_line;
        new_stack->prev = current_line->prev;

        if (current_line->prev)
            current_line->prev->next = new_stack;
        else
            head = new_stack;  /* No previous node, means current_line is at head */

        current_line = new_stack;
    }

    new_stack->text = t;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM