comparison scripturereader.c @ 0:6e1857de3922

Finished
author VilyaemKenyaz
date Fri, 25 Aug 2023 14:14:24 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6e1857de3922
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define PAGE_SIZE 50
6
7 void print_page(const char* filename, int page_number, int total_pages) {
8 system("clear");
9 printf("---SCRIPTURE-READER---\n");
10 FILE* file = fopen(filename, "r");
11 if (file == NULL) {
12 printf("Error opening file: %s\n", filename);
13 exit(1);
14 }
15
16 char line[256];
17 int line_count = 0;
18 int current_page = 1;
19
20 while (fgets(line, sizeof(line), file)) {
21 line_count++;
22 if (line_count > (page_number - 1) * PAGE_SIZE && line_count <= page_number * PAGE_SIZE) {
23 printf("%s", line);
24 }
25 if (line_count > page_number * PAGE_SIZE) {
26 break;
27 }
28 }
29
30 fclose(file);
31
32 printf("\n--- Page %d of %d ---\n", page_number, total_pages);
33 }
34
35 int get_total_pages(const char* filename) {
36 FILE* file = fopen(filename, "r");
37 if (file == NULL) {
38 printf("Error opening file: %s\n", filename);
39 exit(1);
40 }
41
42 char line[256];
43 int line_count = 0;
44
45 while (fgets(line, sizeof(line), file)) {
46 line_count++;
47 }
48
49 fclose(file);
50
51 return (line_count + PAGE_SIZE - 1) / PAGE_SIZE;
52 }
53
54 int main(int argc, char* argv[]) {
55 if (argc < 2 || argc > 3) {
56 printf("ScriptureReader by William @ peepsoftgames.github.io thekenyaz@yandex.com\n");
57 printf("Usage:\n");
58 printf(" scripturereader <textfile>\n");
59 return 1;
60 }
61
62 const char* filename = argv[1];
63
64 int total_pages = get_total_pages(filename);
65 int current_page = 1;
66
67 while (1) {
68 system("clear");
69 print_page(filename, current_page, total_pages);
70
71 char ch;
72 scanf(" %c", &ch);
73 switch (ch) {
74 case 'l':
75 case 'L':
76 if (current_page > 1) {
77 current_page--;
78 }
79 break;
80 case 'r':
81 case 'R':
82 if (current_page < total_pages) {
83 current_page++;
84 }
85 break;
86 case 'q':
87 case 'Q':
88 exit(0);
89 }
90 }
91
92 exit(0);
93 }
94