summaryrefslogtreecommitdiff
path: root/editmode.c
diff options
context:
space:
mode:
Diffstat (limited to 'editmode.c')
-rw-r--r--editmode.c100
1 files changed, 74 insertions, 26 deletions
diff --git a/editmode.c b/editmode.c
index a53e976..a5d7641 100644
--- a/editmode.c
+++ b/editmode.c
@@ -6,11 +6,69 @@
#include "7ed.h"
#include <stdint.h>
-int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) {
+int delete_specified_newline(char filename[], long focus) { // special version of write_line that does as the name says
char *line;
size_t start;
- int ret = GET_LINE(filename, focus, &line, &start); // get start position of focus
+ int ret = get_line(filename, focus, &line, &start); // get start position of focus
+ if (ret == 1) {
+ return EXIT_FAILURE;
+ }
+ // TODO: Some code that checks if its the first line in the file. Because such a situation might cause a problem. Im not entirely sure yet.
+
+ start = start-1; // decrement by 1 to place start position at the newline
+
+ FILE *file;
+ FILE *temp;
+ file = fopen(filename,"r"); // Open file
+ temp = fopen("temp.7ed","w+"); // Open temp file
+ if (file == NULL || temp == NULL) { // Check if you can open file
+ fprintf(stderr, "Cannot open file. delete_specified_newline\n");
+ return 1;
+ }
+
+ fseek(file, 0, SEEK_SET); // go to start of file
+ size_t counter = 0;
+ char ch;
+ for (;; counter++) { // for loop that puts contents of file in to temp
+ // when counter is equal to start, the buffer with the edited content is written to temp
+ if (start == counter) { // after that it uses fseek to point temp right after the written content
+
+ fseek(temp, start, SEEK_SET); // After that the function keeps writing everything else to temp
+ fseek(file, start+1, SEEK_SET); // +1 to skip over the newline and thus having it be removed
+ }
+ ch = fgetc(file);
+ if (ch == EOF) {
+ break;
+ }
+ fputc(ch, temp);
+
+ }
+ fseek(temp, 0, SEEK_SET); // reset both files to start at 0 so it doesnt mess with fread and fwrite
+ fclose(file);
+ file = fopen(filename, "w"); // open file again to write the rest
+ if (file == NULL) { // Check if you can open file
+ fprintf(stderr, "Cannot open file for writing.\n");
+ return 1;
+ }
+ char buffer[BUF_SZ_2];
+ size_t bytes_read;
+ while ((bytes_read = fread(buffer, 1, BUF_SZ_2, temp)) > 0) { // Write contents of temp to file
+ fwrite(buffer, 1, bytes_read, file);
+ }
+ free(line);
+ fclose(file);
+ fclose(temp);
+ remove("temp.7ed");
+ return 0;
+
+}
+
+int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer_size) { // writes text at specified line
+
+ char *line;
+ size_t start;
+ int ret = get_line(filename, focus, &line, &start); // get start position of focus
if (ret == 1) {
return EXIT_FAILURE;
}
@@ -74,7 +132,7 @@ int write_line(char filename[], long focus, char editbuffer[], size_t editbuffer
}
-int check_end_newline(char filename[]) {
+int check_end_newline(char filename[]) { // function that checks if a file ends with a newline
FILE *file;
file = fopen(filename, "r");
@@ -92,7 +150,7 @@ int check_end_newline(char filename[]) {
}
-int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again
+int new_line(char filename[], long new_line_pos_temp) { // creates a new line within a file after a specified line number
long new_line_pos;
if (new_line_pos_temp < 0) {
@@ -104,13 +162,17 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again
int cen = check_end_newline(filename);
if (cen == -1) {
size_t linecount;
- int clif = COUNT_LINES_IN_FILE(filename, &linecount);
+ int clif = count_lines_in_file(filename, &linecount);
if (clif == 1) {
return 1;
}
if (linecount == (size_t)new_line_pos) { // if the file ends without a newline and the user wants to put a newline at the end, then this code within the if-statement will do that.
FILE *file;
file = fopen(filename, "a");
+ if (file == NULL) {
+ fprintf(stderr, "Cannot open file. new_line()\n");
+ return 1;
+ }
fputc('\n', file);
fputc('\n', file);
fclose(file);
@@ -123,7 +185,7 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again
// line2 is the line right after line
size_t start; // start is the position at the start of line
// start2 is the position at the start of line2
- int ret = GET_LINE(filename, new_line_pos+1, &line, &start);
+ int ret = get_line(filename, new_line_pos+1, &line, &start);
if (ret == 1) {
return 1;
}
@@ -142,11 +204,11 @@ int NEW_LINE(char filename[], long new_line_pos_temp) { // doin this test again
}
-int remove_line_contents(char filename[], long focus) {
+int remove_line_contents(char filename[], long focus) { // removes contents of a specified line without removing the newline
char *line;
size_t start;
- int ret = GET_LINE(filename, focus, &line, &start);
+ int ret = get_line(filename, focus, &line, &start);
if (ret == 1) {
return 1;
}
@@ -158,31 +220,18 @@ int remove_line_contents(char filename[], long focus) {
write_line(filename, focus, editbuffer, 1); // remove contents in line
if (cen == -1) { // put newline at end if cen == -1
- NEW_LINE(filename, focus-1);
+ new_line(filename, focus-1);
}
free(line);
return 0;
}
-int delete_line(char filename[], long focus) {
-
- char *line;
- size_t start;
- int ret = GET_LINE(filename, focus, &line, &start);
- if (ret == 1) {
- return 1;
- }
-
- free(line);
- return 0;
-}
-
-int editmode(char filename[], long focus) {
+int editmode(char filename[], long focus) { // the editing interface
char *line;
size_t start;
- int ret = GET_LINE(filename, focus, &line, &start);
+ int ret = get_line(filename, focus, &line, &start);
if (ret == 1) {
return EXIT_FAILURE;
}
@@ -202,10 +251,9 @@ int editmode(char filename[], long focus) {
}
fprintf(stdout, "Do you want to write the changes?\n");
- int yesno = CHOICE();
+ int yesno = choice();
if (yesno == 1) {
- printf("N\n");
return 0;
}