From f7d319ad09efa6d0a1d3b9b392234827539488f1 Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 2 May 2024 14:08:43 +0100 Subject: [PATCH] seperate some functions into multiple files --- .vscode/settings.json | 5 +- Makefile | 2 +- config.c | 2 +- config.h | 2 +- cursorMovement.c | 79 ++++++++++++++++++++ cursorMovement.h | 8 ++ handleCommand.c | 0 lengths.c | 64 ++++++++++++++++ lengths.h | 16 ++++ list.c | 0 main.c | 4 +- openFile.c | 169 +++++------------------------------------- openFile.h | 2 +- render.c | 2 +- 14 files changed, 196 insertions(+), 159 deletions(-) create mode 100644 cursorMovement.c create mode 100644 cursorMovement.h delete mode 100644 handleCommand.c create mode 100644 lengths.c create mode 100644 lengths.h delete mode 100644 list.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 27ca49f..e1c229f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "*.ejs": "html", "ncurses.h": "c", "stdio.h": "c", - "string.h": "c" + "string.h": "c", + "cursormovement.h": "c" } -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index 786c09b..2963e06 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ full: build: mkdir -p bin - gcc -o bin/mte *.c -lncurses + gcc -o bin/mte *.c -lncurses -Wall -Wextra -Werror -pedantic -std=c99 clean: rm -rf bin \ No newline at end of file diff --git a/config.c b/config.c index 81e4349..225f044 100644 --- a/config.c +++ b/config.c @@ -1 +1 @@ -char programName[] = "My Text Editor"; \ No newline at end of file +char programName[] = "My Text Editor"; diff --git a/config.h b/config.h index c927efa..bae2132 100644 --- a/config.h +++ b/config.h @@ -3,4 +3,4 @@ extern char programName[]; -#endif \ No newline at end of file +#endif diff --git a/cursorMovement.c b/cursorMovement.c new file mode 100644 index 0000000..73a7243 --- /dev/null +++ b/cursorMovement.c @@ -0,0 +1,79 @@ +#include "cursorMovement.h" +#include "openFile.h" +#include "lengths.h" + +#include + +void cursorMovement(char choice, struct FileData *fileData, char **content, int *across, int *stop) +{ + int cursorLineLength; + int lineWidth; + switch (choice) + { + case 3: + if ((*fileData).cursor_y > 0) + { + (*fileData).cursor_y--; + int cursorLineLength = lineLength(*content, (*fileData).cursor_y); + (*fileData).cursor_x = *across; + if ((*fileData).cursor_x > cursorLineLength) + { + (*fileData).cursor_x = cursorLineLength; + } + } + *stop = 1; + break; + case 2: + if ((*fileData).cursor_y < (*fileData).numLines - 1) + { + (*fileData).cursor_y++; + int cursorLineLength = lineLength(*content, (*fileData).cursor_y); + (*fileData).cursor_x = *across; + if ((*fileData).cursor_x > cursorLineLength) + { + (*fileData).cursor_x = cursorLineLength; + } + } + *stop = 1; + break; + case 4: + if ((*fileData).cursor_x > 0) + { + (*fileData).cursor_x--; + *across = (*fileData).cursor_x; + } + else if ((*fileData).cursor_y > 0) + { + (*fileData).cursor_y--; + *across = lineLength(*content, (*fileData).cursor_y); + } + cursorLineLength = lineLength(*content, (*fileData).cursor_y); + (*fileData).cursor_x = *across; + if ((*fileData).cursor_x > cursorLineLength) + { + (*fileData).cursor_x = cursorLineLength; + } + *stop = 1; + break; + case 5: + lineWidth = lineLength(*content, (*fileData).cursor_y); + if (*across < lineWidth) + { + *across += 1; + } + else if ((*fileData).cursor_y < (*fileData).numLines - 1) + { + *across = 0; + (*fileData).cursor_y++; + } + cursorLineLength = lineLength(*content, (*fileData).cursor_y); + (*fileData).cursor_x = *across; + if ((*fileData).cursor_x > cursorLineLength) + { + (*fileData).cursor_x = cursorLineLength; + } + *stop = 1; + break; + } + return; +} diff --git a/cursorMovement.h b/cursorMovement.h new file mode 100644 index 0000000..eb50d1b --- /dev/null +++ b/cursorMovement.h @@ -0,0 +1,8 @@ +#ifndef CURSORMOVEMENT_H +#define CURSORMOVEMENT_H + +#include "openFile.h" + +void cursorMovement(char choice, struct FileData *fileData, char **content, int *across, int *stop); + +#endif // CURSORMOVEMENT_H diff --git a/handleCommand.c b/handleCommand.c deleted file mode 100644 index e69de29..0000000 diff --git a/lengths.c b/lengths.c new file mode 100644 index 0000000..982d1e6 --- /dev/null +++ b/lengths.c @@ -0,0 +1,64 @@ +#include "lengths.h" + +#include + +struct lineAndCharNum getLineAndCharNum(char *content) +{ + int numLines = 1; + int numChars = strlen(content); + for (int i = 0; i < numChars; i++) + { + if (content[i] == '\n') + { + numLines++; + } + } + return (struct lineAndCharNum){numLines, numChars}; +} + +int lineLength(char *content, int cursor_y) +{ + int numLines = 0; + int numChars = strlen(content); + int across = 0; + for (int i = 0; i < numChars; i++) + { + if (content[i] == '\n') + { + if (numLines == cursor_y) + { + return across; + } + numLines++; + across = 0; + } + else + { + across++; + } + } + return across; +} + +int cursorToCharPos(char *content, int cursor_x, int cursor_y) +{ + if (cursor_y == 0) + { + return cursor_x; + } + int numLines = 0; + int numChars = strlen(content); + int i; + for (i = 0; i < numChars; i++) + { + if (content[i] == '\n') + { + numLines++; + if (numLines == cursor_y) + { + return i + cursor_x + 1; + } + } + } + return i; +} diff --git a/lengths.h b/lengths.h new file mode 100644 index 0000000..f85ca18 --- /dev/null +++ b/lengths.h @@ -0,0 +1,16 @@ +#ifndef LENGTHS_H +#define LENGTHS_H + +struct lineAndCharNum +{ + int lineNum; + int charNum; +}; + +struct lineAndCharNum getLineAndCharNum(char *content); + +int lineLength(char *content, int cursor_y); + +int cursorToCharPos(char *content, int cursor_x, int cursor_y); + +#endif // LENGTHS_H diff --git a/list.c b/list.c deleted file mode 100644 index e69de29..0000000 diff --git a/main.c b/main.c index c87f3b2..2d2b5db 100644 --- a/main.c +++ b/main.c @@ -5,13 +5,15 @@ #include #include #include "openFile.h" +#include int main(int argc, char *argv[]) { + signal(SIGINT, SIG_IGN); char *path = ""; if (argc > 1) { path = argv[1]; } return openFile(path); -} \ No newline at end of file +} diff --git a/openFile.c b/openFile.c index 9994546..1fc3ed3 100644 --- a/openFile.c +++ b/openFile.c @@ -1,6 +1,8 @@ #include "openFile.h" #include "config.h" #include "render.h" +#include "cursorMovement.h" +#include "lengths.h" #include #include @@ -9,85 +11,15 @@ #include #include -struct lineAndCharNum -{ - int lineNum; - int charNum; -}; - -struct lineAndCharNum getLineAndCharNum(char *content) -{ - int numLines = 1; - int numChars = strlen(content); - for (int i = 0; i < numChars; i++) - { - if (content[i] == '\n') - { - numLines++; - } - } - return (struct lineAndCharNum){numLines, numChars}; -} - -int lineLength(char *content, int cursor_y) -{ - int numLines = 0; - int numChars = strlen(content); - int across = 0; - for (int i = 0; i < numChars; i++) - { - if (content[i] == '\n') - { - if (numLines == cursor_y) - { - return across; - } - numLines++; - across = 0; - } - else - { - across++; - } - } - return across; -} - -int cursorToCharPos(char *content, int cursor_x, int cursor_y) -{ - if (cursor_y == 0) - { - return cursor_x; - } - int numLines = 0; - int numChars = strlen(content); - int across = 0; - int i; - for (i = 0; i < numChars; i++) - { - if (content[i] == '\n') - { - numLines++; - if (numLines == cursor_y) - { - return i + cursor_x + 1; - } - across = 0; - } - else - { - across++; - } - } - return i; -} - int openFile(char *Path) { char *path = malloc(strlen(Path) + 1); + char *message = malloc(1); + message[0] = '\0'; strcpy(path, Path); ESCDELAY = 0; - char *content = malloc(0); + char *content = malloc(1); + content[0] = '\0'; if (strlen(path) != 0) { @@ -105,6 +37,8 @@ int openFile(char *Path) fclose(fptr); } } + char *content_COPY = malloc(strlen(content) + 1); + strcpy(content_COPY, content); // path = malloc(100); int mode = 0; char *command = malloc(1); @@ -125,9 +59,7 @@ int openFile(char *Path) int across = 0; int choice; - int cursorLineLength; char *newContent; - int lineWidth; int running = 1; while (1) { @@ -136,73 +68,7 @@ int openFile(char *Path) int stop = 0; if (mode != 2) { - switch (choice) - { - case KEY_UP: - if (fileData.cursor_y > 0) - { - fileData.cursor_y--; - int cursorLineLength = lineLength(content, fileData.cursor_y); - fileData.cursor_x = across; - if (fileData.cursor_x > cursorLineLength) - { - fileData.cursor_x = cursorLineLength; - } - } - stop = 1; - break; - case KEY_DOWN: - if (fileData.cursor_y < fileData.numLines - 1) - { - fileData.cursor_y++; - int cursorLineLength = lineLength(content, fileData.cursor_y); - fileData.cursor_x = across; - if (fileData.cursor_x > cursorLineLength) - { - fileData.cursor_x = cursorLineLength; - } - } - stop = 1; - break; - case KEY_LEFT: - if (fileData.cursor_x > 0) - { - fileData.cursor_x--; - across = fileData.cursor_x; - } - else if (fileData.cursor_y > 0) - { - fileData.cursor_y--; - across = lineLength(content, fileData.cursor_y); - } - cursorLineLength = lineLength(content, fileData.cursor_y); - fileData.cursor_x = across; - if (fileData.cursor_x > cursorLineLength) - { - fileData.cursor_x = cursorLineLength; - } - stop = 1; - break; - case KEY_RIGHT: - lineWidth = lineLength(content, fileData.cursor_y); - if (across < lineWidth) - { - across++; - } - else if (fileData.cursor_y < fileData.numLines - 1) - { - across = 0; - fileData.cursor_y++; - } - cursorLineLength = lineLength(content, fileData.cursor_y); - fileData.cursor_x = across; - if (fileData.cursor_x > cursorLineLength) - { - fileData.cursor_x = cursorLineLength; - } - stop = 1; - break; - } + cursorMovement(choice, &fileData, &content, &across, &stop); } if (!stop) { @@ -281,11 +147,10 @@ int openFile(char *Path) if (strlen(content) > 0) { int cursorPos = cursorToCharPos(content, fileData.cursor_x, fileData.cursor_y); - if (cursorPos == strlen(content)) + if (cursorPos == (int)strlen(content)) { break; } - int length = lineLength(content, fileData.cursor_y); char *newContent = malloc(strlen(content)); strncpy(newContent, content, cursorPos); strncpy(newContent + cursorPos, content + cursorPos + 1, strlen(content) - cursorPos); @@ -353,18 +218,18 @@ int openFile(char *Path) case 's': if (strlen(command) > 2) { - char *newPath = malloc(strlen(command) - 2); - strncpy(newPath, command + 2, strlen(command) - 2); - newPath[strlen(command) - 2] = '\0'; + path = realloc(path, strlen(command) - 2); + strncpy(path, command + 2, strlen(command) - 2); + path[strlen(command) - 2] = '\0'; FILE *fptr; - fptr = fopen(newPath, "w"); + fptr = fopen(path, "w"); if (fptr != NULL) { fwrite(content, 1, strlen(content), fptr); fclose(fptr); + content_COPY = realloc(content_COPY, strlen(content) + 1); + strcpy(content_COPY, content); } - free(path); - path = newPath; } break; case 'o': @@ -466,7 +331,9 @@ int openFile(char *Path) free(content); free(command); + free(content_COPY); free(path); + free(message); // Clean up and exit ncurses endwin(); diff --git a/openFile.h b/openFile.h index 3fcdd79..084d957 100644 --- a/openFile.h +++ b/openFile.h @@ -19,4 +19,4 @@ struct FileData // Function to open a file int openFile(char *path); -#endif // OPENFILE_H \ No newline at end of file +#endif // OPENFILE_H diff --git a/render.c b/render.c index ff268ed..73e3535 100644 --- a/render.c +++ b/render.c @@ -154,4 +154,4 @@ void render(struct FileData fileData) refresh(); free(line); -} \ No newline at end of file +}