|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#define MAX_LINE 1024 // Maximum length of a line |
| 6 | + |
| 7 | +typedef struct { |
| 8 | + char **lines; // Array of lines |
| 9 | + int num_lines; // Number of lines in buffer |
| 10 | + int current_line; // Current line (1-based, 0 means none) |
| 11 | + int dirty; // Tracks unsaved changes |
| 12 | +} Editor; |
| 13 | + |
| 14 | +static char *my_strdup(const char *s); |
| 15 | +static void init_editor(Editor *ed); |
| 16 | +static int parse_address(Editor *ed, const char *addr); |
| 17 | +static void append_line(Editor *ed, int addr); |
| 18 | +static void insert_line(Editor *ed, int addr); |
| 19 | +static void print_line(Editor *ed, int addr); |
| 20 | +static void delete_line(Editor *ed, int addr); |
| 21 | +static void write_file(Editor *ed); |
| 22 | +static void free_editor(Editor *ed); |
| 23 | +static void execute_command(Editor *ed, char *cmd); |
| 24 | +static void load_file(Editor *ed, const char *filename); // New function |
| 25 | + |
| 26 | +char *my_strdup(const char *s) { |
| 27 | + if (s == NULL) return NULL; |
| 28 | + size_t len = strlen(s) + 1; // Include null terminator |
| 29 | + char *dup = (char *)malloc(len); |
| 30 | + if (dup == NULL) return NULL; // Allocation failed |
| 31 | + strcpy(dup, s); |
| 32 | + return dup; |
| 33 | +} |
| 34 | + |
| 35 | +static void init_editor(Editor *ed) { |
| 36 | + ed->lines = NULL; |
| 37 | + ed->num_lines = 0; |
| 38 | + ed->current_line = 0; |
| 39 | + ed->dirty = 0; |
| 40 | +} |
| 41 | + |
| 42 | +static int parse_address(Editor *ed, const char *addr) { |
| 43 | + if (!addr || strlen(addr) == 0) return ed->current_line - 1; |
| 44 | + if (strcmp(addr, ".") == 0) return ed->current_line - 1; |
| 45 | + if (strcmp(addr, "$") == 0) return ed->num_lines - 1; |
| 46 | + int line = atoi(addr); |
| 47 | + if (line <= 0 || line > ed->num_lines) return -1; // Invalid |
| 48 | + return line - 1; // Convert to 0-based |
| 49 | +} |
| 50 | + |
| 51 | +static void append_line(Editor *ed, int addr) { |
| 52 | + char line[MAX_LINE]; |
| 53 | + printf("(Enter text, end with '.' on a new line)\n"); |
| 54 | + while (fgets(line, MAX_LINE, stdin)) { |
| 55 | + line[strcspn(line, "\n")] = 0; // Remove newline |
| 56 | + if (strcmp(line, ".") == 0) break; |
| 57 | + |
| 58 | + ed->lines = realloc(ed->lines, (ed->num_lines + 1) * sizeof(char *)); |
| 59 | + ed->lines[ed->num_lines] = my_strdup(line); |
| 60 | + ed->num_lines++; |
| 61 | + if (addr < ed->num_lines - 1) { |
| 62 | + for (int i = ed->num_lines - 1; i > addr + 1; i--) { |
| 63 | + char *temp = ed->lines[i]; |
| 64 | + ed->lines[i] = ed->lines[i - 1]; |
| 65 | + ed->lines[i - 1] = temp; |
| 66 | + } |
| 67 | + addr++; |
| 68 | + } |
| 69 | + } |
| 70 | + ed->current_line = addr + 1; |
| 71 | + ed->dirty = 1; |
| 72 | +} |
| 73 | + |
| 74 | +static void insert_line(Editor *ed, int addr) { |
| 75 | + char line[MAX_LINE]; |
| 76 | + printf("(Enter text, end with '.' on a new line)\n"); |
| 77 | + while (fgets(line, MAX_LINE, stdin)) { |
| 78 | + line[strcspn(line, "\n")] = 0; // Remove newline |
| 79 | + if (strcmp(line, ".") == 0) break; |
| 80 | + |
| 81 | + ed->lines = realloc(ed->lines, (ed->num_lines + 1) * sizeof(char *)); |
| 82 | + for (int i = ed->num_lines; i > addr; i--) { |
| 83 | + ed->lines[i] = ed->lines[i - 1]; |
| 84 | + } |
| 85 | + ed->lines[addr] = my_strdup(line); |
| 86 | + ed->num_lines++; |
| 87 | + addr++; |
| 88 | + } |
| 89 | + ed->current_line = addr; |
| 90 | + ed->dirty = 1; |
| 91 | +} |
| 92 | + |
| 93 | +static void print_line(Editor *ed, int addr) { |
| 94 | + if (ed->num_lines == 0 || addr < 0 || addr >= ed->num_lines) { |
| 95 | + printf("?\n"); |
| 96 | + return; |
| 97 | + } |
| 98 | + printf("%s\n", ed->lines[addr]); |
| 99 | + ed->current_line = addr + 1; |
| 100 | +} |
| 101 | + |
| 102 | +static void delete_line(Editor *ed, int addr) { |
| 103 | + if (ed->num_lines == 0 || addr < 0 || addr >= ed->num_lines) { |
| 104 | + printf("?\n"); |
| 105 | + return; |
| 106 | + } |
| 107 | + free(ed->lines[addr]); |
| 108 | + for (int i = addr; i < ed->num_lines - 1; i++) { |
| 109 | + ed->lines[i] = ed->lines[i + 1]; |
| 110 | + } |
| 111 | + ed->num_lines--; |
| 112 | + ed->lines = realloc(ed->lines, ed->num_lines * sizeof(char *)); |
| 113 | + ed->current_line = addr + 1 > ed->num_lines ? ed->num_lines : addr + 1; |
| 114 | + ed->dirty = 1; |
| 115 | +} |
| 116 | + |
| 117 | +static void write_file(Editor *ed) { |
| 118 | + char filename[MAX_LINE]; |
| 119 | + printf("Enter filename: "); |
| 120 | + fgets(filename, MAX_LINE, stdin); |
| 121 | + filename[strcspn(filename, "\n")] = 0; |
| 122 | + |
| 123 | + FILE *fp = fopen(filename, "w"); |
| 124 | + if (!fp) { |
| 125 | + printf("?\n"); |
| 126 | + return; |
| 127 | + } |
| 128 | + int bytes = 0; |
| 129 | + for (int i = 0; i < ed->num_lines; i++) { |
| 130 | + fprintf(fp, "%s\n", ed->lines[i]); |
| 131 | + bytes += strlen(ed->lines[i]) + 1; // Include newline |
| 132 | + } |
| 133 | + fclose(fp); |
| 134 | + printf("%d\n", bytes); |
| 135 | + ed->dirty = 0; |
| 136 | +} |
| 137 | + |
| 138 | +static void free_editor(Editor *ed) { |
| 139 | + for (int i = 0; i < ed->num_lines; i++) { |
| 140 | + free(ed->lines[i]); |
| 141 | + } |
| 142 | + free(ed->lines); |
| 143 | +} |
| 144 | + |
| 145 | +static void execute_command(Editor *ed, char *cmd) { |
| 146 | + cmd[strcspn(cmd, "\n")] = 0; // Remove newline |
| 147 | + if (strlen(cmd) == 0) return; |
| 148 | + |
| 149 | + char addr_str[MAX_LINE] = ""; |
| 150 | + char op = cmd[strlen(cmd) - 1]; |
| 151 | + if (strlen(cmd) > 1) { |
| 152 | + strncpy(addr_str, cmd, strlen(cmd) - 1); |
| 153 | + addr_str[strlen(cmd) - 1] = 0; |
| 154 | + } else { |
| 155 | + op = cmd[0]; |
| 156 | + } |
| 157 | + |
| 158 | + if (strspn(cmd, "0123456789.$") == strlen(cmd)) { |
| 159 | + int addr = parse_address(ed, cmd); |
| 160 | + if (addr >= 0 && addr < ed->num_lines) ed->current_line = addr + 1; |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + int addr = parse_address(ed, addr_str); |
| 165 | + if (addr < 0 && op != 'a' && op != 'q' && op != 'w') { |
| 166 | + printf("?\n"); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + switch (op) { |
| 171 | + case 'a': append_line(ed, addr < 0 ? ed->num_lines - 1 : addr); break; |
| 172 | + case 'i': insert_line(ed, addr < 0 ? 0 : addr); break; |
| 173 | + case 'p': print_line(ed, addr); break; |
| 174 | + case 'd': delete_line(ed, addr); break; |
| 175 | + case 'w': write_file(ed); break; |
| 176 | + case 'q': if (ed->dirty) printf("?\n"); else exit(0); break; |
| 177 | + default: printf("?\n"); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// New function to load a file into the editor buffer |
| 182 | +static void load_file(Editor *ed, const char *filename) { |
| 183 | + FILE *fp = fopen(filename, "r"); |
| 184 | + if (!fp) { |
| 185 | + printf("?\n"); // File not found or can't be opened |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + char line[MAX_LINE]; |
| 190 | + int bytes = 0; |
| 191 | + while (fgets(line, MAX_LINE, fp)) { |
| 192 | + line[strcspn(line, "\n")] = 0; // Remove newline |
| 193 | + ed->lines = realloc(ed->lines, (ed->num_lines + 1) * sizeof(char *)); |
| 194 | + ed->lines[ed->num_lines] = my_strdup(line); |
| 195 | + if (ed->lines[ed->num_lines] == NULL) { |
| 196 | + printf("?\n"); // Memory allocation failed |
| 197 | + fclose(fp); |
| 198 | + return; |
| 199 | + } |
| 200 | + ed->num_lines++; |
| 201 | + bytes += strlen(line) + 1; // Count bytes including newline |
| 202 | + } |
| 203 | + fclose(fp); |
| 204 | + ed->current_line = ed->num_lines; // Set current line to last line |
| 205 | + printf("%d\n", bytes); // Print byte count like POSIX ed |
| 206 | +} |
| 207 | + |
| 208 | +int main(int argc, char *argv[]) { |
| 209 | + Editor ed; |
| 210 | + init_editor(&ed); |
| 211 | + char cmd[MAX_LINE]; |
| 212 | + |
| 213 | + // If a filename is provided as a command-line argument, load it |
| 214 | + if (argc > 1) { |
| 215 | + load_file(&ed, argv[1]); |
| 216 | + } |
| 217 | + |
| 218 | + printf("Simple POSIX ed-like editor in C. Type commands (e.g., 'a', 'p', 'q')\n"); |
| 219 | + while (1) { |
| 220 | + fgets(cmd, MAX_LINE, stdin); |
| 221 | + execute_command(&ed, cmd); |
| 222 | + } |
| 223 | + |
| 224 | + free_editor(&ed); // Unreachable due to infinite loop, but good practice |
| 225 | + return 0; |
| 226 | +} |
0 commit comments