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

23
util.c
View File

@ -1,7 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h>
void print_array(int *array, int length) { void print_array(int *array, int length) {
if (length == 0) return; if (length == 0) {
printf("\n");
return;
}
printf("|"); printf("|");
@ -10,3 +14,20 @@ void print_array(int *array, int length) {
} }
printf("\n"); 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); void print_array(int *, int);
char *read_file(char *filepath, int *len);
#endif //KMP_UTIL_H #endif //KMP_UTIL_H