-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
147 lines (138 loc) · 3.69 KB
/
functions.c
File metadata and controls
147 lines (138 loc) · 3.69 KB
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include "invindex.h"
#include <dirent.h>
#define MAX_ARGS 10
#define BUFFERSIZE 256
int makePipes(int N){
/**********************************************
* creates 2 named piped for each worker *
* N: number of workers *
* returns: 1 on success , -1 on failure *
***********************************************/
char pipe[128];
int i;
for(i=0;i<N;i++){
sprintf(pipe,"Worker2Executor%d",i);
if(mkfifo(pipe, 0666) == -1){
if(errno!=EEXIST){
perror("Make fifo failed.");
return -1;
}
}
sprintf(pipe,"Executor2Worker%d",i);
if(mkfifo(pipe, 0666) == -1){
if(errno!=EEXIST){
perror("Make fifo failed.");
return -1;
}
}
}
}
int readInt(int fd){
int input;
while(1){
if(read(fd,&input,sizeof(int))>0){
return input;
}
}
}
char *readString(int fd){
char *input = malloc(sizeof(char)*BUFFERSIZE);
while(1){
if(read(fd,input,BUFFERSIZE)>0){
return input;
}
}
}
int openPipes(int N,int *fdRead,int *fdWrite){
/**********************************************
* opens pipes for writing and reading *
* N: number of workers *
* returns: 1 on success , -1 on failure *
***********************************************/
int i;
char pipe[BUFFERSIZE];
for(i=0;i<N;i++){
sprintf(pipe,"Executor2Worker%d",i);
if((fdWrite[i]=open(pipe, O_WRONLY)) < 0){ /*pipes for writing wait for the read-end to be opened*/
perror("4 fifo open problem");
return -1;
}
sprintf(pipe,"Worker2Executor%d",i);
if((fdRead[i]=open(pipe, O_RDONLY | O_NONBLOCK)) < 0){
perror("3 fifo open problem");
return 1;
}
}
}
InvertedIndex *CombineSearchResults(InvertedIndex **results,int noResults){
/* this function gets noResults Inverted Indexes and puts them in one*/
InvertedIndex *result = NULL;
pList *current = NULL;
offsetList *offset_list;
int i;
for(i=0;i<noResults;i++){ //for every result
if(results[i] == NULL) continue;
current = results[i]->postingList;
while(current!=NULL){
offset_list = current->offsets;
while(offset_list!=NULL){ /*parse every row of path in inverted index*/
ResultsInvertedIndexUpdate(&result,current->path,offset_list->row_offset);
offset_list = offset_list->next;
}
current = current->next;
}
}
return result;
}
void CountTextBWL(char *path,int *bytes,int *words,int *lines){
int b=0,w=0,l=0;
FILE *fp;
char c , prev_c = ' ';
fp = fopen(path,"r");
while((c=getc(fp))!=-1){
b++;
if(c == ' ' && prev_c!= ' ' && prev_c!='\n') w++;
else if(c == '\n'){
l++;
if(prev_c!='\n' &&prev_c!=' ') w++;
}
prev_c = c;
}
*bytes = b;
*words = w;
*lines = l;
return;
}
void CountTotalBWL(char **dir_names,int dirs,int *totalBytes,int *totalWords,int *totalLines){
*totalBytes=0;
*totalWords=0;
*totalLines=0;
int b,w,l,j;
DIR *dir;
struct dirent *d;
char file[BUFFERSIZE];
for(j=0;j<dirs;j++){
if((dir=opendir(dir_names[j]))!=NULL){
while((d = readdir(dir))!=NULL){
if(strcmp(d->d_name,".")!=0 && strcmp(d->d_name,"..")!=0){
strcpy(file,dir_names[j]);
strcat(file,d->d_name);
CountTextBWL(file,&b,&w,&l);
*totalBytes+=b;
*totalWords+=w;
*totalLines+=l;
}
memset(file,'\0',256);
}
closedir(dir);
}
}
}