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