annotate basedfilelib.h @ 19:8fe0b5711413

Added BasedTremGrx and Dvorak
author VilyaemKenyaz
date Wed, 27 Sep 2023 01:47:28 -0400
parents 1b0ebe86b44c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
1 /*********************************************
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
2 * Description - BasedFileLib makes file I/O less like *nix and more like
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
3 * TempleOS
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
4 * Author - William King
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
5 * Date - Sep 25 2023
8fe0b5711413 Added BasedTremGrx and Dvorak
VilyaemKenyaz
parents: 2
diff changeset
6 * *******************************************/
2
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
7
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
8 #define MAX_LINES 100000
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
9 #define MAXLENGTH 512
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
10
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
11 char* ReadFile(const char* fileName) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
12 FILE* file = fopen(fileName, "rb");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
13 if (file == NULL) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
14 perror("Error opening file");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
15 return NULL;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
16 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
17
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
18 fseek(file, 0, SEEK_END);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
19 long fileSize = ftell(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
20 rewind(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
21
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
22 char* fileContent = (char*)malloc(fileSize * sizeof(char));
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
23 if (fileContent == NULL) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
24 perror("Error allocating memory for file content");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
25 fclose(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
26 return NULL;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
27 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
28
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
29 size_t readSize = fread(fileContent, sizeof(char), fileSize, file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
30 if (readSize != fileSize) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
31 perror("Error reading file");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
32 fclose(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
33 free(fileContent);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
34 return NULL;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
35 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
36
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
37 fclose(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
38 return fileContent;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
39 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
40
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
41 void WriteFile(const char* fileName, const char* content) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
42 FILE* file = fopen(fileName, "wb");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
43 if (file == NULL) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
44 perror("Error opening file");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
45 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
46
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
47 size_t writeSize = fwrite(content, sizeof(char), strlen(content), file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
48 if (writeSize != strlen(content)) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
49 perror("Error writing to file");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
50 fclose(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
51 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
52
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
53 fclose(file);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
54 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
55
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
56
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
57 char* DeleteLine(char* str, unsigned int line_number) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
58 char* lines[MAX_LINES];
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
59 char* line;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
60 unsigned int current_line = 0;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
61
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
62 line = strtok(str, "\n");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
63 while (line != NULL && current_line < MAX_LINES) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
64 lines[current_line++] = line;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
65 line = strtok(NULL, "\n");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
66 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
67
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
68 // Delete the specified line
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
69 if (line_number > 0 && line_number <= current_line) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
70 for (unsigned int i = line_number - 1; i < current_line - 1; i++) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
71 lines[i] = lines[i + 1];
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
72 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
73 current_line--;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
74 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
75
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
76 // Reconstruct the string with remaining lines
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
77 str[0] = '\0';
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
78 for (unsigned int i = 0; i < current_line; i++) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
79 strcat(str, lines[i]);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
80 strcat(str, "\n");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
81 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
82
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
83 return str;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
84 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
85
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
86
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
87
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
88 char* ReadLine(const char *str, unsigned int lineNum) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
89 int currentLine = 1;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
90 size_t strLen = strlen(str);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
91 char *line = NULL;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
92
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
93 line = malloc(strLen + 1);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
94 if (line == NULL) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
95 fprintf(stderr, "Error: memory allocation failed.\n");
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
96 exit(1);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
97 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
98
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
99 int lineStartIndex = 0;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
100 int lineEndIndex = 0;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
101
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
102 // Traverse through the string until the desired line or the end of the string
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
103 while (currentLine <= lineNum && lineEndIndex < strLen) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
104 // Find the start index of the current line
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
105 while (lineStartIndex < strLen && (str[lineStartIndex] == '\n' || str[lineStartIndex] == '\r')) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
106 lineStartIndex++;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
107 lineEndIndex++;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
108 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
109
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
110 // Find the end index of the current line
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
111 lineEndIndex = lineStartIndex;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
112 while (lineEndIndex < strLen && str[lineEndIndex] != '\n' && str[lineEndIndex] != '\r') {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
113 lineEndIndex++;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
114 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
115
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
116 // Check if the desired line is found
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
117 if (currentLine == lineNum) {
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
118 // Copy the line to the allocated memory
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
119 strncpy(line, str + lineStartIndex, lineEndIndex - lineStartIndex);
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
120 line[lineEndIndex - lineStartIndex] = '\0';
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
121 break;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
122 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
123
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
124 // Move to the start of the next line
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
125 currentLine++;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
126 lineStartIndex = lineEndIndex;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
127 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
128
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
129 return line;
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
130 }
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
131
1b0ebe86b44c Automated Vimcurial commmit
VilyaemKenyaz
parents:
diff changeset
132 //BasedFileLib