annotate basedfilelib.h @ 4:0ed615367b10

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