summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--7ed.h9
-rw-r--r--editmode.c77
-rw-r--r--functions.c4
-rw-r--r--startmode.c8
-rw-r--r--test/newlinetest10
-rw-r--r--test/testgetline.c4
6 files changed, 100 insertions, 12 deletions
diff --git a/7ed.h b/7ed.h
index 733be83..83495bf 100644
--- a/7ed.h
+++ b/7ed.h
@@ -61,4 +61,11 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start);
int editmode(char filename[], long focus);
-#endif /* EDITMODE_H */ \ No newline at end of file
+#endif /* EDITMODE_H */
+
+#ifndef NEW_LINE_H
+#define NEW_LINE_H
+
+int NEW_LINE(char filename[]);
+
+#endif /* NEW_LINE_H */ \ No newline at end of file
diff --git a/editmode.c b/editmode.c
index 67bd17e..b010383 100644
--- a/editmode.c
+++ b/editmode.c
@@ -16,10 +16,10 @@ if (ret == 1) {
FILE *file;
FILE *temp;
-file = fopen(filename,"r+"); // Open file
-temp = fopen("temp.7ed","r+");
+file = fopen(filename,"r"); // Open file
+temp = fopen("temp.7ed","w+");
if (file == NULL || temp == NULL) { // Check if you can open file
- fprintf(stderr, "Cannot open file.\n");
+ fprintf(stderr, "Cannot open file. write_line\n");
return 1;
}
@@ -54,21 +54,86 @@ for (;; counter++) { // for loop that puts contents of file in to temp
}
fseek(temp, 0, SEEK_SET); // reset both files to start at 0 so it doesnt mess with fread and fwrite
-fseek(file, 0, SEEK_SET);
+fclose(file);
+file = fopen(filename, "w");
+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 NEW_LINE(char filename[]) {
+
+ long new_line_pos;
+ long new_line_pos_temp;
+ char buf[1024];
+ int success;
+
+ do {
+ fprintf(stdout, "Create a new line after: ");
+ if (!fgets(buf, 1024, stdin)) {
+ fprintf(stderr, "Too many characters\n");
+ break;
+ }
+ char *endptr;
+
+ new_line_pos_temp = strtol(buf, &endptr, 10);
+ errno = 0;
+ if (errno == ERANGE) {
+ fprintf(stderr, "Sorry, this number is too small or too large.\n");
+ success = 0;
+ }
+ else if (endptr == buf) {
+ // no character was read
+ success = 0;
+ }
+ else if (*endptr && *endptr != '\n') {
+ success = 0;
+ }
+
+ else {
+ success = 1;
+ }
+
+ } while (!success);
+
+ if (new_line_pos_temp < 1) {
+ fprintf(stderr, "The new line can not be under 0!\n");
+ return 1;
+ } else {
+ new_line_pos = new_line_pos_temp;
+
+ char *line;
+ size_t start;
+ int ret = GET_LINE(filename, new_line_pos, &line, &start);
+ if (ret == 1) {
+ return 1;
+ }
+
+ size_t len = strlen(line);
+ char *newline_and_line = malloc(len+2);
+ strcpy(newline_and_line, line);
+ strcat(newline_and_line, "\n");
+ write_line(filename, new_line_pos, newline_and_line, 1);
+ free(newline_and_line);
+ }
+
+ return 0;
+
+}
+
int editmode(char filename[], long focus) {
char *line;
diff --git a/functions.c b/functions.c
index ac3f789..e5837b7 100644
--- a/functions.c
+++ b/functions.c
@@ -55,7 +55,7 @@ int COUNT_LINES_IN_FILE (char filename[], size_t *lines) {
file = fopen(filename,"rb"); // Open file
if (file == NULL) { // Check if you can open file
- fprintf(stderr, "Cannot open file.\n");
+ fprintf(stderr, "Cannot open file. COUNT_LINES_IN_FILE\n");
return 1;
}
fseek(file, -1, SEEK_END);
@@ -128,7 +128,7 @@ int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Makin
file = fopen(filename,"r"); // Open file
if (file == NULL) { // Check if you can open file
- fprintf(stderr, "Cannot open file.\n");
+ fprintf(stderr, "Cannot open file GET_LINE.\n");
return 1;
}
diff --git a/startmode.c b/startmode.c
index a6f1395..988b6fd 100644
--- a/startmode.c
+++ b/startmode.c
@@ -11,7 +11,7 @@ int NCAT(char filename[]) {
file = fopen(filename,"r");
if (file == NULL) {
- fprintf(stderr, "Cannot open file.\n");
+ fprintf(stderr, "Cannot open file. NCAT\n");
return 1;
}
@@ -127,6 +127,12 @@ int startmode(char filename[]) {
case 'A':
NCAT(filename);
break;
+ case 'n':
+ case 'N':
+
+ NEW_LINE(filename);
+
+ break;
default:
fprintf(stdout, "?\n");
}
diff --git a/test/newlinetest b/test/newlinetest
new file mode 100644
index 0000000..f00c965
--- /dev/null
+++ b/test/newlinetest
@@ -0,0 +1,10 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
diff --git a/test/testgetline.c b/test/testgetline.c
index fb16c54..dfbab4b 100644
--- a/test/testgetline.c
+++ b/test/testgetline.c
@@ -40,5 +40,5 @@ while ((opt = getopt(argc, argv, "i:")) != -1) {
}
return EXIT_SUCCESS;
-
-} \ No newline at end of file
+// TEST!
+} // LINE 44 YOYOYOYOOYOYOOYOYOYOYOYYO