-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2.3.c
169 lines (150 loc) · 3.68 KB
/
m2.3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
long index;
long line;
} char_info;
typedef enum {
find_ok,
find_str_inv,
find_eof,
find_not_ok,
find_cnt_inv,
find_stream_null,
find_bad_alloc
} find_substr_st;
bool is_equal_s(const char* s1, const char* s2) {
while (*s1 != '\0' && *s2 != '\0') {
if (*s1 != *s2) {
return false;
}
s1++;
s2++;
}
if (*s1 != '\0' || *s2 != '\0') {
return false;
}
return true;
}
find_substr_st find_substr_in_file(char_info* info, const char* str, FILE* stream, char_info* ptr_pos) {
size_t len = strlen(str);
char* strbuf = (char*) malloc(sizeof(char) * (len + 1));
if (strbuf == NULL) {
return find_not_ok;
}
strbuf[len] = '\0';
while (fread(strbuf, sizeof(char), len, stream) == len) {
fseek(stream, 1 - len, SEEK_CUR);
ptr_pos->index++;
if (is_equal_s(str, strbuf)) {
info->index = ptr_pos->index;
info->line = ptr_pos->line;
if (*strbuf == '\n') {
ptr_pos->line++;
ptr_pos->index = -1;
}
return find_ok;
}
if (*strbuf == '\n') {
ptr_pos->line++;
ptr_pos->index = -1;
}
}
return find_eof;
}
find_substr_st find_all_substr(const char* str, const char* filename, char_info** substr_indexes) {
FILE* stream = fopen(filename, "r");
if (stream == NULL) {
return find_stream_null;
}
*substr_indexes = (char_info*) malloc(sizeof(char_info));
if (*substr_indexes == NULL) {
return find_bad_alloc;
}
size_t size = 1;
char_info res_index, cur_index = {.index = -1, .line = 1};
int rv;
while ((rv = find_substr_in_file(&res_index, str, stream, &cur_index)) == find_ok) {
char_info* temp;
(*substr_indexes)[size - 1] = res_index;
temp = (char_info*) realloc(*substr_indexes, (++size) * sizeof(char_info));
if (temp == NULL) {
free(*substr_indexes);
substr_indexes = NULL;
return find_bad_alloc;
}
*substr_indexes = temp;
}
if (rv == find_not_ok) {
free(*substr_indexes);
*substr_indexes = NULL;
return find_bad_alloc;
}
(*substr_indexes)[size - 1].line = -1;
fclose(stream);
return find_ok;
}
void erase(char_info** info, size_t cnt) {
char_info** ptr = info;
for (int i = 0; i < cnt; i++) {
free(*ptr);
ptr++;
}
free(info);
}
find_substr_st find_all_substr_in_multiple_files(char_info*** indexes_arr, int count, const char* substr, ...) {
if(substr[0] == 0) {
return find_str_inv;
}
va_list a;
if (count == 0) {
return find_cnt_inv;
}
*indexes_arr = (char_info**) malloc(sizeof(char_info*) * count);
va_start(a, substr);
for (int i = 0; i < count; i++) {
char* filename = va_arg(a, char*);
char_info* indexes;
int rv = find_all_substr(substr, filename, &indexes);
if (rv == find_ok) {
(*indexes_arr)[i] = indexes;
} else{
(*indexes_arr)[i] = NULL;
}
}
va_end(a);
return 0;
}
void print(char_info* const* info, size_t cnt) {
char_info* const* info_p = info;
for (int i = 0; i < cnt; i++) {
char_info const* ptr = *info_p;
if (ptr == NULL) {
continue;
}
while (ptr->line != -1) {
printf("substr on %ld line, %ld index\n", ptr->line, ptr->index);
ptr++;
}
info_p++;
putchar('\n');
}
}
void print_erase(char_info** info, size_t cnt) {
print(info, cnt);
erase(info, cnt);
}
int main() {
char_info** indexes_arr;
if (find_all_substr_in_multiple_files(&indexes_arr, 3, "sv", "test.txt", "../test.txt", "test2.txt") == find_ok) {
print_erase(indexes_arr, 3);
indexes_arr = NULL;
} else {
printf("something went wrong\n");
}
return 0;
}