Read from files

This commit is contained in:
Raffaele Mignone 2021-03-19 17:56:57 +01:00
parent bee0b9ef57
commit 451d04cccf
Signed by: norangebit
GPG Key ID: F5255658CB220573
3 changed files with 32 additions and 12 deletions

19
main.c
View File

@ -8,10 +8,6 @@
#define MASTER 0
#define DEFAULT_TAG 17
char *read_text();
char *read_pattern();
void find_end(int, char *, char *, int **, int *);
void apply_shift(int, int *, int);
@ -31,12 +27,12 @@ int main() {
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == MASTER) {
text = read_text();
pattern = read_pattern();
text = read_file("data/text.txt", &text_len);
pattern = read_file("data/pattern.txt", &pattern_len);
printf("text: %s\n", text);
printf("pattern: %s\n", pattern);
text_len = strlen(text);
pattern_len = strlen(pattern);
private_text_len = text_len / size;
remain = text_len % size;
@ -130,9 +126,8 @@ int main() {
}
}
char *read_text() {
// TODO
return "ciaoooooo mamma come stai???";
char *read_text(int *len) {
return read_file("data/text.txt", len);
}
char *read_pattern() {
@ -146,6 +141,7 @@ void find_end(int residue, char *pattern, char *text, int **matches, int *match_
int text_len = strlen(text);
int pattern_len = strlen(pattern);
// TODO: match splitted on 3+ node
if (pattern_len - residue > text_len) return;
while (pattern_index < pattern_len && pattern[pattern_index] == text[text_index]) {
@ -174,3 +170,4 @@ int sum_array(int *array, int size) {
return sum;
}

23
util.c
View File

@ -1,7 +1,11 @@
#include <stdio.h>
#include <malloc.h>
void print_array(int *array, int length) {
if (length == 0) return;
if (length == 0) {
printf("\n");
return;
}
printf("|");
@ -10,3 +14,20 @@ void print_array(int *array, int length) {
}
printf("\n");
}
char *read_file(char *filepath, int *len) {
FILE *f = fopen(filepath, "r");
fseek(f, 0, SEEK_END);
*len = ftell(f);
fseek(f, 0, SEEK_SET);
char *content = (char *) malloc(sizeof(char) * (*len + 1));
fread(content, 1, *len, f);
content[*len] = '\0';
fclose(f);
return content;
}

2
util.h
View File

@ -3,4 +3,6 @@
void print_array(int *, int);
char *read_file(char *filepath, int *len);
#endif //KMP_UTIL_H