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