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

时间:2012-03-05 01:40:36

标签: c

我正在为类构建一个程序,它应该作为一个非常基本的基于文​​本的文本编辑器。它有9个可以传递给它的命令,每个命令产生不同的命令,我们使用双向链表来管理可以附加,插入,删除和导航的文本行。我已经获得了一些功能,虽然我认为我的大部分问题都更具概念性,但我会将这些功能作为背景信息的基础提供:

// 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;
}

我的自定义“附加”功能:

//
// 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);
}

这是我的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. 如果我想发送用户从其他功能输入的get_line输入,我是否可以发送get_line stdin来识别屏幕上输入的用户输入?或者我是否必须使用某种形式的fgets向其发送信息?

  2. 如果允许用户通过使用回车键(又名\n)分隔多行来输入,并且预计可以按 CTRL + D 继续使用该函数,如何告诉应用程序使 CTRL + D EOF?

  3. 我的get_line函数接收整个文件,并输出一行。我被指示使用多个get_line调用来获取多行文件,并将每行发送到各自的堆栈条目中。我如何告诉应用程序,“这是同一个文件,但我希望您现在检查下一行而不是之前输出的那一行”?我假设一旦我弄清楚这一点,我可以将相同的逻辑应用于用户即时输入的输入。

  4. 我已被告知get_line功能已完成,所以我觉得在我称之为get_line的地方(例如在read_file中),我需要控制如何通过将get_line读取到遇到read_file的点,将其更改为行尾符号(又称\n,一次向'\0'发送大量内容})并将其发送到get_line,然后以某种方式让read_file继续执行相同的操作,直到达到EOF。但是我也觉得这个功能很多都在get_line函数中......所以我想我对给定函数的实现感到困惑。

    我将来可能会有更多问题,但就目前而言,我认为这个最初的问题足够冗长。我希望能把这些东西搞清楚,其余部分只会在我脑海中点击。谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

append_line函数中还有一些错误,例如您在初始化之前使用new_stack->prev

接下来是我的看法:

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;
}