Read from files
This commit is contained in:
parent
bee0b9ef57
commit
451d04cccf
19
main.c
19
main.c
@ -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
23
util.c
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user