summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2023-10-28 10:54:05 +0200
committerOskar <[email protected]>2023-10-28 10:54:05 +0200
commitc09957c3026aa7f25d7bc7a0379d961990bc9e34 (patch)
treec71abbca0621a1bb76d1bf44dfecef0596d2a8c2
parent6e381ded456981842994cce8a205bf4259ff95c8 (diff)
modified GET_LINE() to make it return the index of the first character of the line you are focused on
-rw-r--r--7ed.h2
-rw-r--r--functions.c17
-rw-r--r--startmode.c4
3 files changed, 14 insertions, 9 deletions
diff --git a/7ed.h b/7ed.h
index 3686773..c7236f0 100644
--- a/7ed.h
+++ b/7ed.h
@@ -52,6 +52,6 @@ int startmode(char filename[]);
#ifndef GET_LINE_H
#define GET_LINE_H
-int GET_LINE(char filename[], long focus, char **line);
+int GET_LINE(char filename[], long focus, char **line, size_t *start);
#endif /* GET_LINE_H */
diff --git a/functions.c b/functions.c
index 324dd42..34e0dd7 100644
--- a/functions.c
+++ b/functions.c
@@ -121,16 +121,16 @@ void shuffle(char arr[], int n) {
}
}
-int GET_LINE(char filename[], long focus, char **line) { // Making this function was hell. Hardest thing ive coded in a while.
+int GET_LINE(char filename[], long focus, char **line, size_t *start) { // Making this function was hell. Hardest thing ive coded in a while.
size_t lines;
int ret = COUNT_LINES_IN_FILE(filename, &lines);
- if (ret == 1) {
- return EXIT_FAILURE;
+ if (ret == 1) {
+ return EXIT_FAILURE;
}
- if ((long)lines < focus) {
- return EXIT_FAILURE;
+ if ((long)lines < focus) { // check if focus is bigger than the amount of
+ return EXIT_FAILURE; // lines in the actual file and returns exit failure
}
FILE *file;
@@ -153,7 +153,7 @@ int GET_LINE(char filename[], long focus, char **line) { // Making this function
} else {
c1_count++;
}
- }
+ } // checks how many characters are in the first line
char c1buf[c1_count];
fseek(file, 0, SEEK_SET);
@@ -166,8 +166,10 @@ int GET_LINE(char filename[], long focus, char **line) { // Making this function
*line = (char *)malloc(strlen(c1buf) + 1);
if (*line != NULL) {
- strcpy(*line, c1buf);
+ strcpy(*line, c1buf); // Return line 1
}
+
+ *start = 0;
//printf("%s", c1buf); // The purpose of this if statement is that it will only print line 1. Not too elegant of a way to handle this but its the only way i knew how to.
} else {
@@ -214,6 +216,7 @@ int GET_LINE(char filename[], long focus, char **line) { // Making this function
if (*line != NULL) {
strcpy(*line, c2buf);
}
+ *start = save_i+1;
}
fclose(file);
diff --git a/startmode.c b/startmode.c
index 3c5b606..9744b0b 100644
--- a/startmode.c
+++ b/startmode.c
@@ -73,11 +73,13 @@ int startmode(char filename[]) {
case 'p':
char *line;
- int ret = GET_LINE(filename, focus, &line);
+ size_t start;
+ int ret = GET_LINE(filename, focus, &line, &start);
if (ret == 1) {
return EXIT_FAILURE;
}
printf("%s", line);
+ //printf("%ld", start);
free(line);
break;