annotate scripturereader.c @ 2:44859795513d default tip

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