📄 sted.c
字号:
save_err = save_file (file_name); if (save_err == 1) { move (12, 0); clrtoeol (); addstr ("Error saving file!"); refresh (); getch (); } move(y, x_pos - left_col); break; } else { file_name[0] = '\0'; continue; } } if (!strlen (file_name)) { move (12, 0); clrtoeol (); addstr ("Filename: "); refresh (); count = 0; do { c = getch (); if (c == KEY_BACKSPACE) { if (count > 0) { file_name[--count] = '\0'; move(12, strlen("filename: ")); clrtoeol(); addstr(file_name); } } else { file_name[count] = c; addch (c); count++; } refresh (); } while (c != '\n'); file_name[count - 1] = '\0'; save_err = save_file (file_name); if (save_err == 1) { move (12, 0); clrtoeol (); addstr ("Error saving file!"); refresh (); getch (); } move (y, x_pos - left_col); refresh (); } break; case (KEY_F0 + 11): /* load a file */ load_err = 0; move (11, 0); clrtoeol (); move (13, 0); clrtoeol (); move (12, 0); clrtoeol (); addstr ("Load a file and clear the current buffer without saving? (Y/N)"); refresh (); c = getch (); if (c == 'y' || c == 'Y') { count = 0; move (12, 0); clrtoeol (); addstr ("Filename: "); refresh (); c = getch (); while (c != '\n') { if (c == KEY_BACKSPACE) { if (count > 0) { file_name[--count] = '\0'; move(12, strlen("filename: ")); clrtoeol(); addstr(file_name); } } else { file_name[count++] = c; addch (c); } refresh (); c = getch (); } file_name[count] = '\0'; clean_up (); load_err = load_file (file_name, lines_p); if (load_err != 0) { fprintf (stderr, "Error: couldn't load file %s!", file_name); finish (1); } y = x = x_pos = 0; top_line = left_col = 0; line_number = 0; strcpy (buffer, get_line (0)); move (y, x_pos - left_col); } else { move (y, x_pos - left_col); } break; } getsyx (y, x); show_list(top_line, left_col, y, x); move (y, 0); clrtoeol (); for (count = left_col; count < (left_col + (COLS - 1)) && (count < strlen(buffer)); count++) { if (buffer[count] == '\t') addch(' '); else addch(buffer[count]); } move (y, x_pos - left_col); if (colour_on == 1) { attrset(COLOR_PAIR(colour++)); if (colour > 7) colour = 1; } refresh (); /* Updating the screen after each character. */ } finish (0); /* This is the end... */}/************************************************************************** * the finish function * Returns: nothing * Arguments: basically nothing. * * Just cleans up and returns you to the shell. **************************************************************************/static voidfinish (int sig){ clean_up (); endwin (); exit (0);}/************************************************************************** * The clean_up() function. * Returns: nothing * Arguments: none * * Just goes through the linked lists and free()s all allocated memory. *************************************************************************/voidclean_up (void){ rad *temp = head, *prev = NULL; if (head != NULL) { while (temp != NULL) /* Freeing all allocated memory. */ { free (temp->content); prev = temp->next; free (temp); temp = prev; } } head = NULL;}/************************************************************************** * The push_numbers function. * Returns: nothing (probably should, though) * Arguments: line_number, the line at which to begin pushing. * * To make place for a new line, it pushes all the line numbers from that * line up one step, so that we don't end up with several lines with the * same line numbers. **************************************************************************/voidpush_numbers (int line_number){ rad *temp = head; while (temp != NULL) { if (temp->number == line_number) { while (temp != NULL) { ++temp->number; temp = temp->next; } return; } temp = temp->next; }}/************************************************************************* * The add_line function. Allocates memory and adds a line to the text. * Returns: nothing (probably should, though) * Arguments: buffer, the line to be stored. * line_number, the number of the line to store. * * Linked list, not anything I'm very good at, but it seems to work quite * well. *************************************************************************/voidadd_line (char *buffer, int line_number){ rad *new = NULL, *temp = NULL, *prev = NULL; new = (rad *) malloc (sizeof (rad)); if (!new) { fprintf (stderr, "Unable to allocate memory!"); exit (1); } new->content = (char *) malloc (strlen (buffer) + 1); if (!new->content) { fprintf (stderr, "Unable to allocate memory!"); exit (1); } strcpy (new->content, buffer); new->number = line_number; new->next = NULL; if (head == NULL) { head = new; new->next = NULL; } else { if (new->number < head->number) { new->next = head; head = new; } else { temp = head->next; prev = head; if (temp == NULL) { prev->next = new; new->next = NULL; } else { while ((temp->next != NULL)) { if (new->number < temp->number) { new->next = temp; prev->next = new; break; } else { temp = temp->next; prev = prev->next; } } if (temp->next == NULL) { if (new->number < temp->number) { new->next = temp; prev->next = new; } else { temp->next = new; new->next = NULL; } } } } }}/************************************************************************* * The modify_line function. Reallocates memory and modifies a previously * stored line in memory. * Returns: nothing (probably should, though). * Arguments: buffer, the line to be stored. * line_number, the number of the line to be stored. * * Self-explanatory, I suppose. Just reallocates memory and puts the * buffer into the struct, instead of the old line. *************************************************************************/voidmodify_line (char *buffer, int line_number){ rad *temp = head; while (temp != NULL) { if (temp->number == line_number) { temp->content[0] = '\0'; temp->content = (char *) realloc (temp->content, strlen (buffer) + 1); if (temp->content == NULL) { fprintf (stderr, "Failed to reallocate memory!"); exit (1); } strcpy (temp->content, buffer); return; } temp = temp->next; }}/************************************************************************** * The del_line function * Returns: nothing (probably should) * Arguments: line_number, the line to be deleted * * Goes through the linked list until it finds the line to delete, deletes * it and free()s the memory, after linking together the previous and next * structs. Then it decrements the line numbers of each of the following * structs, so that we don't get any holes in the linked list. **************************************************************************/voiddel_line (int line_number){ rad *temp = head, *prev = head; if (head->number == line_number) { head = temp->next; free (temp->content); free (temp); } else { while (temp != NULL) { prev = temp; temp = temp->next; if (temp->number == line_number) { prev->next = temp->next; free (temp->content); free (temp); break; } } } temp = head; while (temp != NULL) { if (temp->number >= line_number) --temp->number; temp = temp->next; }}/************************************************************************** * The get_line function. Finds a specific line and returns it. * Returns: a pointer to the string that was found. * Arguments: line_number, the number of the line to find. * * Searches through the list to see if there's a line that corresponds to * the one we're after. *************************************************************************/char *get_line (int line_number){ rad *temp = head; while (temp != NULL) { if (temp->number == line_number) return temp->content; else temp = temp->next; } return NULL;}/************************************************************************* * The show_list function, prints what's in the memory. * Returns: nothing * Arguments: y, the current y coordinate, x, the current x coordinate, * top_line, the first line to print, left_col, the leftmost column. * * Prints the lines that we've stored in the linked list to the screen. * And of course it doesn't print more than the current screen-full. **************************************************************************/voidshow_list (int top_line, int left_col, int y, int x){ rad *temp = head; int count, count2; erase (); for (count = 0; (temp != NULL) && ((count - top_line) < LINES); count++) { if (temp->number >= top_line) { move (count - top_line, 0); for (count2 = left_col; count2 < (left_col + (COLS - 1)) && (count2 < strlen(temp->content)); count2++) { if (temp->content[count2] == '\t') addch(' '); else addch(temp->content[count2]); } } temp = temp->next; } move (y, x);}/************************************************************************** * The save_file() function * Returns: 0 on success, 1 if there was some error. * Arguments: file_name, the file name (wasn't that quite obvious?) * * Opens a file, writes to it with fputc(), closes it. **************************************************************************/intsave_file (char *file_name){ int error = 0, count = 0; rad *temp = head; FILE *fp; if ((fp = fopen (file_name, "w")) != NULL) { while (temp != NULL) { for (count = 0; count < strlen(temp->content); count++) { if (temp->content[count] == '\t') { fputc(temp->content[count], fp); count += (TAB_SPACING - 1); } else fputc(temp->content[count], fp); } fputc('\n', fp); temp = temp->next; } fclose (fp); } else error = 1; return error;}/*************************************************************************** * The load_file() function * Returns: 0 on success, 1 on failure * Arguments: file_name, the file name and *lines, a pointer the number of * liness, so that I can modify it. * * Quite simply, it opens a file, reads it line by line, puts in spaces * instead of tabs, and stores everything with the add_line()-function. **************************************************************************/intload_file (char *file_name, int *lines){ int error = 0, line = 0, count = 0, count2 = 0, count3 = 0; FILE *fp; char buffer[LINE_LENGTH + 1]; for (count = 0; count < LINE_LENGTH + 1; count++) buffer[count] = '\0'; if ((fp = fopen (file_name, "r")) != NULL) { while (!feof (fp)) { fgets (buffer, LINE_LENGTH + 1, fp); buffer[strlen (buffer) - 1] = '\0'; for (count = 0; count < strlen (buffer); count++) { if (buffer[count] == '\t') { buffer[count] = '\t'; for (count2 = 0; (count2 < TAB_SPACING - 1) && (strlen (buffer) < LINE_LENGTH + 1); count2++) { for (count3 = strlen (buffer); count3 > count + count2; count3--) buffer[count3 + 1] = buffer[count3]; buffer[count + count2 + 1] = ' '; } } } add_line (buffer, line++); for (count = 0; count < LINE_LENGTH + 1; count++) buffer[count] = '\0'; } fclose (fp); del_line(--line); *lines = line; } else error = 1; return error;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -