annotate basedfilelib.h @ 1:b82f21466dfd

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