annotate based/basedfilelib.h @ 21:44e906aeea91 default tip

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